A Mastodon bot to drip-toot a Piwigo gallery one image at a time.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bloomscroller.py 1.5KB

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