Intial version of toot-reader.py
This commit is contained in:
commit
b508e7d3fe
BIN
fonts/elec.ttf
Executable file
BIN
fonts/elec.ttf
Executable file
Binary file not shown.
2
fonts/elec.txt
Normal file
2
fonts/elec.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Electronic Highway Sign by Ash Pikachu Font
|
||||||
|
https://www.dafont.com/electronic-highway-sign.font
|
3
toot-reader.cfg.default
Normal file
3
toot-reader.cfg.default
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[server]
|
||||||
|
token=YOUR_TOKEN_GOES_HERE
|
||||||
|
host=mastodon.example.com
|
113
toot-reader.py
Normal file
113
toot-reader.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user