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