A Mastodon bot to drip-toot a Piwigo gallery one image at a time.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. if image['comment'] is None:
  22. image['comment'] = "" # Better not mess up templates when no comment.
  23. media = mastodon.media_post(
  24. requests.get(
  25. image["derivatives"][config.MEDIA_SIZE]["url"]).content,
  26. mime_type="image/jpeg",
  27. description = config.ALT_TEXT_TEMPLATE.format(**image))
  28. mastodon.status_post(
  29. config.TOOT_TEMPLATE.format(**image),
  30. media_ids=[media],
  31. visibility=config.TOOT_VISIBILITY,
  32. language=config.TOOT_LANGUAGE)
  33. set_previous_id(image['id'])
  34. if __name__=='__main__':
  35. toot_next_image()