indybot/indy.py
2021-07-14 10:16:42 +02:00

51 lines
No EOL
1.4 KiB
Python

from mastodon import Mastodon
from datetime import datetime
import requests
import pause
import json
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
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, visibility='private', media_ids=media_ids)