You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8KB

  1. import netifaces
  2. import requests
  3. import time
  4. from inky import InkyPHAT
  5. from mastodon import Mastodon
  6. from PIL import Image, ImageFont, ImageDraw
  7. from font_fredoka_one import FredokaOne
  8. stats = last_stats = {}
  9. class InkyDraw:
  10. users_pos = (46, 64)
  11. statuses_pos = (46, 105)
  12. hashtags_pos = (46, 146)
  13. def __init__(self):
  14. self.users = 0
  15. self.statuses = 0
  16. self.hashtags = 0
  17. self.inky = InkyPHAT("black")
  18. self.inky.set_border(self.inky.BLACK)
  19. self.font = ImageFont.truetype(FredokaOne, 20)
  20. def ip(self):
  21. wlan0 = netifaces.ifaddresses('wlan0')
  22. ips = "\n".join([addr['addr'] for addr in wlan0[netifaces.AF_INET]])
  23. self.image = Image.new("P", (self.inky.WIDTH, self.inky.HEIGHT))
  24. self.draw = ImageDraw.Draw(self.image)
  25. self.draw.text((0, 0), ips, self.inky.BLACK, font=self.font)
  26. self.inky.set_image(self.image)
  27. self.inky.show()
  28. def update(self, stats):
  29. self.users = stats['new_users']
  30. self.statuses = stats['new_statuses']
  31. self.hashtags = stats['new_hashtags']
  32. def write(self):
  33. self.image = Image.open("graphics/xoxo.png")
  34. self.draw = ImageDraw.Draw(self.image)
  35. self.draw.text(self.users_pos, str(self.users), self.inky.BLACK, font=self.font)
  36. self.draw.text(self.statuses_pos, str(self.statuses), self.inky.BLACK, font=self.font)
  37. self.draw.text(self.hashtags_pos, str(self.hashtags), self.inky.BLACK, font=self.font)
  38. self.image = self.image.transpose(Image.ROTATE_90)
  39. self.inky.set_image(self.image)
  40. self.inky.show()
  41. if __name__ == '__main__':
  42. draw = InkyDraw()
  43. draw.ip()
  44. time.sleep(10)
  45. last_id = 0
  46. while True:
  47. result = requests.get("https://xoxo-zone-stats.glitch.me/").json()
  48. if result != last_stats:
  49. draw.update(result)
  50. draw.write()
  51. last_stats = result
  52. time.sleep(30)
  53. # vim:ts=2 sw=2 et: