indy.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from mastodon import Mastodon
  2. from datetime import datetime
  3. import requests
  4. import pause
  5. import json
  6. data = json.load(open('sorted.json'))
  7. # Set up Mastodon
  8. mastodon = Mastodon(
  9. api_base_url = 'https://mastodon.cisti.org',
  10. access_token = 'token.secret'
  11. )
  12. for post in data:
  13. # add 20 years
  14. dt = datetime.fromisoformat(post['created'])
  15. dt = dt.replace(year=2021)
  16. # skip past post
  17. if dt < datetime.now():
  18. print("Skip ", post['heading'], dt)
  19. continue
  20. # wait for the next post
  21. print("Next post at: ", dt, post['heading'])
  22. pause.until(dt)
  23. # FINAL URL
  24. url = "https://italy.indymedia.org/posts/"+post['hugo']
  25. out = post['created'] +"\n\n"+post['heading']+"\n\n"
  26. # CHECK MEDIA LINK
  27. media_id = False
  28. if post['linked_file']:
  29. # MEDIA LINK URL
  30. linkurl = "https://italy.indymedia.org/uploads/"
  31. # IMAGES ONLY
  32. if post['linked_file'].lower().find('jpg') >= 0 or \
  33. post['linked_file'].lower().find('jpeg') >= 0 or \
  34. post['linked_file'].lower().find('gif') >= 0:
  35. response = requests.get("https://italy.indymedia.org/uploads/" + post['linked_file'])
  36. with open('./tmp', 'wb') as f:
  37. f.write(response.content)
  38. media_id = mastodon.media_post('./tmp')
  39. media_ids = [media_id] if media_id else []
  40. out += 'LEGGI IL POST: '+url
  41. print(out)
  42. mastodon.status_post(out, visibility='private', media_ids=media_ids)