from mastodon import Mastodon from datetime import datetime import requests import pause import json import traceback data = json.load(open('sorted.json')) # Set up Mastodon mastodon = Mastodon( api_base_url = 'https://mastodon.cisti.org', access_token = 'token.secret' ) for post in data: # add 20 years dt = datetime.fromisoformat(post['created']) dt = dt.replace(year=2021) # skip past post if dt < datetime.now(): print("Skip ", post['heading'], dt) continue # wait for the next post print("Next post at: ", dt, post['heading']) pause.until(dt) # FINAL URL url = "https://italy.indymedia.org/posts/"+post['hugo'] out = post['created'] +"\n\n"+post['heading']+"\n\n" # CHECK MEDIA LINK try: media_id = False if post['linked_file']: # MEDIA LINK URL linkurl = "https://italy.indymedia.org/uploads/" # IMAGES ONLY if post['linked_file'].lower().find('jpg') >= 0 or \ post['linked_file'].lower().find('jpeg') >= 0 or \ post['linked_file'].lower().find('gif') >= 0: response = requests.get("https://italy.indymedia.org/uploads/" + post['linked_file']) with open('./tmp', 'wb') as f: f.write(response.content) media_id = mastodon.media_post('./tmp') media_ids = [media_id] if media_id else [] out += 'LEGGI IL POST: '+url print(out) mastodon.status_post(out, media_ids=media_ids) except Exception: traceback.print_exc()