Переглянути джерело

Use `date_creation` instead of `id` for sorting

master
The Dod 5 дні тому
джерело
коміт
a13020cdc5
2 змінених файлів з 12 додано та 11 видалено
  1. 1
    1
      .gitignore
  2. 11
    10
      bloomscroller.py

+ 1
- 1
.gitignore Переглянути файл

@@ -1,4 +1,4 @@
1 1
 secrets.txt
2
-previous_id.txt
2
+previous_creation_date.txt
3 3
 venv
4 4
 __pycache__

+ 11
- 10
bloomscroller.py Переглянути файл

@@ -2,26 +2,27 @@ import json
2 2
 import requests
3 3
 from mastodon import Mastodon
4 4
 import config
5
+import sys
5 6
 
6 7
 def get_images(album_api):
7 8
     return requests.get(album_api).json()['result']['images']
8 9
 
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
10
+def get_next_image(images, previous_creation_date):
11
+    tail = [image for image in images if image['date_creation']>previous_creation_date]
12
+    return tail and sorted(tail, key=lambda image: image['date_creation'])[0] or None
12 13
 
13
-def get_previous_id():
14
+def get_previous_creation_date():
14 15
    try: 
15
-       return int(json.load(open("previous_id.txt")))
16
+       return json.load(open("previous_creation_date.txt"))
16 17
    except Exception:
17
-       return 0
18
+       return ""
18 19
 
19
-def set_previous_id(previous_id):
20
-    json.dump(previous_id, open("previous_id.txt", "w"))
20
+def set_previous_creation_date(previous_creation_date):
21
+    json.dump(previous_creation_date, open("previous_creation_date.txt", "w"))
21 22
 
22 23
 def toot_next_image():
23 24
     mastodon = Mastodon(access_token="secrets.txt")
24
-    image = get_next_image(get_images(config.ALBUM_API), get_previous_id())
25
+    image = get_next_image(get_images(config.ALBUM_API), get_previous_creation_date())
25 26
     if image:
26 27
         if image['comment'] is None:
27 28
             image['comment'] = ""  # Better not mess up templates when no comment.
@@ -35,7 +36,7 @@ def toot_next_image():
35 36
             media_ids=[media],
36 37
             visibility=config.TOOT_VISIBILITY,
37 38
             language=config.TOOT_LANGUAGE)
38
-        set_previous_id(image['id'])
39
+        set_previous_creation_date(image['date_creation'])
39 40
 
40 41
 if __name__=='__main__':
41 42
     toot_next_image()

Завантаження…
Відмінити
Зберегти