This repository has been archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodonduckbot.py
executable file
·53 lines (43 loc) · 1.62 KB
/
mastodonduckbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3
# Copyright (C) 2024 Vivia Nikolaidou <vivia AT ahiru.eu>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
import os
import requests
from mastodon import Mastodon
duck_url = "https://random-d.uk/api/v2/random"
def main():
"""Post random duck pictures from the above API to Mastodon."""
mastodon = Mastodon(
api_base_url="https://toot.cat",
access_token=os.environ["MASTODON_TOKEN"],
)
resp = requests.get(duck_url, timeout=10)
resp.raise_for_status()
resp_data = resp.json()
image_url = resp_data["url"]
image_resp = requests.get(image_url, stream=True, timeout=10)
image_resp.raise_for_status()
image_resp.raw.decode_content = True
mime_type = image_resp.headers["Content-Type"]
image_comment = resp_data["message"]
media = mastodon.media_post(
image_resp.raw,
mime_type=mime_type,
description="A random duck picture",
synchronous=True,
)
mastodon.status_post(image_comment, media_ids=media)
if __name__ == "__main__":
main()