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

fedicurses.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. def printPost(win, post, parser):
  8. win.addstr(post["account"]["acct"], curses.A_BOLD)
  9. win.addstr(':', curses.A_BOLD)
  10. newLine(win)
  11. parser.feed(post["content"])
  12. def printAtLevel(stdscr, col1, col2, posts, parser, level):
  13. col1.clear()
  14. col2.clear()
  15. col1.move(0, 0)
  16. col2.move(0, 0)
  17. printPost(col1, posts[level], parser)
  18. printPost(col1, posts[(level + 1) % len(posts)], parser)
  19. try:
  20. col2.addstr(str(posts[level]))
  21. except curses.error:
  22. pass
  23. stdscr.noutrefresh()
  24. col1.noutrefresh()
  25. col2.noutrefresh()
  26. curses.doupdate()
  27. class PostParser(HTMLParser):
  28. def __init__(self, ncwin, defncatt):
  29. HTMLParser.__init__(self)
  30. self.win = ncwin
  31. self.defncatt = defncatt
  32. self.curatt = defncatt
  33. def handle_starttag(self, tag, attrs):
  34. if tag == 'p':
  35. self.curatt = self.defncatt
  36. elif tag == 'strong':
  37. self.curatt = self.curatt ^ curses.A_BOLD
  38. elif tag == 'br':
  39. newLine(self.win)
  40. def handle_endtag(self, tag):
  41. if tag == 'p':
  42. newLine(self.win, 2)
  43. elif tag == 'strong':
  44. self.curatt = self.curatt ^ curses.A_BOLD
  45. def handle_data(self, data):
  46. try:
  47. self.win.addstr(data, self.curatt)
  48. except curses.error:
  49. pass
  50. # print(mastodon.timeline()[0]["content"])
  51. def main(stdscr):
  52. mastodon = Mastodon(
  53. access_token = 'd100.club_usercred.secret',
  54. api_base_url = 'd100.club'
  55. )
  56. posts = mastodon.timeline()
  57. stdscr.clear()
  58. # stdscr.addstr("test")
  59. curses.halfdelay(10)
  60. my, mx = stdscr.getmaxyx()
  61. c1w = c2w = (mx - 3) // 2
  62. if (mx % 2) != 0:
  63. c2w = c2w - 1
  64. colw1 = curses.newwin(my - 2, c1w, 1, 1)
  65. colw2 = curses.newwin(my - 2, c2w, 1, 1 + c1w + 2)
  66. stdscr.border()
  67. # colw1.border()
  68. # colw2.border()
  69. parser = PostParser(colw1, 0)
  70. # colw1.move(0,0)
  71. # stdscr.addstr(0, 0, 'test')
  72. # colw2.addstr(0, 0, 'test2')
  73. # colw1.addstr(0, 0, 'test3')
  74. i = 'c'
  75. q = 0
  76. printAtLevel(stdscr, colw1, colw2, posts, parser, q)
  77. con = True
  78. while con:
  79. i = stdscr.getch()
  80. if (i == ord('q')):
  81. con = False
  82. elif (i == ord('j')):
  83. q = (q + 1) % len(posts)
  84. printAtLevel(stdscr, colw1, colw2, posts, parser, q)
  85. elif (i == ord('k')):
  86. q = (q - 1) % len(posts)
  87. printAtLevel(stdscr, colw1, colw2, posts, parser, q)
  88. curses.wrapper(main)