Parcourir la source

Initial commit

master
The Dod il y a 2 jours
révision
7c8622846b
9 fichiers modifiés avec 10511 ajouts et 0 suppressions
  1. 3
    0
      .gitignore
  2. 3
    0
      README.md
  3. 39
    0
      bloomscroller.py
  4. 12
    0
      config.py
  5. 5216
    0
      example.json
  6. 5216
    0
      example2.json
  7. 1
    0
      previous_id.txt
  8. 19
    0
      register.py
  9. 2
    0
      requirements.txt

+ 3
- 0
.gitignore Voir le fichier

@@ -0,0 +1,3 @@
1
+secrets.txt
2
+venv
3
+__pycache__

+ 3
- 0
README.md Voir le fichier

@@ -0,0 +1,3 @@
1
+Bloomscroller is a [Mastodon](https://joinmastodon.org) bot that is run as a cron job in order to drip-toot a [Piwigo](https://piwigo.org) gallery one image at a time.
2
+
3
+_to be cotinued_

+ 39
- 0
bloomscroller.py Voir le fichier

@@ -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()

+ 12
- 0
config.py Voir le fichier

@@ -0,0 +1,12 @@
1
+APP_NAME = "Bloomscroller"
2
+APP_WEBSITE = "https://nimrodkerrett.opalstacked.com/nimrodkerrett/bloomscroller"
3
+APP_DESCRIPTION = "Tooting a Piwigo album as a cron job"
4
+APP_INSTANCE_URL = "https://tooot.im"
5
+
6
+ALBUM_API = "https://photos.nandn.org.il/piwigo/ws.php?format=json&method=pwg.categories.getImages&cat_id=21&per_page=529"
7
+
8
+TOOT_TEMPLATE = "#Bloomscrolling"
9
+ALT_TEXT_TEMPLATE = "Flowers from n&n's garden arranged in a vase.\nPhotographed at {date_creation}."
10
+TOOT_LANGUAGE = "en"
11
+TOOT_VISIBILITY = "public"
12
+MEDIA_SIZE = "4xlarge"

+ 5216
- 0
example.json
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 5216
- 0
example2.json
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 1
- 0
previous_id.txt Voir le fichier

@@ -0,0 +1 @@
1
+294

+ 19
- 0
register.py Voir le fichier

@@ -0,0 +1,19 @@
1
+from mastodon import Mastodon
2
+import config
3
+
4
+def register_app():
5
+    Mastodon.create_app(
6
+        config.APP_NAME,
7
+        website=config.APP_WEBSITE,
8
+        api_base_url=config.APP_INSTANCE_URL,
9
+        to_file='secrets.txt')
10
+    app = Mastodon(client_id='secrets.txt')
11
+    print("Browse to the OAuth authorization page at")
12
+    print(app.auth_request_url())
13
+    app.log_in(
14
+        code=input("and paste here the OAuth authorization code: "),
15
+        to_file='secrets.txt')
16
+    print("Registration complete")
17
+
18
+if __name__=='__main__':
19
+    register_app()

+ 2
- 0
requirements.txt Voir le fichier

@@ -0,0 +1,2 @@
1
+requests
2
+mastodon.py

Chargement…
Annuler
Enregistrer