Mine’s just 54 lines right now. Executed daily as a cron job. Wrote in some simple checks that enable it to account for leap days, or being behind. It can be made to catch up simply by having it run more than once a day.
I should make it cache the login, too.
It can be made to post any comic on gocomics, daily, in sync, simply by editing a config toml.
comciposter.py
import datetime
import os
import requests
import pickle
import toml
from pythorhead import Lemmy
from PIL import Image
from io import BytesIO
# Config
config = toml.load(open(os.path.curdir+'/config.toml', 'r'))
try:
botstate = pickle.load(open(os.path.join(os.path.curdir, 'botstate.data'), 'rb'))
except Exception:
botstate = {}
botstate['startdate'] = datetime.datetime.strptime(config['startdate']+' 12:00:00', '%d/%m/%Y %H:%M:%S')
botstate['lastrun'] = datetime.datetime.now() - datetime.timedelta(days=1)
botstate['lastpostdate'] = botstate['startdate'] - datetime.timedelta(days=1)
withopen(os.path.join(os.path.curdir, 'botstate.data'), 'wb') as handle:
pickle.dump(botstate, handle)
today = datetime.datetime.today()
if datetime.datetime.now() - botstate['lastrun'] < datetime.timedelta(hours=10):
print('less than a day has passed since the last post, exiting')
exit(0)
elif botstate['lastpostdate'].month <= today.month and botstate['lastpostdate'].day < today.day:
postdate = botstate['lastpostdate'] + datetime.timedelta(days=1)
title = postdate.strftime('%d %B %Y')
url = 'https://www.gocomics.com/'+config['comic']+'/'+postdate.strftime('%Y/%m/%d')
r = requests.get(url, allow_redirects=True)
loc = int(r.text.find('https://assets.amuniversal.com/'))
imgurl = r.text[loc:loc+63]
img_response = requests.get(imgurl, allow_redirects=True)
with Image.open(BytesIO(img_response.content)) as image:
image.save(os.path.join(os.path.curdir, 'comic.webp'), 'WEBP')
lemmy = Lemmy('https://'+config['Lemmy']['instance'], request_timeout=30)
lemmy.log_in(config['Lemmy']['username'], config['Lemmy']['password'])
try:
image = lemmy.image.upload(os.path.join(os.path.curdir, 'comic.webp'))
except IOError as e:
print(e)
exit(1)
community_id = lemmy.discover_community(config['community'])
post = lemmy.post.create(community_id, title, url=image[0]["image_url"])
print('posted: '+post['post_view']['post']['ap_id'])
botstate['lastpostdate'] = postdate
botstate['lastrun'] = datetime.datetime.now()
withopen(os.path.join(os.path.curdir, 'botstate.data'), 'wb') as handle:
pickle.dump(botstate, handle)
Mine’s just 54 lines right now. Executed daily as a cron job. Wrote in some simple checks that enable it to account for leap days, or being behind. It can be made to catch up simply by having it run more than once a day.
I should make it cache the login, too.
It can be made to post any comic on gocomics, daily, in sync, simply by editing a config toml.
comciposter.py
import datetime import os import requests import pickle import toml from pythorhead import Lemmy from PIL import Image from io import BytesIO # Config config = toml.load(open(os.path.curdir+'/config.toml', 'r')) try: botstate = pickle.load(open(os.path.join(os.path.curdir, 'botstate.data'), 'rb')) except Exception: botstate = {} botstate['startdate'] = datetime.datetime.strptime(config['startdate']+' 12:00:00', '%d/%m/%Y %H:%M:%S') botstate['lastrun'] = datetime.datetime.now() - datetime.timedelta(days=1) botstate['lastpostdate'] = botstate['startdate'] - datetime.timedelta(days=1) with open(os.path.join(os.path.curdir, 'botstate.data'), 'wb') as handle: pickle.dump(botstate, handle) today = datetime.datetime.today() if datetime.datetime.now() - botstate['lastrun'] < datetime.timedelta(hours=10): print('less than a day has passed since the last post, exiting') exit(0) elif botstate['lastpostdate'].month <= today.month and botstate['lastpostdate'].day < today.day: postdate = botstate['lastpostdate'] + datetime.timedelta(days=1) title = postdate.strftime('%d %B %Y') url = 'https://www.gocomics.com/'+config['comic']+'/'+postdate.strftime('%Y/%m/%d') r = requests.get(url, allow_redirects=True) loc = int(r.text.find('https://assets.amuniversal.com/')) imgurl = r.text[loc:loc+63] img_response = requests.get(imgurl, allow_redirects=True) with Image.open(BytesIO(img_response.content)) as image: image.save(os.path.join(os.path.curdir, 'comic.webp'), 'WEBP') lemmy = Lemmy('https://'+config['Lemmy']['instance'], request_timeout=30) lemmy.log_in(config['Lemmy']['username'], config['Lemmy']['password']) try: image = lemmy.image.upload(os.path.join(os.path.curdir, 'comic.webp')) except IOError as e: print(e) exit(1) community_id = lemmy.discover_community(config['community']) post = lemmy.post.create(community_id, title, url=image[0]["image_url"]) print('posted: '+post['post_view']['post']['ap_id']) botstate['lastpostdate'] = postdate botstate['lastrun'] = datetime.datetime.now() with open(os.path.join(os.path.curdir, 'botstate.data'), 'wb') as handle: pickle.dump(botstate, handle)