A Mastodon bot to drip-toot a Piwigo gallery one image at a time.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bloomscroller.py 1.2KB

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