commit b508e7d3feab25b4177d2ea08def9f0d51875a69 Author: Annika Backstrom Date: Sat Aug 31 23:55:41 2019 +0100 Intial version of toot-reader.py diff --git a/fonts/elec.ttf b/fonts/elec.ttf new file mode 100755 index 0000000..16c1059 Binary files /dev/null and b/fonts/elec.ttf differ diff --git a/fonts/elec.txt b/fonts/elec.txt new file mode 100644 index 0000000..12b8545 --- /dev/null +++ b/fonts/elec.txt @@ -0,0 +1,2 @@ +Electronic Highway Sign by Ash Pikachu Font +https://www.dafont.com/electronic-highway-sign.font diff --git a/toot-reader.cfg.default b/toot-reader.cfg.default new file mode 100644 index 0000000..dd062b4 --- /dev/null +++ b/toot-reader.cfg.default @@ -0,0 +1,3 @@ +[server] +token=YOUR_TOKEN_GOES_HERE +host=mastodon.example.com diff --git a/toot-reader.py b/toot-reader.py new file mode 100644 index 0000000..f7b9820 --- /dev/null +++ b/toot-reader.py @@ -0,0 +1,113 @@ +import time + +from inky import InkyPHAT +from PIL import Image, ImageFont, ImageDraw +from mastodon import Mastodon +from HTMLParser import HTMLParser +from UserDict import UserDict + +from ConfigParser import RawConfigParser +config = RawConfigParser() +config.read('toot-reader.cfg') + +mastodon = Mastodon( + access_token=config.get('server', 'token'), + api_base_url='https://' + config.get('server', 'host') +) + +class MLStripper(HTMLParser): + def __init__(self): + self.reset() + self.fed = [] + def handle_starttag(self, tag, attrs): + if tag == 'br': + self.fed.append(" ") + def handle_endtag(self, tag): + if tag == 'p': + self.fed.append(" ") + def handle_data(self, d): + self.fed.append(d) + def get_data(self): + print self.fed + return ''.join(self.fed) + +class Toot: + def __init__(self, toot): + self.toot = toot + self.html = MLStripper() + + def title(self): + return self.toot['reblog']['account']['acct'] if self.toot['reblog'] else self.toot['account']['acct'] + + def content(self): + self.html.feed(self.toot['content']) + content = self.html.get_data().strip().split(' ') + self.html.reset() + + return content + +class InkyDraw: + def __init__(self): + self.inky = InkyPHAT("black") + self.font = ImageFont.truetype('fonts/elec.ttf', 10) + + self.reset() + + def reset(self): + self.y = 0 + self.image = Image.new("P", (self.inky.WIDTH, self.inky.HEIGHT)) + self.draw = ImageDraw.Draw(self.image) + + def title(self, title): + w, h = self.font.getsize(title) + x = (self.inky.WIDTH / 2) - (w / 2) + self.draw.rectangle([(0, self.y), (self.inky.WIDTH, h)], self.inky.BLACK); + self.draw.text((x, 0), title, self.inky.WHITE, self.font) + + self.y = h + 2 + + def body(self, body): + lines = [[]] + max_h = 0 + for word in body: + lines[-1].append(word) + w, h = self.font.getsize(' '.join(lines[-1])) + max_h = h if h > max_h else max_h + if w > self.inky.WIDTH: + lines[-1].pop() + lines.append([]) + lines[-1].append(word) + + lines = [" ".join(line) for line in lines] + for line in lines: + self.draw.text((0, self.y), line, self.inky.BLACK, self.font) + self.y += max_h + + def finish(self): + # I happen to want this upside-down + self.image = self.image.transpose(Image.ROTATE_180) + self.inky.set_image(self.image) + self.inky.show() + self.reset() + +def update(draw, last_id): + toot = mastodon.timeline(limit=1)[0] + toot_id = toot['id'] + + if itoot_id == last_id: + return toot_id + + toot = Toot(toot) + + draw.title(toot.title()) + draw.body(toot.content()) + draw.finish() + + return toot_id + +if __name__ == '__main__': + draw = InkyDraw() + last_id = 0 + while True: + last_id = update(draw, last_id) + time.sleep(30)