Browse Source

Use `date_creation` instead of `id` for sorting

master
The Dod 5 days ago
parent
commit
a13020cdc5
2 changed files with 12 additions and 11 deletions
  1. 1
    1
      .gitignore
  2. 11
    10
      bloomscroller.py

+ 1
- 1
.gitignore View File

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

+ 11
- 10
bloomscroller.py View File

2
 import requests
2
 import requests
3
 from mastodon import Mastodon
3
 from mastodon import Mastodon
4
 import config
4
 import config
5
+import sys
5
 
6
 
6
 def get_images(album_api):
7
 def get_images(album_api):
7
     return requests.get(album_api).json()['result']['images']
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
    try: 
15
    try: 
15
-       return int(json.load(open("previous_id.txt")))
16
+       return json.load(open("previous_creation_date.txt"))
16
    except Exception:
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
 def toot_next_image():
23
 def toot_next_image():
23
     mastodon = Mastodon(access_token="secrets.txt")
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
     if image:
26
     if image:
26
         if image['comment'] is None:
27
         if image['comment'] is None:
27
             image['comment'] = ""  # Better not mess up templates when no comment.
28
             image['comment'] = ""  # Better not mess up templates when no comment.
35
             media_ids=[media],
36
             media_ids=[media],
36
             visibility=config.TOOT_VISIBILITY,
37
             visibility=config.TOOT_VISIBILITY,
37
             language=config.TOOT_LANGUAGE)
38
             language=config.TOOT_LANGUAGE)
38
-        set_previous_id(image['id'])
39
+        set_previous_creation_date(image['date_creation'])
39
 
40
 
40
 if __name__=='__main__':
41
 if __name__=='__main__':
41
     toot_next_image()
42
     toot_next_image()

Loading…
Cancel
Save