Commit | Line | Data |
---|---|---|
e9f5200b VNM |
1 | import random |
2 | import itertools | |
7500d90b VNM |
3 | import requests |
4 | import datetime | |
5 | import time | |
6 | ||
2da50cc4 | 7 | from twitter.util import printNicely |
e9f5200b VNM |
8 | from functools import wraps |
9 | from pyfiglet import figlet_format | |
10 | from functools import reduce | |
7500d90b VNM |
11 | from StringIO import StringIO |
12 | from dateutil import parser | |
2da50cc4 | 13 | from .c_image import * |
7500d90b VNM |
14 | from .colors import * |
15 | from .config import * | |
16 | from .db import * | |
17 | ||
18 | db = RainbowDB() | |
1fdd6a5c VNM |
19 | g = {} |
20 | ||
e9f5200b VNM |
21 | def init_cycle(): |
22 | """ | |
23 | Init the cycle | |
24 | """ | |
25 | colors_shuffle = [globals()[i.encode('utf8')] | |
37ae740e | 26 | if not i.startswith('term_') |
8124f3a8 | 27 | else term_color(int(i[5:])) |
e9f5200b | 28 | for i in c['CYCLE_COLOR']] |
5f22104f | 29 | return itertools.cycle(colors_shuffle) |
30 | g['cyc'] = init_cycle() | |
e43ebfa6 | 31 | g['cache'] = {} |
1fdd6a5c | 32 | |
e9f5200b | 33 | |
e43ebfa6 | 34 | def reset_cycle(): |
2359c276 VNM |
35 | """ |
36 | Notify from rainbow | |
37 | """ | |
5f22104f | 38 | g['cyc'] = init_cycle() |
e43ebfa6 | 39 | g['cache'] = {} |
2359c276 VNM |
40 | |
41 | ||
e9f5200b VNM |
42 | def order_rainbow(s): |
43 | """ | |
44 | Print a string with ordered color with each character | |
45 | """ | |
5f22104f | 46 | colors_shuffle = [globals()[i.encode('utf8')] |
47 | if not i.startswith('term_') | |
48 | else term_color(int(i[5:])) | |
49 | for i in c['CYCLE_COLOR']] | |
50 | colored = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] | |
51 | return reduce(lambda x, y: x + y, colored) | |
e9f5200b VNM |
52 | |
53 | ||
54 | def random_rainbow(s): | |
55 | """ | |
56 | Print a string with random color with each character | |
57 | """ | |
5f22104f | 58 | colors_shuffle = [globals()[i.encode('utf8')] |
59 | if not i.startswith('term_') | |
60 | else term_color(int(i[5:])) | |
61 | for i in c['CYCLE_COLOR']] | |
62 | colored = [random.choice(colors_shuffle)(i) for i in s] | |
63 | return reduce(lambda x, y: x + y, colored) | |
e9f5200b VNM |
64 | |
65 | ||
66 | def Memoize(func): | |
67 | """ | |
68 | Memoize decorator | |
69 | """ | |
e9f5200b VNM |
70 | @wraps(func) |
71 | def wrapper(*args): | |
e43ebfa6 | 72 | if args not in g['cache']: |
73 | g['cache'][args] = func(*args) | |
74 | return g['cache'][args] | |
e9f5200b VNM |
75 | return wrapper |
76 | ||
77 | ||
78 | @Memoize | |
79 | def cycle_color(s): | |
80 | """ | |
81 | Cycle the colors_shuffle | |
82 | """ | |
1fdd6a5c | 83 | return next(g['cyc'])(s) |
e9f5200b VNM |
84 | |
85 | ||
86 | def ascii_art(text): | |
87 | """ | |
88 | Draw the Ascii Art | |
89 | """ | |
90 | fi = figlet_format(text, font='doom') | |
91 | print('\n'.join( | |
1fdd6a5c | 92 | [next(g['cyc'])(i) for i in fi.split('\n')] |
e9f5200b VNM |
93 | )) |
94 | ||
95 | ||
2a0cabee O |
96 | def show_calendar(month, date, rel): |
97 | """ | |
98 | Show the calendar in rainbow mode | |
99 | """ | |
100 | month = random_rainbow(month) | |
101 | date = ' '.join([cycle_color(i) for i in date.split(' ')]) | |
102 | today = str(int(os.popen('date +\'%d\'').read().strip())) | |
103 | # Display | |
104 | printNicely(month) | |
105 | printNicely(date) | |
106 | for line in rel: | |
107 | ary = line.split(' ') | |
108 | ary = map(lambda x: color_func(c['CAL']['today'])(x) | |
109 | if x == today | |
110 | else color_func(c['CAL']['days'])(x) | |
111 | , ary) | |
112 | printNicely(' '.join(ary)) | |
113 | ||
114 | ||
4cf86720 VNM |
115 | def check_theme(): |
116 | """ | |
117 | Check current theme and update if necessary | |
118 | """ | |
119 | exists = db.theme_query() | |
120 | themes = [t.theme_name for t in exists] | |
ddb1e615 VNM |
121 | if c['theme'] != themes[0]: |
122 | c['theme'] = themes[0] | |
4cf86720 | 123 | # Determine path |
ddb1e615 | 124 | if c['theme'] == 'custom': |
4cf86720 VNM |
125 | config = os.environ.get( |
126 | 'HOME', | |
9c7342ca | 127 | os.environ.get('USERPROFILE', |
4cf86720 VNM |
128 | '')) + os.sep + '.rainbow_config.json' |
129 | else: | |
15f3e155 | 130 | config = os.path.dirname(__file__) + '/colorset/'+c['theme']+'.json' |
4cf86720 VNM |
131 | # Load new config |
132 | data = load_config(config) | |
a5301bc0 VNM |
133 | if data: |
134 | for d in data: | |
135 | c[d] = data[d] | |
9c7342ca | 136 | # Re-init color cycle |
1fdd6a5c | 137 | g['cyc'] = init_cycle() |
7500d90b | 138 | |
fe08f905 VNM |
139 | |
140 | def color_func(func_name): | |
141 | """ | |
142 | Call color function base on name | |
143 | """ | |
144 | pure = func_name.encode('utf8') | |
8124f3a8 VNM |
145 | if pure.startswith('term_') and pure[5:].isdigit(): |
146 | return term_color(int(pure[5:])) | |
1face019 | 147 | return globals()[pure] |
fe08f905 VNM |
148 | |
149 | ||
7500d90b VNM |
150 | def draw(t, iot=False, keyword=None, fil=[], ig=[]): |
151 | """ | |
152 | Draw the rainbow | |
153 | """ | |
154 | ||
4cf86720 | 155 | check_theme() |
7500d90b VNM |
156 | # Retrieve tweet |
157 | tid = t['id'] | |
158 | text = t['text'] | |
159 | screen_name = t['user']['screen_name'] | |
160 | name = t['user']['name'] | |
161 | created_at = t['created_at'] | |
162 | favorited = t['favorited'] | |
163 | date = parser.parse(created_at) | |
164 | date = date - datetime.timedelta(seconds=time.timezone) | |
165 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
166 | ||
167 | # Get expanded url | |
168 | try: | |
169 | expanded_url = [] | |
170 | url = [] | |
171 | urls = t['entities']['urls'] | |
172 | for u in urls: | |
173 | expanded_url.append(u['expanded_url']) | |
174 | url.append(u['url']) | |
175 | except: | |
176 | expanded_url = None | |
177 | url = None | |
178 | ||
179 | # Get media | |
180 | try: | |
181 | media_url = [] | |
182 | media = t['entities']['media'] | |
183 | for m in media: | |
184 | media_url.append(m['media_url']) | |
185 | except: | |
186 | media_url = None | |
187 | ||
188 | # Filter and ignore | |
189 | screen_name = '@' + screen_name | |
190 | if fil and screen_name not in fil: | |
191 | return | |
192 | if ig and screen_name in ig: | |
193 | return | |
194 | ||
195 | # Get rainbow id | |
196 | res = db.tweet_to_rainbow_query(tid) | |
197 | if not res: | |
198 | db.tweet_store(tid) | |
199 | res = db.tweet_to_rainbow_query(tid) | |
200 | rid = res[0].rainbow_id | |
201 | ||
202 | # Format info | |
c075e6dc O |
203 | user = cycle_color( |
204 | name) + color_func(c['TWEET']['nick'])(' ' + screen_name + ' ') | |
205 | meta = color_func(c['TWEET']['clock'])( | |
206 | '[' + clock + '] ') + color_func(c['TWEET']['id'])('[id=' + str(rid) + '] ') | |
7500d90b | 207 | if favorited: |
1a0ac1b8 | 208 | meta = meta + color_func(c['TWEET']['favorited'])(u'\u2605') |
7500d90b VNM |
209 | tweet = text.split() |
210 | # Replace url | |
211 | if expanded_url: | |
212 | for index in range(len(expanded_url)): | |
213 | tweet = map( | |
214 | lambda x: expanded_url[index] if x == url[index] else x, | |
215 | tweet) | |
216 | # Highlight RT | |
c075e6dc O |
217 | tweet = map( |
218 | lambda x: color_func( | |
219 | c['TWEET']['rt'])(x) if x == 'RT' else x, | |
220 | tweet) | |
7500d90b VNM |
221 | # Highlight screen_name |
222 | tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet) | |
223 | # Highlight link | |
c075e6dc O |
224 | tweet = map( |
225 | lambda x: color_func( | |
226 | c['TWEET']['link'])(x) if x[ | |
227 | 0:4] == 'http' else x, | |
228 | tweet) | |
7500d90b VNM |
229 | # Highlight search keyword |
230 | if keyword: | |
231 | tweet = map( | |
632c6fa5 | 232 | lambda x: color_func(c['TWEET']['keyword'])(x) if |
7500d90b VNM |
233 | ''.join(c for c in x if c.isalnum()).lower() == keyword.lower() |
234 | else x, | |
235 | tweet | |
236 | ) | |
237 | # Recreate tweet | |
238 | tweet = ' '.join(tweet) | |
239 | ||
240 | # Draw rainbow | |
241 | line1 = u"{u:>{uw}}:".format( | |
242 | u=user, | |
243 | uw=len(user) + 2, | |
244 | ) | |
245 | line2 = u"{c:>{cw}}".format( | |
246 | c=meta, | |
247 | cw=len(meta) + 2, | |
248 | ) | |
249 | line3 = ' ' + tweet | |
250 | ||
251 | printNicely('') | |
252 | printNicely(line1) | |
253 | printNicely(line2) | |
254 | printNicely(line3) | |
255 | ||
256 | # Display Image | |
257 | if iot and media_url: | |
258 | for mu in media_url: | |
259 | response = requests.get(mu) | |
260 | image_to_display(StringIO(response.content)) | |
261 | ||
262 | ||
263 | def print_message(m): | |
264 | """ | |
265 | Print direct message | |
266 | """ | |
267 | sender_screen_name = '@' + m['sender_screen_name'] | |
268 | sender_name = m['sender']['name'] | |
269 | text = m['text'] | |
270 | recipient_screen_name = '@' + m['recipient_screen_name'] | |
271 | recipient_name = m['recipient']['name'] | |
272 | mid = m['id'] | |
273 | date = parser.parse(m['created_at']) | |
274 | date = date - datetime.timedelta(seconds=time.timezone) | |
275 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
276 | ||
277 | # Get rainbow id | |
278 | res = db.message_to_rainbow_query(mid) | |
279 | if not res: | |
280 | db.message_store(mid) | |
281 | res = db.message_to_rainbow_query(mid) | |
282 | rid = res[0].rainbow_id | |
283 | ||
6fa09c14 | 284 | # Draw |
c075e6dc O |
285 | sender = cycle_color( |
286 | sender_name) + color_func(c['MESSAGE']['sender'])(' ' + sender_screen_name + ' ') | |
287 | recipient = cycle_color(recipient_name) + color_func( | |
288 | c['MESSAGE']['recipient'])( | |
289 | ' ' + recipient_screen_name + ' ') | |
632c6fa5 | 290 | user = sender + color_func(c['MESSAGE']['to'])(' >>> ') + recipient |
c075e6dc O |
291 | meta = color_func( |
292 | c['MESSAGE']['clock'])( | |
293 | '[' + clock + ']') + color_func( | |
294 | c['MESSAGE']['id'])( | |
295 | ' [message_id=' + str(rid) + '] ') | |
7500d90b VNM |
296 | text = ''.join(map(lambda x: x + ' ' if x == '\n' else x, text)) |
297 | ||
298 | line1 = u"{u:>{uw}}:".format( | |
299 | u=user, | |
300 | uw=len(user) + 2, | |
301 | ) | |
302 | line2 = u"{c:>{cw}}".format( | |
303 | c=meta, | |
304 | cw=len(meta) + 2, | |
305 | ) | |
306 | ||
307 | line3 = ' ' + text | |
308 | ||
309 | printNicely('') | |
310 | printNicely(line1) | |
311 | printNicely(line2) | |
312 | printNicely(line3) | |
313 | ||
314 | ||
315 | def show_profile(u, iot=False): | |
316 | """ | |
317 | Show a profile | |
318 | """ | |
319 | # Retrieve info | |
320 | name = u['name'] | |
321 | screen_name = u['screen_name'] | |
322 | description = u['description'] | |
323 | profile_image_url = u['profile_image_url'] | |
324 | location = u['location'] | |
325 | url = u['url'] | |
326 | created_at = u['created_at'] | |
327 | statuses_count = u['statuses_count'] | |
328 | friends_count = u['friends_count'] | |
329 | followers_count = u['followers_count'] | |
6fa09c14 | 330 | |
7500d90b | 331 | # Create content |
c075e6dc O |
332 | statuses_count = color_func( |
333 | c['PROFILE']['statuses_count'])( | |
334 | str(statuses_count) + | |
335 | ' tweets') | |
336 | friends_count = color_func( | |
337 | c['PROFILE']['friends_count'])( | |
338 | str(friends_count) + | |
339 | ' following') | |
340 | followers_count = color_func( | |
341 | c['PROFILE']['followers_count'])( | |
342 | str(followers_count) + | |
343 | ' followers') | |
7500d90b | 344 | count = statuses_count + ' ' + friends_count + ' ' + followers_count |
c075e6dc O |
345 | user = cycle_color( |
346 | name) + color_func(c['PROFILE']['nick'])(' @' + screen_name + ' : ') + count | |
347 | profile_image_raw_url = 'Profile photo: ' + \ | |
348 | color_func(c['PROFILE']['profile_image_url'])(profile_image_url) | |
7500d90b VNM |
349 | description = ''.join( |
350 | map(lambda x: x + ' ' * 4 if x == '\n' else x, description)) | |
632c6fa5 O |
351 | description = color_func(c['PROFILE']['description'])(description) |
352 | location = 'Location : ' + color_func(c['PROFILE']['location'])(location) | |
353 | url = 'URL : ' + (color_func(c['PROFILE']['url'])(url) if url else '') | |
7500d90b VNM |
354 | date = parser.parse(created_at) |
355 | date = date - datetime.timedelta(seconds=time.timezone) | |
356 | clock = date.strftime('%Y/%m/%d %H:%M:%S') | |
632c6fa5 | 357 | clock = 'Join at ' + color_func(c['PROFILE']['clock'])(clock) |
6fa09c14 | 358 | |
7500d90b VNM |
359 | # Format |
360 | line1 = u"{u:>{uw}}".format( | |
361 | u=user, | |
362 | uw=len(user) + 2, | |
363 | ) | |
364 | line2 = u"{p:>{pw}}".format( | |
365 | p=profile_image_raw_url, | |
366 | pw=len(profile_image_raw_url) + 4, | |
367 | ) | |
368 | line3 = u"{d:>{dw}}".format( | |
369 | d=description, | |
370 | dw=len(description) + 4, | |
371 | ) | |
372 | line4 = u"{l:>{lw}}".format( | |
373 | l=location, | |
374 | lw=len(location) + 4, | |
375 | ) | |
376 | line5 = u"{u:>{uw}}".format( | |
377 | u=url, | |
378 | uw=len(url) + 4, | |
379 | ) | |
380 | line6 = u"{c:>{cw}}".format( | |
381 | c=clock, | |
382 | cw=len(clock) + 4, | |
383 | ) | |
6fa09c14 | 384 | |
7500d90b VNM |
385 | # Display |
386 | printNicely('') | |
387 | printNicely(line1) | |
388 | if iot: | |
389 | response = requests.get(profile_image_url) | |
390 | image_to_display(StringIO(response.content), 2, 20) | |
391 | else: | |
392 | printNicely(line2) | |
393 | for line in [line3, line4, line5, line6]: | |
394 | printNicely(line) | |
395 | printNicely('') | |
396 | ||
397 | ||
398 | def print_trends(trends): | |
399 | """ | |
400 | Display topics | |
401 | """ | |
632c6fa5 | 402 | for topic in trends[:c['TREND_MAX']]: |
7500d90b VNM |
403 | name = topic['name'] |
404 | url = topic['url'] | |
8394e34b | 405 | line = cycle_color(name) + ': ' + color_func(c['TREND']['url'])(url) |
7500d90b VNM |
406 | printNicely(line) |
407 | printNicely('') |