WIP repository for a ncurses fediverse/mastodon client, using python mastodon.py

fedicurses.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import curses
  2. from html.parser import HTMLParser
  3. from mastodon import Mastodon
  4. class PostParser(HTMLParser):
  5. def __init__(self, ncwin, defncatt):
  6. HTMLParser.__init__(self)
  7. self.win = ncwin
  8. self.defncatt = defncatt
  9. self.curatt = defncatt
  10. def handle_starttag(self, tag, attrs):
  11. if tag == 'p':
  12. self.curatt = self.defncatt
  13. elif tag == 'strong':
  14. self.curatt = self.curatt ^ curses.A_BOLD
  15. def handle_endtag(self, tag):
  16. if tag == 'p':
  17. cy, cx = self.win.getyx()
  18. self.win.move(cy + 2, 0)
  19. elif tag == 'strong':
  20. self.curatt = self.curatt ^ curses.A_BOLD
  21. def handle_data(self, data):
  22. self.win.addstr(data, self.curatt)
  23. # print(mastodon.timeline()[0]["content"])
  24. def main(stdscr):
  25. mastodon = Mastodon(
  26. access_token = 'd100.club_usercred.secret',
  27. api_base_url = 'd100.club'
  28. )
  29. posts = mastodon.timeline()
  30. parser = PostParser(stdscr, 0)
  31. stdscr.clear()
  32. # stdscr.addstr("test")
  33. curses.halfdelay(10)
  34. try:
  35. for post in posts:
  36. stdscr.addstr(post["account"]["acct"])
  37. stdscr.addstr(": ")
  38. parser.feed(post["content"])
  39. except:
  40. pass
  41. stdscr.refresh()
  42. i = 'c'
  43. q = 0
  44. con = True
  45. while con:
  46. i = stdscr.getch()
  47. con = (i != ord('q'))
  48. curses.wrapper(main)