Browse Source

Image handling

Petra Lamborn 4 years ago
parent
commit
4d54f834e1
1 changed files with 30 additions and 4 deletions
  1. 30
    4
      fedicurses.py

+ 30
- 4
fedicurses.py View File

1
+#! /usr/bin/python3
1
 import curses
2
 import curses
2
 from html.parser import HTMLParser
3
 from html.parser import HTMLParser
3
 from mastodon import Mastodon
4
 from mastodon import Mastodon
6
                        # 0: White on black
7
                        # 0: White on black
7
     curses.init_pair(1, 4, 0) # 1: Blue on black
8
     curses.init_pair(1, 4, 0) # 1: Blue on black
8
     curses.init_pair(2, 1, 0) # 2: Red on black
9
     curses.init_pair(2, 1, 0) # 2: Red on black
9
-    curses.init_pair(3, 6, 0) # 2: Cyan on black
10
+    curses.init_pair(3, 6, 0) # 3: Cyan on black
11
+    curses.init_pair(4, 2, 0) # 4: Green on black
10
 
12
 
11
 def newLine(ncwin, lines=1):
13
 def newLine(ncwin, lines=1):
12
     cy, cx = ncwin.getyx()
14
     cy, cx = ncwin.getyx()
56
 
58
 
57
 def printPost(win, post, parser):
59
 def printPost(win, post, parser):
58
     # win.addstr(post["account"]["acct"], curses.A_BOLD)
60
     # win.addstr(post["account"]["acct"], curses.A_BOLD)
59
-    typeset(post["account"]["acct"], win, curses.A_BOLD, 0)
61
+    typeset("@{}".format(post["account"]["acct"]), win, curses.A_BOLD, 0)
60
     if (post["reblog"] is not None):
62
     if (post["reblog"] is not None):
61
         # win.addstr(" boosted ")
63
         # win.addstr(" boosted ")
62
         typeset(" boosted ", win, 0, 0)
64
         typeset(" boosted ", win, 0, 0)
63
         # win.addstr(post["reblog"]["account"]["acct"], curses.A_BOLD)
65
         # win.addstr(post["reblog"]["account"]["acct"], curses.A_BOLD)
64
-        typeset(post["reblog"]["account"]["acct"], win, curses.A_BOLD, 0)
66
+        typeset("@{}".format(post["reblog"]["account"]["acct"]), win, 
67
+                curses.A_BOLD, 0)
65
     # win.addstr(":")
68
     # win.addstr(":")
66
     typeset(":", win, 0, 0)
69
     typeset(":", win, 0, 0)
67
     newLine(win)
70
     newLine(win)
73
     if parser.openp:
76
     if parser.openp:
74
         newLine(win)
77
         newLine(win)
75
 
78
 
79
+    for att in post["media_attachments"]:
80
+        # typeset("[IMG: {}, <{}>]".format(att["alt"], att["src"]))
81
+        typeset("[IMG: {}]".format(att["description"] if
82
+                                         att["description"] is not None
83
+                                         else "(Alt text missing)"),
84
+               win, 0, 4)
85
+        newLine(win)
86
+
87
+    if (post["reblog"] is not None):
88
+        for att in post["reblog"]["media_attachments"]:
89
+            # typeset("[IMG: {}, <{}>]".format(att["alt"], att["src"]))
90
+            typeset("[IMG: {}]".format(att["description"] if
91
+                                             att["description"] is not None
92
+                                             else "(Alt text missing)"),
93
+                   win, 0, 4)
94
+            newLine(win)
95
+
76
     botstring = "{} @ {} UTC".format(post["visibility"],
96
     botstring = "{} @ {} UTC".format(post["visibility"],
77
                                      post["created_at"
97
                                      post["created_at"
78
                                          ].strftime(
98
                                          ].strftime(
94
     newLine(col1)
114
     newLine(col1)
95
     printPost(col1, posts[(level + 1) % len(posts)], parser)
115
     printPost(col1, posts[(level + 1) % len(posts)], parser)
96
     try:
116
     try:
97
-        col2.addstr(str(posts[level]))
117
+        col2.addstr(str(posts[level]["content"]))
118
+        col2.addstr(str(posts[level]["media_attachments"]))
119
+        col2.addstr(str(posts[level].keys()))
98
     except curses.error:
120
     except curses.error:
99
         pass
121
         pass
100
     stdscr.noutrefresh()
122
     stdscr.noutrefresh()
130
         elif tag == 'code':
152
         elif tag == 'code':
131
             self.colstack.append(self.colour)
153
             self.colstack.append(self.colour)
132
             self.colour = 3
154
             self.colour = 3
155
+        elif tag == 'img':
156
+            typeset("[IMG: {}, <{}>]".format(att["alt"], att["src"]), self.win,
157
+                   0, 4)
158
+
133
 
159
 
134
     def handle_endtag(self, tag):
160
     def handle_endtag(self, tag):
135
         if tag == 'p':
161
         if tag == 'p':