Commit | Line | Data |
---|---|---|
7500d90b VNM |
1 | """ |
2 | Draw | |
3 | """ | |
4 | import requests | |
5 | import datetime | |
6 | import time | |
7 | ||
2da50cc4 | 8 | from twitter.util import printNicely |
7500d90b VNM |
9 | from StringIO import StringIO |
10 | from dateutil import parser | |
2da50cc4 | 11 | from .c_image import * |
7500d90b VNM |
12 | from .colors import * |
13 | from .config import * | |
14 | from .db import * | |
15 | ||
16 | db = RainbowDB() | |
17 | ||
18 | def draw(t, iot=False, keyword=None, fil=[], ig=[]): | |
19 | """ | |
20 | Draw the rainbow | |
21 | """ | |
22 | ||
23 | # Retrieve tweet | |
24 | tid = t['id'] | |
25 | text = t['text'] | |
26 | screen_name = t['user']['screen_name'] | |
27 | name = t['user']['name'] | |
28 | created_at = t['created_at'] | |
29 | favorited = t['favorited'] | |
30 | date = parser.parse(created_at) | |
31 | date = date - datetime.timedelta(seconds=time.timezone) | |
32 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
33 | ||
34 | # Get expanded url | |
35 | try: | |
36 | expanded_url = [] | |
37 | url = [] | |
38 | urls = t['entities']['urls'] | |
39 | for u in urls: | |
40 | expanded_url.append(u['expanded_url']) | |
41 | url.append(u['url']) | |
42 | except: | |
43 | expanded_url = None | |
44 | url = None | |
45 | ||
46 | # Get media | |
47 | try: | |
48 | media_url = [] | |
49 | media = t['entities']['media'] | |
50 | for m in media: | |
51 | media_url.append(m['media_url']) | |
52 | except: | |
53 | media_url = None | |
54 | ||
55 | # Filter and ignore | |
56 | screen_name = '@' + screen_name | |
57 | if fil and screen_name not in fil: | |
58 | return | |
59 | if ig and screen_name in ig: | |
60 | return | |
61 | ||
62 | # Get rainbow id | |
63 | res = db.tweet_to_rainbow_query(tid) | |
64 | if not res: | |
65 | db.tweet_store(tid) | |
66 | res = db.tweet_to_rainbow_query(tid) | |
67 | rid = res[0].rainbow_id | |
68 | ||
69 | # Format info | |
70 | user = cycle_color(name) + grey(' ' + screen_name + ' ') | |
71 | meta = grey('[' + clock + '] [id=' + str(rid) + '] ') | |
72 | if favorited: | |
533edcdb | 73 | meta = meta + light_green(u'\u2605') |
7500d90b VNM |
74 | tweet = text.split() |
75 | # Replace url | |
76 | if expanded_url: | |
77 | for index in range(len(expanded_url)): | |
78 | tweet = map( | |
79 | lambda x: expanded_url[index] if x == url[index] else x, | |
80 | tweet) | |
81 | # Highlight RT | |
82 | tweet = map(lambda x: grey(x) if x == 'RT' else x, tweet) | |
83 | # Highlight screen_name | |
84 | tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) | |
85 | # Highlight link | |
533edcdb | 86 | tweet = map(lambda x: light_cyan(x) if x[0:4] == 'http' else x, tweet) |
7500d90b VNM |
87 | # Highlight search keyword |
88 | if keyword: | |
89 | tweet = map( | |
533edcdb | 90 | lambda x: on_light_yellow(x) if |
7500d90b VNM |
91 | ''.join(c for c in x if c.isalnum()).lower() == keyword.lower() |
92 | else x, | |
93 | tweet | |
94 | ) | |
95 | # Recreate tweet | |
96 | tweet = ' '.join(tweet) | |
97 | ||
98 | # Draw rainbow | |
99 | line1 = u"{u:>{uw}}:".format( | |
100 | u=user, | |
101 | uw=len(user) + 2, | |
102 | ) | |
103 | line2 = u"{c:>{cw}}".format( | |
104 | c=meta, | |
105 | cw=len(meta) + 2, | |
106 | ) | |
107 | line3 = ' ' + tweet | |
108 | ||
109 | printNicely('') | |
110 | printNicely(line1) | |
111 | printNicely(line2) | |
112 | printNicely(line3) | |
113 | ||
114 | # Display Image | |
115 | if iot and media_url: | |
116 | for mu in media_url: | |
117 | response = requests.get(mu) | |
118 | image_to_display(StringIO(response.content)) | |
119 | ||
120 | ||
121 | def print_message(m): | |
122 | """ | |
123 | Print direct message | |
124 | """ | |
125 | sender_screen_name = '@' + m['sender_screen_name'] | |
126 | sender_name = m['sender']['name'] | |
127 | text = m['text'] | |
128 | recipient_screen_name = '@' + m['recipient_screen_name'] | |
129 | recipient_name = m['recipient']['name'] | |
130 | mid = m['id'] | |
131 | date = parser.parse(m['created_at']) | |
132 | date = date - datetime.timedelta(seconds=time.timezone) | |
133 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
134 | ||
135 | # Get rainbow id | |
136 | res = db.message_to_rainbow_query(mid) | |
137 | if not res: | |
138 | db.message_store(mid) | |
139 | res = db.message_to_rainbow_query(mid) | |
140 | rid = res[0].rainbow_id | |
141 | ||
142 | sender = cycle_color(sender_name) + grey(' ' + sender_screen_name + ' ') | |
143 | recipient = cycle_color( | |
144 | recipient_name) + grey(' ' + recipient_screen_name + ' ') | |
533edcdb | 145 | user = sender + light_magenta(' >>> ') + recipient |
7500d90b VNM |
146 | meta = grey('[' + clock + '] [message_id=' + str(rid) + '] ') |
147 | text = ''.join(map(lambda x: x + ' ' if x == '\n' else x, text)) | |
148 | ||
149 | line1 = u"{u:>{uw}}:".format( | |
150 | u=user, | |
151 | uw=len(user) + 2, | |
152 | ) | |
153 | line2 = u"{c:>{cw}}".format( | |
154 | c=meta, | |
155 | cw=len(meta) + 2, | |
156 | ) | |
157 | ||
158 | line3 = ' ' + text | |
159 | ||
160 | printNicely('') | |
161 | printNicely(line1) | |
162 | printNicely(line2) | |
163 | printNicely(line3) | |
164 | ||
165 | ||
166 | def show_profile(u, iot=False): | |
167 | """ | |
168 | Show a profile | |
169 | """ | |
170 | # Retrieve info | |
171 | name = u['name'] | |
172 | screen_name = u['screen_name'] | |
173 | description = u['description'] | |
174 | profile_image_url = u['profile_image_url'] | |
175 | location = u['location'] | |
176 | url = u['url'] | |
177 | created_at = u['created_at'] | |
178 | statuses_count = u['statuses_count'] | |
179 | friends_count = u['friends_count'] | |
180 | followers_count = u['followers_count'] | |
181 | # Create content | |
533edcdb VNM |
182 | statuses_count = light_green(str(statuses_count) + ' tweets') |
183 | friends_count = light_green(str(friends_count) + ' following') | |
184 | followers_count = light_green(str(followers_count) + ' followers') | |
7500d90b VNM |
185 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
186 | user = cycle_color(name) + grey(' @' + screen_name + ' : ') + count | |
533edcdb | 187 | profile_image_raw_url = 'Profile photo: ' + light_cyan(profile_image_url) |
7500d90b VNM |
188 | description = ''.join( |
189 | map(lambda x: x + ' ' * 4 if x == '\n' else x, description)) | |
533edcdb VNM |
190 | description = light_yellow(description) |
191 | location = 'Location : ' + light_magenta(location) | |
192 | url = 'URL : ' + (light_cyan(url) if url else '') | |
7500d90b VNM |
193 | date = parser.parse(created_at) |
194 | date = date - datetime.timedelta(seconds=time.timezone) | |
195 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
196 | clock = 'Join at ' + white(clock) | |
197 | # Format | |
198 | line1 = u"{u:>{uw}}".format( | |
199 | u=user, | |
200 | uw=len(user) + 2, | |
201 | ) | |
202 | line2 = u"{p:>{pw}}".format( | |
203 | p=profile_image_raw_url, | |
204 | pw=len(profile_image_raw_url) + 4, | |
205 | ) | |
206 | line3 = u"{d:>{dw}}".format( | |
207 | d=description, | |
208 | dw=len(description) + 4, | |
209 | ) | |
210 | line4 = u"{l:>{lw}}".format( | |
211 | l=location, | |
212 | lw=len(location) + 4, | |
213 | ) | |
214 | line5 = u"{u:>{uw}}".format( | |
215 | u=url, | |
216 | uw=len(url) + 4, | |
217 | ) | |
218 | line6 = u"{c:>{cw}}".format( | |
219 | c=clock, | |
220 | cw=len(clock) + 4, | |
221 | ) | |
222 | # Display | |
223 | printNicely('') | |
224 | printNicely(line1) | |
225 | if iot: | |
226 | response = requests.get(profile_image_url) | |
227 | image_to_display(StringIO(response.content), 2, 20) | |
228 | else: | |
229 | printNicely(line2) | |
230 | for line in [line3, line4, line5, line6]: | |
231 | printNicely(line) | |
232 | printNicely('') | |
233 | ||
234 | ||
235 | def print_trends(trends): | |
236 | """ | |
237 | Display topics | |
238 | """ | |
239 | for topic in trends[:TREND_MAX]: | |
240 | name = topic['name'] | |
241 | url = topic['url'] | |
533edcdb | 242 | line = cycle_color(name) + ': ' + light_cyan(url) |
7500d90b VNM |
243 | printNicely(line) |
244 | printNicely('') | |
245 |