Add initial xoxo.py script
This commit is contained in:
parent
25f5de73cf
commit
fe2409b03f
67
xoxo.py
Normal file
67
xoxo.py
Normal file
@ -0,0 +1,67 @@
|
||||
import netifaces
|
||||
import requests
|
||||
import time
|
||||
|
||||
from inky import InkyPHAT
|
||||
from mastodon import Mastodon
|
||||
from PIL import Image, ImageFont, ImageDraw
|
||||
from font_fredoka_one import FredokaOne
|
||||
|
||||
stats = last_stats = {}
|
||||
|
||||
class InkyDraw:
|
||||
users_pos = (46, 64)
|
||||
statuses_pos = (46, 105)
|
||||
hashtags_pos = (46, 146)
|
||||
|
||||
def __init__(self):
|
||||
self.users = 0
|
||||
self.statuses = 0
|
||||
self.hashtags = 0
|
||||
|
||||
self.inky = InkyPHAT("black")
|
||||
self.inky.set_border(self.inky.BLACK)
|
||||
self.font = ImageFont.truetype(FredokaOne, 20)
|
||||
|
||||
def ip(self):
|
||||
wlan0 = netifaces.ifaddresses('wlan0')
|
||||
ips = "\n".join([addr['addr'] for addr in wlan0[netifaces.AF_INET]])
|
||||
|
||||
self.image = Image.new("P", (self.inky.WIDTH, self.inky.HEIGHT))
|
||||
self.draw = ImageDraw.Draw(self.image)
|
||||
self.draw.text((0, 0), ips, self.inky.BLACK, font=self.font)
|
||||
self.inky.set_image(self.image)
|
||||
self.inky.show()
|
||||
|
||||
def update(self, stats):
|
||||
self.users = stats['new_users']
|
||||
self.statuses = stats['new_statuses']
|
||||
|
||||
def write(self):
|
||||
self.image = Image.open("graphics/xoxo.png")
|
||||
self.draw = ImageDraw.Draw(self.image)
|
||||
|
||||
self.draw.text(self.users_pos, str(self.users), self.inky.BLACK, font=self.font)
|
||||
self.draw.text(self.statuses_pos, str(self.statuses), self.inky.BLACK, font=self.font)
|
||||
self.draw.text(self.hashtags_pos, str(self.hashtags), self.inky.BLACK, font=self.font)
|
||||
|
||||
self.image = self.image.transpose(Image.ROTATE_90)
|
||||
self.inky.set_image(self.image)
|
||||
self.inky.show()
|
||||
|
||||
if __name__ == '__main__':
|
||||
draw = InkyDraw()
|
||||
|
||||
draw.ip()
|
||||
time.sleep(10)
|
||||
|
||||
last_id = 0
|
||||
while True:
|
||||
result = requests.get("https://xoxo-zone-stats.glitch.me/").json()
|
||||
if result['stats'] != last_stats:
|
||||
draw.update(result['stats'])
|
||||
draw.write()
|
||||
last_stats = result['stats']
|
||||
time.sleep(30)
|
||||
|
||||
# vim:ts=2 sw=2 et:
|
Loading…
Reference in New Issue
Block a user