0be6ecebc389ae8fb440a4d6a6d67d18a9fe2588
8 from twitter
.util
import printNicely
9 from functools
import wraps
10 from pyfiglet
import figlet_format
11 from dateutil
import parser
12 from .c_image
import *
16 from .py3patch
import *
27 colors_shuffle
= [globals()[i
.encode('utf8')]
28 if not str(i
).isdigit()
29 else term_color(int(i
))
30 for i
in c
['CYCLE_COLOR']]
31 return itertools
.cycle(colors_shuffle
)
38 g
['cyc'] = init_cycle()
44 Print a string with ordered color with each character
46 colors_shuffle
= [globals()[i
.encode('utf8')]
47 if not str(i
).isdigit()
48 else term_color(int(i
))
49 for i
in c
['CYCLE_COLOR']]
50 colored
= [colors_shuffle
[i
% 7](s
[i
]) for i
in xrange(len(s
))]
51 return ''.join(colored
)
54 def random_rainbow(s
):
56 Print a string with random color with each character
58 colors_shuffle
= [globals()[i
.encode('utf8')]
59 if not str(i
).isdigit()
60 else term_color(int(i
))
61 for i
in c
['CYCLE_COLOR']]
62 colored
= [random
.choice(colors_shuffle
)(i
) for i
in s
]
63 return ''.join(colored
)
72 if args
not in g
['cache']:
73 g
['cache'][args
] = func(*args
)
74 return g
['cache'][args
]
81 Cycle the colors_shuffle
83 return next(g
['cyc'])(s
)
90 fi
= figlet_format(text
, font
='doom')
92 [next(g
['cyc'])(i
) for i
in fi
.split('\n')]
96 def show_calendar(month
, date
, rel
):
98 Show the calendar in rainbow mode
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()))
107 ary
= line
.split(' ')
108 ary
= lmap(lambda x
: color_func(c
['CAL']['today'])(x
)
110 else color_func(c
['CAL']['days'])(x
), ary
)
111 printNicely(' '.join(ary
))
116 Check if config is changed
119 data
= get_all_config()
122 if data
[key
] != c
[key
]:
128 def validate_theme(theme
):
130 Validate a theme exists or not
132 # Theme changed check
133 files
= os
.listdir(os
.path
.dirname(__file__
) + '/colorset')
134 themes
= [f
.split('.')[0] for f
in files
if f
.split('.')[-1] == 'json']
135 return theme
in themes
138 def reload_theme(current_config
):
140 Check current theme and update if necessary
142 exists
= db
.theme_query()
143 themes
= [t
.theme_name
for t
in exists
]
144 if current_config
!= themes
[0]:
145 config
= os
.path
.dirname(
146 __file__
) + '/colorset/' + current_config
+ '.json'
148 data
= load_config(config
)
152 # Restart color cycle and update db/config
154 db
.theme_update(current_config
)
155 set_config('THEME', current_config
)
158 def color_func(func_name
):
160 Call color function base on name
162 if str(func_name
).isdigit():
163 return term_color(int(func_name
))
164 return globals()[func_name
]
167 def draw(t
, keyword
=None, check_semaphore
=False, fil
=[], ig
=[]):
173 reload_theme(c
['THEME'])
177 screen_name
= t
['user']['screen_name']
178 name
= t
['user']['name']
179 created_at
= t
['created_at']
180 favorited
= t
['favorited']
181 date
= parser
.parse(created_at
)
182 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
183 clock_format
= '%Y/%m/%d %H:%M:%S'
185 clock_format
= c
['FORMAT']['TWEET']['CLOCK_FORMAT']
188 clock
= date
.strftime(clock_format
)
190 # Pull extended retweet text
192 text
= 'RT @' + t
['retweeted_status']['user']['screen_name'] + ': ' +\
193 t
['retweeted_status']['text']
197 # Unescape HTML character
198 text
= unescape(text
)
204 urls
= t
['entities']['urls']
206 expanded_url
.append(u
['expanded_url'])
215 media
= t
['entities']['media']
217 media_url
.append(m
['media_url'])
222 screen_name
= '@' + screen_name
223 if fil
and screen_name
not in fil
:
225 if ig
and screen_name
in ig
:
229 res
= db
.tweet_to_rainbow_query(tid
)
232 res
= db
.tweet_to_rainbow_query(tid
)
233 rid
= res
[0].rainbow_id
236 name
= cycle_color(name
)
237 nick
= color_func(c
['TWEET']['nick'])(screen_name
)
242 fav
= color_func(c
['TWEET']['favorited'])(u
'\u2605')
247 for index
in range(len(expanded_url
)):
249 lambda x
: expanded_url
[index
] if x
== url
[index
] else x
,
253 lambda x
: color_func(
254 c
['TWEET']['rt'])(x
) if x
== 'RT' else x
,
256 # Highlight screen_name
257 tweet
= lmap(lambda x
: cycle_color(x
) if x
[0] == '@' else x
, tweet
)
260 lambda x
: color_func(
261 c
['TWEET']['link'])(x
) if x
[
262 0:4] == 'http' else x
,
266 tweet
= ' '.join(tweet
)
268 roj
= re
.search(keyword
, tweet
, re
.IGNORECASE
)
271 ary
= tweet
.split(occur
)
272 delimiter
= color_func(c
['TWEET']['keyword'])(occur
)
273 tweet
= delimiter
.join(ary
)
275 # Load config formater
277 formater
= c
['FORMAT']['TWEET']['DISPLAY']
278 formater
= name
.join(formater
.split("#name"))
279 formater
= nick
.join(formater
.split("#nick"))
280 formater
= fav
.join(formater
.split("#fav"))
281 formater
= tweet
.join(formater
.split("#tweet"))
283 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
284 delimiter
= color_func(
285 c
['TWEET']['clock'])(
287 word
.split('#clock')))
288 formater
= delimiter
.join(formater
.split(word
))
290 word
= [w
for w
in formater
.split() if '#id' in w
][0]
291 delimiter
= color_func(c
['TWEET']['id'])(id.join(word
.split('#id')))
292 formater
= delimiter
.join(formater
.split(word
))
294 printNicely(red('Wrong format in config.'))
297 # Check the semaphore lock (stream process only)
299 if db
.semaphore_query_pause():
301 while db
.semaphore_query_flag():
305 printNicely(formater
)
308 if c
['IMAGE_ON_TERM'] and media_url
:
311 response
= requests
.get(mu
)
312 image_to_display(BytesIO(response
.content
))
314 printNicely(red('Sorry, image link is broken'))
317 def print_message(m
):
321 sender_screen_name
= '@' + m
['sender_screen_name']
322 sender_name
= m
['sender']['name']
323 text
= unescape(m
['text'])
324 recipient_screen_name
= '@' + m
['recipient_screen_name']
325 recipient_name
= m
['recipient']['name']
327 date
= parser
.parse(m
['created_at'])
328 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
329 clock_format
= '%Y/%m/%d %H:%M:%S'
331 clock_format
= c
['FORMAT']['MESSAGE']['CLOCK_FORMAT']
334 clock
= date
.strftime(clock_format
)
337 res
= db
.message_to_rainbow_query(mid
)
339 db
.message_store(mid
)
340 res
= db
.message_to_rainbow_query(mid
)
341 rid
= res
[0].rainbow_id
344 sender_name
= cycle_color(sender_name
)
345 sender_nick
= color_func(c
['MESSAGE']['sender'])(sender_screen_name
)
346 recipient_name
= cycle_color(recipient_name
)
347 recipient_nick
= color_func(
348 c
['MESSAGE']['recipient'])(recipient_screen_name
)
349 to
= color_func(c
['MESSAGE']['to'])('>>>')
353 text
= ''.join(lmap(lambda x
: x
+ ' ' if x
== '\n' else x
, text
))
355 # Load config formater
357 formater
= c
['FORMAT']['MESSAGE']['DISPLAY']
358 formater
= sender_name
.join(formater
.split("#sender_name"))
359 formater
= sender_nick
.join(formater
.split("#sender_nick"))
360 formater
= to
.join(formater
.split("#to"))
361 formater
= recipient_name
.join(formater
.split("#recipient_name"))
362 formater
= recipient_nick
.join(formater
.split("#recipient_nick"))
363 formater
= text
.join(formater
.split("#message"))
365 word
= [w
for w
in formater
.split() if '#clock' in w
][0]
366 delimiter
= color_func(
367 c
['MESSAGE']['clock'])(
369 word
.split('#clock')))
370 formater
= delimiter
.join(formater
.split(word
))
372 word
= [w
for w
in formater
.split() if '#id' in w
][0]
373 delimiter
= color_func(c
['MESSAGE']['id'])(id.join(word
.split('#id')))
374 formater
= delimiter
.join(formater
.split(word
))
376 printNicely(red('Wrong format in config.'))
380 printNicely(formater
)
389 screen_name
= u
['screen_name']
390 description
= u
['description']
391 profile_image_url
= u
['profile_image_url']
392 location
= u
['location']
394 created_at
= u
['created_at']
395 statuses_count
= u
['statuses_count']
396 friends_count
= u
['friends_count']
397 followers_count
= u
['followers_count']
400 statuses_count
= color_func(
401 c
['PROFILE']['statuses_count'])(
402 str(statuses_count
) +
404 friends_count
= color_func(
405 c
['PROFILE']['friends_count'])(
408 followers_count
= color_func(
409 c
['PROFILE']['followers_count'])(
410 str(followers_count
) +
412 count
= statuses_count
+ ' ' + friends_count
+ ' ' + followers_count
414 name
) + color_func(c
['PROFILE']['nick'])(' @' + screen_name
+ ' : ') + count
415 profile_image_raw_url
= 'Profile photo: ' + \
416 color_func(c
['PROFILE']['profile_image_url'])(profile_image_url
)
417 description
= ''.join(
418 lmap(lambda x
: x
+ ' ' * 4 if x
== '\n' else x
, description
))
419 description
= color_func(c
['PROFILE']['description'])(description
)
420 location
= 'Location : ' + color_func(c
['PROFILE']['location'])(location
)
421 url
= 'URL : ' + (color_func(c
['PROFILE']['url'])(url
) if url
else '')
422 date
= parser
.parse(created_at
)
423 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
424 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
425 clock
= 'Join at ' + color_func(c
['PROFILE']['clock'])(clock
)
428 line1
= u
"{u:>{uw}}".format(
432 line2
= u
"{p:>{pw}}".format(
433 p
=profile_image_raw_url
,
434 pw
=len(profile_image_raw_url
) + 4,
436 line3
= u
"{d:>{dw}}".format(
438 dw
=len(description
) + 4,
440 line4
= u
"{l:>{lw}}".format(
442 lw
=len(location
) + 4,
444 line5
= u
"{u:>{uw}}".format(
448 line6
= u
"{c:>{cw}}".format(
456 if c
['IMAGE_ON_TERM']:
458 response
= requests
.get(profile_image_url
)
459 image_to_display(BytesIO(response
.content
), 2, 20)
464 for line
in [line3
, line4
, line5
, line6
]:
469 def print_trends(trends
):
473 for topic
in trends
[:c
['TREND_MAX']]:
476 line
= cycle_color(name
) + ': ' + color_func(c
['TREND']['url'])(url
)
481 def print_list(group
):
487 name
= g
['full_name']
488 name
= color_func(c
['GROUP']['name'])(name
+ ' : ')
489 member
= str(g
['member_count'])
490 member
= color_func(c
['GROUP']['member'])(member
+ ' member')
491 subscriber
= str(g
['subscriber_count'])
492 subscriber
= color_func(
493 c
['GROUP']['subscriber'])(
496 description
= g
['description'].strip()
497 description
= color_func(c
['GROUP']['description'])(description
)
499 mode
= color_func(c
['GROUP']['mode'])('Type: ' + mode
)
500 created_at
= g
['created_at']
501 date
= parser
.parse(created_at
)
502 date
= date
- datetime
.timedelta(seconds
=time
.timezone
)
503 clock
= date
.strftime('%Y/%m/%d %H:%M:%S')
504 clock
= 'Created at ' + color_func(c
['GROUP']['clock'])(clock
)
507 line1
= ' ' * 2 + name
+ member
+ ' ' + subscriber
508 line2
= ' ' * 4 + description
509 line3
= ' ' * 4 + mode
510 line4
= ' ' * 4 + clock
522 # Start the color cycle