diff --git a/mastodon-bot/Dockerfile b/mastodon-bot/Dockerfile new file mode 100644 index 0000000..500f8c0 --- /dev/null +++ b/mastodon-bot/Dockerfile @@ -0,0 +1,19 @@ +FROM alpine +LABEL \ + maintainer="Davide Alberani " + +RUN \ + apk add --no-cache \ + git python3 && \ + pip3 install Mastodon.py && \ + pip3 install markovify && \ + cd / && \ + git clone https://github.com/alberanid/onthisday.git && \ + cd onthisday && \ + python3 ./setup.py install + +COPY onthisday-mastodon-bot.py /usr/bin/ + +ENTRYPOINT ["python3", "/usr/bin/onthisday-mastodon-bot.py"] + + diff --git a/mastodon-bot/onthisday-mastodon-bot.py b/mastodon-bot/onthisday-mastodon-bot.py new file mode 100755 index 0000000..fdd0bf3 --- /dev/null +++ b/mastodon-bot/onthisday-mastodon-bot.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import sys +import subprocess +from mastodon import Mastodon + +API_URL = 'https://botsin.space/' + +sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1) + +def getEvents(): + cmd = ['python3', '-m', 'onthisday'] + if len(sys.argv) > 1: + cmd += sys.argv[1:] + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + try: + stdout = stdout.strip() + stdout = stdout.decode('utf8') + except: + return 'uh-oh: something was wrong with the encoding of the events; try again' + if process.returncode != 0: + return 'something terrible is happening: exit code: %s, stderr: %s' % ( + process.returncode, stderr.decode('utf8')) + if not stdout: + return 'sadness: the list of events is empty; try again' + return stdout + + +def serve(token): + events = getEvents() + print('On this day:\n\n%s' % events) + mastodon = Mastodon(access_token=token, api_base_url=API_URL) + mastodon.status_post(events) + + +if __name__ == '__main__': + if 'EVENTS_TOKEN' not in os.environ: + print("Please specify the Mastodon token in the EVENTS_TOKEN environment variable") + sys.exit(1) + serve(token=os.environ['EVENTS_TOKEN'])