|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+import json
|
|
|
2
|
+import requests
|
|
|
3
|
+from mastodon import Mastodon
|
|
|
4
|
+import config
|
|
|
5
|
+
|
|
|
6
|
+def get_images(album_api):
|
|
|
7
|
+ return requests.get(album_api).json()['result']['images']
|
|
|
8
|
+
|
|
|
9
|
+def get_next_image(images, previous_id):
|
|
|
10
|
+ tail = [image for image in images if image['id']>previous_id]
|
|
|
11
|
+ return tail and sorted(tail, key=lambda image: image['id'])[0] or None
|
|
|
12
|
+
|
|
|
13
|
+def get_previous_id():
|
|
|
14
|
+ try:
|
|
|
15
|
+ return int(json.load(open("previous_id.txt")))
|
|
|
16
|
+ except Exception:
|
|
|
17
|
+ return 0
|
|
|
18
|
+
|
|
|
19
|
+def set_previous_id(previous_id):
|
|
|
20
|
+ json.dump(previous_id, open("previous_id.txt", "w"))
|
|
|
21
|
+
|
|
|
22
|
+def toot_next_image():
|
|
|
23
|
+ mastodon = Mastodon(access_token="secrets.txt")
|
|
|
24
|
+ image = get_next_image(get_images(config.ALBUM_API), get_previous_id())
|
|
|
25
|
+ if image:
|
|
|
26
|
+ media = mastodon.media_post(
|
|
|
27
|
+ requests.get(
|
|
|
28
|
+ image["derivatives"][config.MEDIA_SIZE]["url"]).content,
|
|
|
29
|
+ mime_type="image/jpeg",
|
|
|
30
|
+ description = config.ALT_TEXT_TEMPLATE.format(**image))
|
|
|
31
|
+ mastodon.status_post(
|
|
|
32
|
+ config.TOOT_TEMPLATE.format(**image),
|
|
|
33
|
+ media_ids=[media],
|
|
|
34
|
+ visibility=config.TOOT_VISIBILITY,
|
|
|
35
|
+ language=config.TOOT_LANGUAGE)
|
|
|
36
|
+ set_previous_id(image['id'])
|
|
|
37
|
+
|
|
|
38
|
+if __name__=='__main__':
|
|
|
39
|
+ toot_next_image()
|