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

fedicurses.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import curses
  2. from html.parser import HTMLParser
  3. from mastodon import Mastodon
  4. def newLine(ncwin, lines=1):
  5. cy, cx = ncwin.getyx()
  6. ncwin.move(cy + lines, 0)
  7. class PostParser(HTMLParser):
  8. def __init__(self, ncwin, defncatt):
  9. HTMLParser.__init__(self)
  10. self.win = ncwin
  11. self.defncatt = defncatt
  12. self.curatt = defncatt
  13. def handle_starttag(self, tag, attrs):
  14. if tag == 'p':
  15. self.curatt = self.defncatt
  16. elif tag == 'strong':
  17. self.curatt = self.curatt ^ curses.A_BOLD
  18. elif tag == 'br':
  19. newLine(self.win)
  20. def handle_endtag(self, tag):
  21. if tag == 'p':
  22. newLine(self.win, 2)
  23. elif tag == 'strong':
  24. self.curatt = self.curatt ^ curses.A_BOLD
  25. def handle_data(self, data):
  26. self.win.addstr(data, self.curatt)
  27. # print(mastodon.timeline()[0]["content"])
  28. def main(stdscr):
  29. mastodon = Mastodon(
  30. access_token = 'd100.club_usercred.secret',
  31. api_base_url = 'd100.club'
  32. )
  33. posts = mastodon.timeline()
  34. stdscr.clear()
  35. # stdscr.addstr("test")
  36. curses.halfdelay(10)
  37. my, mx = stdscr.getmaxyx()
  38. c1w = c2w = (mx - 1) // 2
  39. if (mx % 2) != 0:
  40. c2w = c2w - 1
  41. colw1 = curses.newwin(1, 1, my - 1, c1w)
  42. colw2 = curses.newwin(1, 1 + c1w + 1, my - 1, c2w)
  43. #colw1.border()
  44. #colw2.border()
  45. parser = PostParser(colw1, 0)
  46. colw1.move(0,0)
  47. try:
  48. for post in posts:
  49. colw1.addstr(post["account"]["acct"], curses.A_BOLD)
  50. colw1.addstr(':', curses.A_BOLD)
  51. newLine(colw1)
  52. parser.feed(post["content"])
  53. except:
  54. pass
  55. colw1.refresh()
  56. colw2.refresh()
  57. stdscr.refresh()
  58. i = 'c'
  59. q = 0
  60. con = True
  61. while con:
  62. i = stdscr.getch()
  63. con = (i != ord('q'))
  64. curses.wrapper(main)