Commit | Line | Data |
---|---|---|
91476ec3 O |
1 | """ |
2 | Colorful user's timeline stream | |
3 | """ | |
b2b933a9 | 4 | import os |
5 | import os.path | |
6 | import sys | |
7 | import signal | |
8 | import argparse | |
9 | import time | |
92983945 | 10 | import threading |
991c30af | 11 | import requests |
80b70d60 | 12 | import webbrowser |
91476ec3 | 13 | |
91476ec3 | 14 | from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup |
54277114 | 15 | from twitter.api import * |
91476ec3 | 16 | from twitter.oauth import OAuth, read_token_file |
8c840a83 | 17 | from twitter.oauth_dance import oauth_dance |
91476ec3 | 18 | from twitter.util import printNicely |
91476ec3 | 19 | |
7500d90b | 20 | from .draw import * |
2a6238f5 O |
21 | from .colors import * |
22 | from .config import * | |
777c52d4 | 23 | from .consumer import * |
94a5f62e | 24 | from .interactive import * |
991c30af | 25 | from .c_image import * |
c3bab4ef | 26 | from .py3patch import * |
27 | ||
531f5682 | 28 | # Global values |
f405a7d0 | 29 | g = {} |
531f5682 | 30 | |
92983945 | 31 | # Lock for streams |
92983945 BS |
32 | StreamLock = threading.Lock() |
33 | ||
c075e6dc | 34 | |
91476ec3 O |
35 | def parse_arguments(): |
36 | """ | |
37 | Parse the arguments | |
38 | """ | |
91476ec3 | 39 | parser = argparse.ArgumentParser(description=__doc__ or "") |
2a6238f5 O |
40 | parser.add_argument( |
41 | '-to', | |
42 | '--timeout', | |
43 | help='Timeout for the stream (seconds).') | |
2a6238f5 O |
44 | parser.add_argument( |
45 | '-tt', | |
46 | '--track-keywords', | |
47 | help='Search the stream for specific text.') | |
d51b4107 O |
48 | parser.add_argument( |
49 | '-fil', | |
50 | '--filter', | |
51 | help='Filter specific screen_name.') | |
52 | parser.add_argument( | |
53 | '-ig', | |
54 | '--ignore', | |
55 | help='Ignore specific screen_name.') | |
88af38d8 | 56 | parser.add_argument( |
c1fa7c94 O |
57 | '-iot', |
58 | '--image-on-term', | |
59 | action='store_true', | |
60 | help='Display all image on terminal.') | |
91476ec3 O |
61 | return parser.parse_args() |
62 | ||
63 | ||
54277114 O |
64 | def authen(): |
65 | """ | |
7b674cef | 66 | Authenticate with Twitter OAuth |
54277114 | 67 | """ |
8c840a83 | 68 | # When using rainbow stream you must authorize. |
2a6238f5 O |
69 | twitter_credential = os.environ.get( |
70 | 'HOME', | |
71 | os.environ.get( | |
72 | 'USERPROFILE', | |
73 | '')) + os.sep + '.rainbow_oauth' | |
8c840a83 O |
74 | if not os.path.exists(twitter_credential): |
75 | oauth_dance("Rainbow Stream", | |
76 | CONSUMER_KEY, | |
77 | CONSUMER_SECRET, | |
78 | twitter_credential) | |
79 | oauth_token, oauth_token_secret = read_token_file(twitter_credential) | |
54277114 | 80 | return OAuth( |
2a6238f5 O |
81 | oauth_token, |
82 | oauth_token_secret, | |
83 | CONSUMER_KEY, | |
84 | CONSUMER_SECRET) | |
91476ec3 | 85 | |
54277114 | 86 | |
e3927852 O |
87 | def build_mute_dict(dict_data=False): |
88 | """ | |
89 | Build muting list | |
90 | """ | |
91 | t = Twitter(auth=authen()) | |
92 | # Init cursor | |
93 | next_cursor = -1 | |
94 | screen_name_list = [] | |
95 | name_list = [] | |
96 | # Cursor loop | |
97 | while next_cursor != 0: | |
98 | list = t.mutes.users.list( | |
99 | screen_name=g['original_name'], | |
100 | cursor=next_cursor, | |
101 | skip_status=True, | |
102 | include_entities=False, | |
103 | ) | |
104 | screen_name_list += ['@' + u['screen_name'] for u in list['users']] | |
105 | name_list += [u['name'] for u in list['users']] | |
106 | next_cursor = list['next_cursor'] | |
107 | # Return dict or list | |
108 | if dict_data: | |
109 | return dict(zip(screen_name_list, name_list)) | |
110 | else: | |
111 | return screen_name_list | |
112 | ||
113 | ||
fe9bb33b | 114 | def init(args): |
54277114 | 115 | """ |
9683e61d | 116 | Init function |
54277114 | 117 | """ |
64156ac4 O |
118 | # Handle Ctrl C |
119 | ctrl_c_handler = lambda signum, frame: quit() | |
120 | signal.signal(signal.SIGINT, ctrl_c_handler) | |
9683e61d | 121 | # Get name |
54277114 | 122 | t = Twitter(auth=authen()) |
c5ff542b | 123 | name = '@' + t.account.verify_credentials()['screen_name'] |
ceec8593 | 124 | if not get_config('PREFIX'): |
125 | set_config('PREFIX', name) | |
42fde775 | 126 | g['original_name'] = name[1:] |
ceec8593 | 127 | g['decorated_name'] = lambda x: color_func( |
a8e71259 | 128 | c['DECORATED_NAME'])('[' + x + ']: ') |
9683e61d | 129 | # Theme init |
422dd385 | 130 | files = os.listdir(os.path.dirname(__file__) + '/colorset') |
c075e6dc | 131 | themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] |
632c6fa5 | 132 | g['themes'] = themes |
4824b181 | 133 | # Startup cmd |
f1c1dfea | 134 | g['cmd'] = '' |
9683e61d | 135 | # Semaphore init |
99b52f5f O |
136 | c['lock'] = False |
137 | c['pause'] = False | |
138 | # Init tweet dict and message dict | |
139 | c['tweet_dict'] = [] | |
140 | c['message_dict'] = [] | |
fe9bb33b | 141 | # Image on term |
142 | c['IMAGE_ON_TERM'] = args.image_on_term | |
62686013 | 143 | set_config('IMAGE_ON_TERM', str(c['IMAGE_ON_TERM'])) |
e3927852 O |
144 | # Mute dict |
145 | c['IGNORE_LIST'] += build_mute_dict() | |
f405a7d0 | 146 | |
ceec8593 | 147 | |
42fde775 | 148 | def switch(): |
149 | """ | |
150 | Switch stream | |
151 | """ | |
152 | try: | |
153 | target = g['stuff'].split()[0] | |
d51b4107 O |
154 | # Filter and ignore |
155 | args = parse_arguments() | |
7e4ccbf3 | 156 | try: |
d51b4107 | 157 | if g['stuff'].split()[-1] == '-f': |
14db58c7 | 158 | guide = 'To ignore an option, just hit Enter key.' |
159 | printNicely(light_magenta(guide)) | |
160 | only = raw_input('Only nicks [Ex: @xxx,@yy]: ') | |
161 | ignore = raw_input('Ignore nicks [Ex: @xxx,@yy]: ') | |
7e4ccbf3 | 162 | args.filter = filter(None, only.split(',')) |
163 | args.ignore = filter(None, ignore.split(',')) | |
d51b4107 | 164 | elif g['stuff'].split()[-1] == '-d': |
632c6fa5 O |
165 | args.filter = c['ONLY_LIST'] |
166 | args.ignore = c['IGNORE_LIST'] | |
d51b4107 O |
167 | except: |
168 | printNicely(red('Sorry, wrong format.')) | |
169 | return | |
42fde775 | 170 | # Public stream |
171 | if target == 'public': | |
172 | keyword = g['stuff'].split()[1] | |
173 | if keyword[0] == '#': | |
174 | keyword = keyword[1:] | |
92983945 BS |
175 | # Kill old thread |
176 | g['stream_stop'] = True | |
42fde775 | 177 | args.track_keywords = keyword |
92983945 | 178 | # Start new thread |
baec5f50 | 179 | th = threading.Thread( |
180 | target=stream, | |
181 | args=( | |
182 | c['PUBLIC_DOMAIN'], | |
183 | args)) | |
92983945 BS |
184 | th.daemon = True |
185 | th.start() | |
42fde775 | 186 | # Personal stream |
187 | elif target == 'mine': | |
92983945 BS |
188 | # Kill old thread |
189 | g['stream_stop'] = True | |
190 | # Start new thread | |
baec5f50 | 191 | th = threading.Thread( |
192 | target=stream, | |
193 | args=( | |
194 | c['USER_DOMAIN'], | |
195 | args, | |
196 | g['original_name'])) | |
92983945 BS |
197 | th.daemon = True |
198 | th.start() | |
d51b4107 | 199 | printNicely('') |
d51b4107 O |
200 | if args.filter: |
201 | printNicely(cyan('Only: ' + str(args.filter))) | |
202 | if args.ignore: | |
203 | printNicely(red('Ignore: ' + str(args.ignore))) | |
204 | printNicely('') | |
49514d7e | 205 | except: |
42fde775 | 206 | printNicely(red('Sorry I can\'t understand.')) |
42fde775 | 207 | |
208 | ||
4592d231 | 209 | def trend(): |
210 | """ | |
211 | Trend | |
212 | """ | |
213 | t = Twitter(auth=authen()) | |
48a25fe8 | 214 | # Get country and town |
4592d231 | 215 | try: |
216 | country = g['stuff'].split()[0] | |
217 | except: | |
218 | country = '' | |
48a25fe8 | 219 | try: |
220 | town = g['stuff'].split()[1] | |
221 | except: | |
222 | town = '' | |
48a25fe8 | 223 | avail = t.trends.available() |
224 | # World wide | |
225 | if not country: | |
226 | trends = t.trends.place(_id=1)[0]['trends'] | |
227 | print_trends(trends) | |
228 | else: | |
229 | for location in avail: | |
230 | # Search for country and Town | |
231 | if town: | |
232 | if location['countryCode'] == country \ | |
233 | and location['placeType']['name'] == 'Town' \ | |
234 | and location['name'] == town: | |
235 | trends = t.trends.place(_id=location['woeid'])[0]['trends'] | |
236 | print_trends(trends) | |
237 | # Search for country only | |
238 | else: | |
239 | if location['countryCode'] == country \ | |
240 | and location['placeType']['name'] == 'Country': | |
241 | trends = t.trends.place(_id=location['woeid'])[0]['trends'] | |
242 | print_trends(trends) | |
4592d231 | 243 | |
244 | ||
7b674cef | 245 | def home(): |
246 | """ | |
247 | Home | |
248 | """ | |
249 | t = Twitter(auth=authen()) | |
632c6fa5 | 250 | num = c['HOME_TWEET_NUM'] |
7b674cef | 251 | if g['stuff'].isdigit(): |
305ce127 | 252 | num = int(g['stuff']) |
94a5f62e | 253 | for tweet in reversed(t.statuses.home_timeline(count=num)): |
fe9bb33b | 254 | draw(t=tweet) |
94a5f62e | 255 | printNicely('') |
7b674cef | 256 | |
257 | ||
258 | def view(): | |
259 | """ | |
260 | Friend view | |
261 | """ | |
262 | t = Twitter(auth=authen()) | |
263 | user = g['stuff'].split()[0] | |
b8fbcb70 | 264 | if user[0] == '@': |
265 | try: | |
94a5f62e | 266 | num = int(g['stuff'].split()[1]) |
b8fbcb70 | 267 | except: |
632c6fa5 | 268 | num = c['HOME_TWEET_NUM'] |
94a5f62e | 269 | for tweet in reversed(t.statuses.user_timeline(count=num, screen_name=user[1:])): |
fe9bb33b | 270 | draw(t=tweet) |
94a5f62e | 271 | printNicely('') |
b8fbcb70 | 272 | else: |
c91f75f2 | 273 | printNicely(red('A name should begin with a \'@\'')) |
7b674cef | 274 | |
275 | ||
305ce127 | 276 | def mentions(): |
277 | """ | |
278 | Mentions timeline | |
279 | """ | |
280 | t = Twitter(auth=authen()) | |
632c6fa5 | 281 | num = c['HOME_TWEET_NUM'] |
305ce127 | 282 | if g['stuff'].isdigit(): |
283 | num = int(g['stuff']) | |
284 | for tweet in reversed(t.statuses.mentions_timeline(count=num)): | |
fe9bb33b | 285 | draw(t=tweet) |
305ce127 | 286 | printNicely('') |
287 | ||
288 | ||
2d0ad040 J |
289 | def conversation(): |
290 | """ | |
291 | Conversation view | |
292 | """ | |
293 | t = Twitter(auth=authen()) | |
294 | try: | |
295 | id = int(g['stuff'].split()[0]) | |
296 | except: | |
297 | printNicely(red('Sorry I can\'t understand.')) | |
298 | return | |
299 | tid = c['tweet_dict'][id] | |
300 | tweet = t.statuses.show(id=tid) | |
301 | limit = 9 | |
302 | thread_ref = [] | |
303 | thread_ref.append (tweet) | |
304 | prev_tid = tweet['in_reply_to_status_id'] | |
305 | while prev_tid and limit: | |
306 | limit -= 1 | |
307 | tweet = t.statuses.show(id=prev_tid) | |
308 | prev_tid = tweet['in_reply_to_status_id'] | |
309 | thread_ref.append (tweet) | |
310 | ||
311 | for tweet in reversed(thread_ref): | |
312 | draw(t=tweet) | |
313 | printNicely('') | |
314 | ||
315 | ||
f405a7d0 | 316 | def tweet(): |
54277114 | 317 | """ |
7b674cef | 318 | Tweet |
54277114 O |
319 | """ |
320 | t = Twitter(auth=authen()) | |
f405a7d0 | 321 | t.statuses.update(status=g['stuff']) |
f405a7d0 | 322 | |
b2b933a9 | 323 | |
1ba4abfd O |
324 | def retweet(): |
325 | """ | |
326 | ReTweet | |
327 | """ | |
328 | t = Twitter(auth=authen()) | |
329 | try: | |
330 | id = int(g['stuff'].split()[0]) | |
1ba4abfd | 331 | except: |
b8c1f42a O |
332 | printNicely(red('Sorry I can\'t understand.')) |
333 | return | |
99b52f5f | 334 | tid = c['tweet_dict'][id] |
b8c1f42a | 335 | t.statuses.retweet(id=tid, include_entities=False, trim_user=True) |
1ba4abfd O |
336 | |
337 | ||
80b70d60 O |
338 | def quote(): |
339 | """ | |
340 | Quote a tweet | |
341 | """ | |
b7c9c570 | 342 | # Get tweet |
80b70d60 O |
343 | t = Twitter(auth=authen()) |
344 | try: | |
345 | id = int(g['stuff'].split()[0]) | |
346 | except: | |
347 | printNicely(red('Sorry I can\'t understand.')) | |
348 | return | |
99b52f5f | 349 | tid = c['tweet_dict'][id] |
80b70d60 | 350 | tweet = t.statuses.show(id=tid) |
b7c9c570 | 351 | # Get formater |
352 | formater = format_quote(tweet) | |
353 | if not formater: | |
7c437a0f | 354 | return |
7c437a0f O |
355 | # Get comment |
356 | prefix = light_magenta('Compose your ') + light_green('#comment: ') | |
357 | comment = raw_input(prefix) | |
358 | if comment: | |
359 | quote = comment.join(formater.split('#comment')) | |
b7c9c570 | 360 | t.statuses.update(status=quote) |
80b70d60 O |
361 | else: |
362 | printNicely(light_magenta('No text added.')) | |
363 | ||
364 | ||
1f24a05a | 365 | def allretweet(): |
366 | """ | |
367 | List all retweet | |
368 | """ | |
369 | t = Twitter(auth=authen()) | |
370 | # Get rainbow id | |
371 | try: | |
372 | id = int(g['stuff'].split()[0]) | |
373 | except: | |
374 | printNicely(red('Sorry I can\'t understand.')) | |
375 | return | |
99b52f5f | 376 | tid = c['tweet_dict'][id] |
1f24a05a | 377 | # Get display num if exist |
378 | try: | |
379 | num = int(g['stuff'].split()[1]) | |
380 | except: | |
632c6fa5 | 381 | num = c['RETWEETS_SHOW_NUM'] |
1f24a05a | 382 | # Get result and display |
d8e901a4 | 383 | rt_ary = t.statuses.retweets(id=tid, count=num) |
1f24a05a | 384 | if not rt_ary: |
385 | printNicely(magenta('This tweet has no retweet.')) | |
386 | return | |
387 | for tweet in reversed(rt_ary): | |
fe9bb33b | 388 | draw(t=tweet) |
1f24a05a | 389 | printNicely('') |
390 | ||
391 | ||
7e4ccbf3 | 392 | def favorite(): |
393 | """ | |
394 | Favorite | |
395 | """ | |
396 | t = Twitter(auth=authen()) | |
397 | try: | |
398 | id = int(g['stuff'].split()[0]) | |
7e4ccbf3 | 399 | except: |
b8c1f42a O |
400 | printNicely(red('Sorry I can\'t understand.')) |
401 | return | |
99b52f5f | 402 | tid = c['tweet_dict'][id] |
b8c1f42a O |
403 | t.favorites.create(_id=tid, include_entities=False) |
404 | printNicely(green('Favorited.')) | |
fe9bb33b | 405 | draw(t.statuses.show(id=tid)) |
b8c1f42a | 406 | printNicely('') |
7e4ccbf3 | 407 | |
408 | ||
7b674cef | 409 | def reply(): |
829cc2d8 | 410 | """ |
7b674cef | 411 | Reply |
829cc2d8 O |
412 | """ |
413 | t = Twitter(auth=authen()) | |
7b674cef | 414 | try: |
415 | id = int(g['stuff'].split()[0]) | |
7b674cef | 416 | except: |
c91f75f2 | 417 | printNicely(red('Sorry I can\'t understand.')) |
b8c1f42a | 418 | return |
99b52f5f | 419 | tid = c['tweet_dict'][id] |
b8c1f42a O |
420 | user = t.statuses.show(id=tid)['user']['screen_name'] |
421 | status = ' '.join(g['stuff'].split()[1:]) | |
7c437a0f | 422 | status = '@' + user + ' ' + str2u(status) |
b8c1f42a | 423 | t.statuses.update(status=status, in_reply_to_status_id=tid) |
7b674cef | 424 | |
425 | ||
426 | def delete(): | |
427 | """ | |
428 | Delete | |
429 | """ | |
430 | t = Twitter(auth=authen()) | |
431 | try: | |
99b52f5f | 432 | id = int(g['stuff'].split()[0]) |
7b674cef | 433 | except: |
305ce127 | 434 | printNicely(red('Sorry I can\'t understand.')) |
b8c1f42a | 435 | return |
99b52f5f | 436 | tid = c['tweet_dict'][id] |
b8c1f42a O |
437 | t.statuses.destroy(id=tid) |
438 | printNicely(green('Okay it\'s gone.')) | |
829cc2d8 O |
439 | |
440 | ||
7e4ccbf3 | 441 | def unfavorite(): |
442 | """ | |
443 | Unfavorite | |
444 | """ | |
445 | t = Twitter(auth=authen()) | |
446 | try: | |
447 | id = int(g['stuff'].split()[0]) | |
7e4ccbf3 | 448 | except: |
b8c1f42a O |
449 | printNicely(red('Sorry I can\'t understand.')) |
450 | return | |
99b52f5f | 451 | tid = c['tweet_dict'][id] |
b8c1f42a O |
452 | t.favorites.destroy(_id=tid) |
453 | printNicely(green('Okay it\'s unfavorited.')) | |
fe9bb33b | 454 | draw(t.statuses.show(id=tid)) |
b8c1f42a | 455 | printNicely('') |
7e4ccbf3 | 456 | |
457 | ||
f405a7d0 O |
458 | def search(): |
459 | """ | |
7b674cef | 460 | Search |
f405a7d0 O |
461 | """ |
462 | t = Twitter(auth=authen()) | |
59262e95 O |
463 | g['stuff'] = g['stuff'].strip() |
464 | rel = t.search.tweets(q=g['stuff'])['statuses'] | |
465 | if rel: | |
466 | printNicely('Newest tweets:') | |
467 | for i in reversed(xrange(c['SEARCH_MAX_RECORD'])): | |
468 | draw(t=rel[i], | |
59262e95 O |
469 | keyword=g['stuff']) |
470 | printNicely('') | |
b8c1f42a | 471 | else: |
59262e95 | 472 | printNicely(magenta('I\'m afraid there is no result')) |
b2b933a9 | 473 | |
f405a7d0 | 474 | |
305ce127 | 475 | def message(): |
476 | """ | |
477 | Send a direct message | |
478 | """ | |
479 | t = Twitter(auth=authen()) | |
480 | user = g['stuff'].split()[0] | |
b8c1f42a | 481 | if user[0].startswith('@'): |
305ce127 | 482 | try: |
483 | content = g['stuff'].split()[1] | |
305ce127 | 484 | except: |
485 | printNicely(red('Sorry I can\'t understand.')) | |
b8c1f42a O |
486 | t.direct_messages.new( |
487 | screen_name=user[1:], | |
488 | text=content | |
489 | ) | |
490 | printNicely(green('Message sent.')) | |
305ce127 | 491 | else: |
492 | printNicely(red('A name should begin with a \'@\'')) | |
493 | ||
494 | ||
f5677fb1 | 495 | def show(): |
843647ad | 496 | """ |
f5677fb1 | 497 | Show image |
843647ad O |
498 | """ |
499 | t = Twitter(auth=authen()) | |
f5677fb1 O |
500 | try: |
501 | target = g['stuff'].split()[0] | |
502 | if target != 'image': | |
503 | return | |
504 | id = int(g['stuff'].split()[1]) | |
99b52f5f | 505 | tid = c['tweet_dict'][id] |
f5677fb1 O |
506 | tweet = t.statuses.show(id=tid) |
507 | media = tweet['entities']['media'] | |
508 | for m in media: | |
509 | res = requests.get(m['media_url']) | |
b3164e62 | 510 | img = Image.open(BytesIO(res.content)) |
f5677fb1 O |
511 | img.show() |
512 | except: | |
513 | printNicely(red('Sorry I can\'t show this image.')) | |
843647ad O |
514 | |
515 | ||
80bb2040 | 516 | def urlopen(): |
80b70d60 O |
517 | """ |
518 | Open url | |
519 | """ | |
520 | t = Twitter(auth=authen()) | |
521 | try: | |
522 | if not g['stuff'].isdigit(): | |
523 | return | |
8101275e | 524 | tid = c['tweet_dict'][int(g['stuff'])] |
80b70d60 | 525 | tweet = t.statuses.show(id=tid) |
422dd385 O |
526 | link_ary = [ |
527 | u for u in tweet['text'].split() if u.startswith('http://')] | |
80b70d60 O |
528 | if not link_ary: |
529 | printNicely(light_magenta('No url here @.@!')) | |
530 | return | |
531 | for link in link_ary: | |
532 | webbrowser.open(link) | |
533 | except: | |
534 | printNicely(red('Sorry I can\'t open url in this tweet.')) | |
535 | ||
536 | ||
2d341029 | 537 | def ls(): |
0f6e4daf | 538 | """ |
539 | List friends for followers | |
540 | """ | |
541 | t = Twitter(auth=authen()) | |
e2b81717 O |
542 | # Get name |
543 | try: | |
544 | name = g['stuff'].split()[1] | |
b8c1f42a | 545 | if name.startswith('@'): |
e2b81717 O |
546 | name = name[1:] |
547 | else: | |
548 | printNicely(red('A name should begin with a \'@\'')) | |
549 | raise Exception('Invalid name') | |
550 | except: | |
551 | name = g['original_name'] | |
552 | # Get list followers or friends | |
0f6e4daf | 553 | try: |
554 | target = g['stuff'].split()[0] | |
0f6e4daf | 555 | except: |
556 | printNicely(red('Omg some syntax is wrong.')) | |
b8c1f42a O |
557 | # Init cursor |
558 | d = {'fl': 'followers', 'fr': 'friends'} | |
559 | next_cursor = -1 | |
560 | rel = {} | |
561 | # Cursor loop | |
562 | while next_cursor != 0: | |
563 | list = getattr(t, d[target]).list( | |
564 | screen_name=name, | |
565 | cursor=next_cursor, | |
566 | skip_status=True, | |
567 | include_entities=False, | |
568 | ) | |
569 | for u in list['users']: | |
570 | rel[u['name']] = '@' + u['screen_name'] | |
571 | next_cursor = list['next_cursor'] | |
572 | # Print out result | |
2d341029 | 573 | printNicely('All: ' + str(len(rel)) + ' ' + d[target] + '.') |
b8c1f42a | 574 | for name in rel: |
2d341029 | 575 | user = ' ' + cycle_color(name) |
422dd385 | 576 | user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') |
b8c1f42a | 577 | printNicely(user) |
0f6e4daf | 578 | |
579 | ||
305ce127 | 580 | def inbox(): |
581 | """ | |
582 | Inbox direct messages | |
583 | """ | |
584 | t = Twitter(auth=authen()) | |
632c6fa5 | 585 | num = c['MESSAGES_DISPLAY'] |
305ce127 | 586 | rel = [] |
587 | if g['stuff'].isdigit(): | |
588 | num = g['stuff'] | |
589 | cur_page = 1 | |
590 | # Max message per page is 20 so we have to loop | |
591 | while num > 20: | |
592 | rel = rel + t.direct_messages( | |
593 | count=20, | |
594 | page=cur_page, | |
595 | include_entities=False, | |
596 | skip_status=False | |
48a25fe8 | 597 | ) |
305ce127 | 598 | num -= 20 |
599 | cur_page += 1 | |
600 | rel = rel + t.direct_messages( | |
601 | count=num, | |
602 | page=cur_page, | |
603 | include_entities=False, | |
604 | skip_status=False | |
48a25fe8 | 605 | ) |
e2b81717 | 606 | # Display |
305ce127 | 607 | printNicely('Inbox: newest ' + str(len(rel)) + ' messages.') |
608 | for m in reversed(rel): | |
609 | print_message(m) | |
610 | printNicely('') | |
611 | ||
e2b81717 | 612 | |
305ce127 | 613 | def sent(): |
614 | """ | |
615 | Sent direct messages | |
616 | """ | |
617 | t = Twitter(auth=authen()) | |
632c6fa5 | 618 | num = c['MESSAGES_DISPLAY'] |
305ce127 | 619 | rel = [] |
620 | if g['stuff'].isdigit(): | |
621 | num = int(g['stuff']) | |
622 | cur_page = 1 | |
623 | # Max message per page is 20 so we have to loop | |
624 | while num > 20: | |
625 | rel = rel + t.direct_messages.sent( | |
626 | count=20, | |
627 | page=cur_page, | |
628 | include_entities=False, | |
629 | skip_status=False | |
48a25fe8 | 630 | ) |
305ce127 | 631 | num -= 20 |
632 | cur_page += 1 | |
633 | rel = rel + t.direct_messages.sent( | |
634 | count=num, | |
635 | page=cur_page, | |
636 | include_entities=False, | |
637 | skip_status=False | |
48a25fe8 | 638 | ) |
e2b81717 | 639 | # Display |
305ce127 | 640 | printNicely('Sent: newest ' + str(len(rel)) + ' messages.') |
641 | for m in reversed(rel): | |
642 | print_message(m) | |
643 | printNicely('') | |
e2b81717 | 644 | |
305ce127 | 645 | |
646 | def trash(): | |
647 | """ | |
648 | Remove message | |
649 | """ | |
650 | t = Twitter(auth=authen()) | |
651 | try: | |
99b52f5f | 652 | id = int(g['stuff'].split()[0]) |
305ce127 | 653 | except: |
654 | printNicely(red('Sorry I can\'t understand.')) | |
99b52f5f | 655 | mid = c['message_dict'][id] |
b8c1f42a O |
656 | t.direct_messages.destroy(id=mid) |
657 | printNicely(green('Message deleted.')) | |
305ce127 | 658 | |
659 | ||
e2b81717 O |
660 | def whois(): |
661 | """ | |
662 | Show profile of a specific user | |
663 | """ | |
664 | t = Twitter(auth=authen()) | |
665 | screen_name = g['stuff'].split()[0] | |
b8c1f42a | 666 | if screen_name.startswith('@'): |
e2b81717 O |
667 | try: |
668 | user = t.users.show( | |
669 | screen_name=screen_name[1:], | |
670 | include_entities=False) | |
fe9bb33b | 671 | show_profile(user) |
e2b81717 O |
672 | except: |
673 | printNicely(red('Omg no user.')) | |
674 | else: | |
b8c1f42a | 675 | printNicely(red('A name should begin with a \'@\'')) |
e2b81717 O |
676 | |
677 | ||
f5677fb1 | 678 | def follow(): |
843647ad | 679 | """ |
f5677fb1 | 680 | Follow a user |
843647ad O |
681 | """ |
682 | t = Twitter(auth=authen()) | |
f5677fb1 | 683 | screen_name = g['stuff'].split()[0] |
b8c1f42a O |
684 | if screen_name.startswith('@'): |
685 | t.friendships.create(screen_name=screen_name[1:], follow=True) | |
686 | printNicely(green('You are following ' + screen_name + ' now!')) | |
f5677fb1 | 687 | else: |
b8c1f42a | 688 | printNicely(red('A name should begin with a \'@\'')) |
f5677fb1 O |
689 | |
690 | ||
691 | def unfollow(): | |
692 | """ | |
693 | Unfollow a user | |
694 | """ | |
695 | t = Twitter(auth=authen()) | |
696 | screen_name = g['stuff'].split()[0] | |
b8c1f42a O |
697 | if screen_name.startswith('@'): |
698 | t.friendships.destroy( | |
699 | screen_name=screen_name[1:], | |
700 | include_entities=False) | |
701 | printNicely(green('Unfollow ' + screen_name + ' success!')) | |
f5677fb1 | 702 | else: |
b8c1f42a | 703 | printNicely(red('A name should begin with a \'@\'')) |
843647ad O |
704 | |
705 | ||
5b2c4faf | 706 | def mute(): |
707 | """ | |
708 | Mute a user | |
709 | """ | |
710 | t = Twitter(auth=authen()) | |
711 | try: | |
712 | screen_name = g['stuff'].split()[0] | |
713 | except: | |
714 | printNicely(red('A name should be specified. ')) | |
715 | return | |
716 | if screen_name.startswith('@'): | |
e3927852 O |
717 | try: |
718 | rel = t.mutes.users.create(screen_name=screen_name[1:]) | |
719 | if isinstance(rel, dict): | |
720 | printNicely(green(screen_name + ' is muted.')) | |
612d6863 | 721 | c['IGNORE_LIST'] += [unc(screen_name)] |
e3927852 O |
722 | c['IGNORE_LIST'] = list(set(c['IGNORE_LIST'])) |
723 | else: | |
724 | printNicely(red(rel)) | |
725 | except: | |
726 | printNicely(red('Something is wrong, can not mute now :(')) | |
5b2c4faf | 727 | else: |
728 | printNicely(red('A name should begin with a \'@\'')) | |
729 | ||
730 | ||
731 | def unmute(): | |
732 | """ | |
733 | Unmute a user | |
734 | """ | |
735 | t = Twitter(auth=authen()) | |
736 | try: | |
737 | screen_name = g['stuff'].split()[0] | |
738 | except: | |
739 | printNicely(red('A name should be specified. ')) | |
740 | return | |
741 | if screen_name.startswith('@'): | |
e3927852 O |
742 | try: |
743 | rel = t.mutes.users.destroy(screen_name=screen_name[1:]) | |
744 | if isinstance(rel, dict): | |
745 | printNicely(green(screen_name + ' is unmuted.')) | |
746 | c['IGNORE_LIST'].remove(screen_name) | |
747 | else: | |
748 | printNicely(red(rel)) | |
749 | except: | |
750 | printNicely(red('Maybe you are not muting this person ?')) | |
5b2c4faf | 751 | else: |
752 | printNicely(red('A name should begin with a \'@\'')) | |
753 | ||
754 | ||
755 | def muting(): | |
756 | """ | |
757 | List muting user | |
758 | """ | |
e3927852 O |
759 | # Get dict of muting users |
760 | md = build_mute_dict(dict_data=True) | |
761 | printNicely('All: ' + str(len(md)) + ' people.') | |
762 | for name in md: | |
763 | user = ' ' + cycle_color(md[name]) | |
764 | user += color_func(c['TWEET']['nick'])(' ' + name + ' ') | |
5b2c4faf | 765 | printNicely(user) |
e3927852 O |
766 | # Update from Twitter |
767 | c['IGNORE_LIST'] = [n for n in md] | |
5b2c4faf | 768 | |
769 | ||
305ce127 | 770 | def block(): |
771 | """ | |
772 | Block a user | |
773 | """ | |
774 | t = Twitter(auth=authen()) | |
775 | screen_name = g['stuff'].split()[0] | |
b8c1f42a O |
776 | if screen_name.startswith('@'): |
777 | t.blocks.create( | |
5b2c4faf | 778 | screen_name=screen_name[1:], |
779 | include_entities=False, | |
780 | skip_status=True) | |
b8c1f42a | 781 | printNicely(green('You blocked ' + screen_name + '.')) |
305ce127 | 782 | else: |
b8c1f42a | 783 | printNicely(red('A name should begin with a \'@\'')) |
305ce127 | 784 | |
785 | ||
786 | def unblock(): | |
787 | """ | |
788 | Unblock a user | |
789 | """ | |
790 | t = Twitter(auth=authen()) | |
791 | screen_name = g['stuff'].split()[0] | |
b8c1f42a O |
792 | if screen_name.startswith('@'): |
793 | t.blocks.destroy( | |
794 | screen_name=screen_name[1:], | |
795 | include_entities=False, | |
796 | skip_status=True) | |
797 | printNicely(green('Unblock ' + screen_name + ' success!')) | |
305ce127 | 798 | else: |
b8c1f42a | 799 | printNicely(red('A name should begin with a \'@\'')) |
305ce127 | 800 | |
801 | ||
802 | def report(): | |
803 | """ | |
804 | Report a user as a spam account | |
805 | """ | |
806 | t = Twitter(auth=authen()) | |
807 | screen_name = g['stuff'].split()[0] | |
b8c1f42a O |
808 | if screen_name.startswith('@'): |
809 | t.users.report_spam( | |
810 | screen_name=screen_name[1:]) | |
811 | printNicely(green('You reported ' + screen_name + '.')) | |
305ce127 | 812 | else: |
813 | printNicely(red('Sorry I can\'t understand.')) | |
814 | ||
815 | ||
8b8566d1 O |
816 | def get_slug(): |
817 | """ | |
818 | Get Slug Decorator | |
819 | """ | |
a8c5fce4 | 820 | # Get list name |
8b8566d1 O |
821 | list_name = raw_input(light_magenta('Give me the list\'s name: ')) |
822 | # Get list name and owner | |
823 | try: | |
824 | owner, slug = list_name.split('/') | |
825 | if slug.startswith('@'): | |
826 | slug = slug[1:] | |
827 | return owner, slug | |
828 | except: | |
a8c5fce4 O |
829 | printNicely( |
830 | light_magenta('List name should follow "@owner/list_name" format.')) | |
8b8566d1 O |
831 | raise Exception('Wrong list name') |
832 | ||
833 | ||
2d341029 O |
834 | def show_lists(t): |
835 | """ | |
422dd385 | 836 | List list |
2d341029 O |
837 | """ |
838 | rel = t.lists.list(screen_name=g['original_name']) | |
839 | if rel: | |
840 | print_list(rel) | |
841 | else: | |
842 | printNicely(light_magenta('You belong to no lists :)')) | |
843 | ||
844 | ||
845 | def list_home(t): | |
846 | """ | |
847 | List home | |
848 | """ | |
8b8566d1 | 849 | owner, slug = get_slug() |
2d341029 | 850 | res = t.lists.statuses( |
422dd385 O |
851 | slug=slug, |
852 | owner_screen_name=owner, | |
853 | count=c['LIST_MAX'], | |
2d341029 O |
854 | include_entities=False) |
855 | for tweet in res: | |
856 | draw(t=tweet) | |
857 | printNicely('') | |
858 | ||
859 | ||
860 | def list_members(t): | |
861 | """ | |
862 | List members | |
863 | """ | |
8b8566d1 | 864 | owner, slug = get_slug() |
422dd385 | 865 | # Get members |
2d341029 O |
866 | rel = {} |
867 | next_cursor = -1 | |
422dd385 | 868 | while next_cursor != 0: |
2d341029 | 869 | m = t.lists.members( |
422dd385 O |
870 | slug=slug, |
871 | owner_screen_name=owner, | |
872 | cursor=next_cursor, | |
2d341029 O |
873 | include_entities=False) |
874 | for u in m['users']: | |
875 | rel[u['name']] = '@' + u['screen_name'] | |
876 | next_cursor = m['next_cursor'] | |
877 | printNicely('All: ' + str(len(rel)) + ' members.') | |
878 | for name in rel: | |
879 | user = ' ' + cycle_color(name) | |
422dd385 | 880 | user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') |
2d341029 O |
881 | printNicely(user) |
882 | ||
883 | ||
884 | def list_subscribers(t): | |
885 | """ | |
886 | List subscribers | |
887 | """ | |
8b8566d1 | 888 | owner, slug = get_slug() |
422dd385 | 889 | # Get subscribers |
2d341029 O |
890 | rel = {} |
891 | next_cursor = -1 | |
422dd385 | 892 | while next_cursor != 0: |
2d341029 | 893 | m = t.lists.subscribers( |
422dd385 O |
894 | slug=slug, |
895 | owner_screen_name=owner, | |
896 | cursor=next_cursor, | |
2d341029 O |
897 | include_entities=False) |
898 | for u in m['users']: | |
899 | rel[u['name']] = '@' + u['screen_name'] | |
900 | next_cursor = m['next_cursor'] | |
901 | printNicely('All: ' + str(len(rel)) + ' subscribers.') | |
902 | for name in rel: | |
903 | user = ' ' + cycle_color(name) | |
422dd385 | 904 | user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ') |
2d341029 O |
905 | printNicely(user) |
906 | ||
907 | ||
422dd385 O |
908 | def list_add(t): |
909 | """ | |
910 | Add specific user to a list | |
911 | """ | |
8b8566d1 | 912 | owner, slug = get_slug() |
422dd385 O |
913 | # Add |
914 | user_name = raw_input(light_magenta('Give me name of the newbie: ')) | |
915 | if user_name.startswith('@'): | |
916 | user_name = user_name[1:] | |
917 | try: | |
918 | t.lists.members.create( | |
919 | slug=slug, | |
920 | owner_screen_name=owner, | |
921 | screen_name=user_name) | |
d6cc4c67 | 922 | printNicely(green('Added.')) |
422dd385 O |
923 | except: |
924 | printNicely(light_magenta('I\'m sorry we can not add him/her.')) | |
925 | ||
926 | ||
2d341029 O |
927 | def list_remove(t): |
928 | """ | |
929 | Remove specific user from a list | |
930 | """ | |
8b8566d1 | 931 | owner, slug = get_slug() |
2d341029 | 932 | # Remove |
422dd385 O |
933 | user_name = raw_input(light_magenta('Give me name of the unlucky one: ')) |
934 | if user_name.startswith('@'): | |
935 | user_name = user_name[1:] | |
2d341029 O |
936 | try: |
937 | t.lists.members.destroy( | |
422dd385 O |
938 | slug=slug, |
939 | owner_screen_name=owner, | |
940 | screen_name=user_name) | |
d6cc4c67 | 941 | printNicely(green('Gone.')) |
422dd385 O |
942 | except: |
943 | printNicely(light_magenta('I\'m sorry we can not remove him/her.')) | |
944 | ||
945 | ||
946 | def list_subscribe(t): | |
947 | """ | |
948 | Subscribe to a list | |
949 | """ | |
8b8566d1 | 950 | owner, slug = get_slug() |
422dd385 O |
951 | # Subscribe |
952 | try: | |
953 | t.lists.subscribers.create( | |
954 | slug=slug, | |
955 | owner_screen_name=owner) | |
d6cc4c67 | 956 | printNicely(green('Done.')) |
422dd385 O |
957 | except: |
958 | printNicely( | |
959 | light_magenta('I\'m sorry you can not subscribe to this list.')) | |
960 | ||
961 | ||
962 | def list_unsubscribe(t): | |
963 | """ | |
964 | Unsubscribe a list | |
965 | """ | |
8b8566d1 | 966 | owner, slug = get_slug() |
422dd385 O |
967 | # Subscribe |
968 | try: | |
969 | t.lists.subscribers.destroy( | |
970 | slug=slug, | |
971 | owner_screen_name=owner) | |
d6cc4c67 | 972 | printNicely(green('Done.')) |
422dd385 O |
973 | except: |
974 | printNicely( | |
975 | light_magenta('I\'m sorry you can not unsubscribe to this list.')) | |
976 | ||
977 | ||
978 | def list_own(t): | |
979 | """ | |
980 | List own | |
981 | """ | |
982 | rel = [] | |
983 | next_cursor = -1 | |
984 | while next_cursor != 0: | |
985 | res = t.lists.ownerships( | |
986 | screen_name=g['original_name'], | |
987 | cursor=next_cursor) | |
988 | rel += res['lists'] | |
989 | next_cursor = res['next_cursor'] | |
990 | if rel: | |
991 | print_list(rel) | |
992 | else: | |
993 | printNicely(light_magenta('You own no lists :)')) | |
994 | ||
995 | ||
996 | def list_new(t): | |
997 | """ | |
998 | Create a new list | |
999 | """ | |
1000 | name = raw_input(light_magenta('New list\'s name: ')) | |
1001 | mode = raw_input(light_magenta('New list\'s mode (public/private): ')) | |
1002 | description = raw_input(light_magenta('New list\'s description: ')) | |
1003 | try: | |
1004 | t.lists.create( | |
1005 | name=name, | |
1006 | mode=mode, | |
1007 | description=description) | |
d6cc4c67 | 1008 | printNicely(green(name + ' list is created.')) |
422dd385 O |
1009 | except: |
1010 | printNicely(red('Oops something is wrong with Twitter :(')) | |
1011 | ||
1012 | ||
1013 | def list_update(t): | |
1014 | """ | |
1015 | Update a list | |
1016 | """ | |
1017 | slug = raw_input(light_magenta('Your list that you want to update: ')) | |
1018 | name = raw_input(light_magenta('Update name (leave blank to unchange): ')) | |
1019 | mode = raw_input(light_magenta('Update mode (public/private): ')) | |
1020 | description = raw_input(light_magenta('Update description: ')) | |
1021 | try: | |
1022 | if name: | |
1023 | t.lists.update( | |
1024 | slug='-'.join(slug.split()), | |
1025 | owner_screen_name=g['original_name'], | |
1026 | name=name, | |
1027 | mode=mode, | |
1028 | description=description) | |
1029 | else: | |
1030 | t.lists.update( | |
1031 | slug=slug, | |
1032 | owner_screen_name=g['original_name'], | |
1033 | mode=mode, | |
1034 | description=description) | |
d6cc4c67 | 1035 | printNicely(green(slug + ' list is updated.')) |
3c85d8fc | 1036 | except: |
422dd385 O |
1037 | printNicely(red('Oops something is wrong with Twitter :(')) |
1038 | ||
1039 | ||
1040 | def list_delete(t): | |
1041 | """ | |
1042 | Delete a list | |
1043 | """ | |
8b3456f9 | 1044 | slug = raw_input(light_magenta('Your list that you want to delete: ')) |
422dd385 O |
1045 | try: |
1046 | t.lists.destroy( | |
1047 | slug='-'.join(slug.split()), | |
1048 | owner_screen_name=g['original_name']) | |
d6cc4c67 | 1049 | printNicely(green(slug + ' list is deleted.')) |
2d341029 | 1050 | except: |
422dd385 | 1051 | printNicely(red('Oops something is wrong with Twitter :(')) |
2d341029 O |
1052 | |
1053 | ||
e3927852 | 1054 | def twitterlist(): |
2d341029 O |
1055 | """ |
1056 | Twitter's list | |
1057 | """ | |
1058 | t = Twitter(auth=authen()) | |
1059 | # List all lists or base on action | |
1060 | try: | |
1061 | g['list_action'] = g['stuff'].split()[0] | |
1062 | except: | |
1063 | show_lists(t) | |
1064 | return | |
422dd385 | 1065 | # Sub-function |
2d341029 O |
1066 | action_ary = { |
1067 | 'home': list_home, | |
1068 | 'all_mem': list_members, | |
1069 | 'all_sub': list_subscribers, | |
422dd385 | 1070 | 'add': list_add, |
2d341029 | 1071 | 'rm': list_remove, |
422dd385 O |
1072 | 'sub': list_subscribe, |
1073 | 'unsub': list_unsubscribe, | |
1074 | 'own': list_own, | |
1075 | 'new': list_new, | |
1076 | 'update': list_update, | |
1077 | 'del': list_delete, | |
2d341029 O |
1078 | } |
1079 | try: | |
1080 | return action_ary[g['list_action']](t) | |
3c85d8fc | 1081 | except: |
8b8566d1 | 1082 | printNicely(red('Please try again.')) |
2d341029 O |
1083 | |
1084 | ||
813a5d80 | 1085 | def cal(): |
1086 | """ | |
1087 | Unix's command `cal` | |
1088 | """ | |
1089 | # Format | |
1090 | rel = os.popen('cal').read().split('\n') | |
1091 | month = rel.pop(0) | |
813a5d80 | 1092 | date = rel.pop(0) |
2a0cabee | 1093 | show_calendar(month, date, rel) |
813a5d80 | 1094 | |
1095 | ||
29fd0be6 O |
1096 | def config(): |
1097 | """ | |
1098 | Browse and change config | |
1099 | """ | |
1100 | all_config = get_all_config() | |
1101 | g['stuff'] = g['stuff'].strip() | |
1102 | # List all config | |
1103 | if not g['stuff']: | |
1104 | for k in all_config: | |
a8c5fce4 | 1105 | line = ' ' * 2 + \ |
d6cc4c67 | 1106 | green(k) + ': ' + light_yellow(str(all_config[k])) |
29fd0be6 O |
1107 | printNicely(line) |
1108 | guide = 'Detailed explanation can be found at ' + \ | |
a8c5fce4 O |
1109 | color_func(c['TWEET']['link'])( |
1110 | 'http://rainbowstream.readthedocs.org/en/latest/#config-explanation') | |
29fd0be6 O |
1111 | printNicely(guide) |
1112 | # Print specific config | |
1113 | elif len(g['stuff'].split()) == 1: | |
1114 | if g['stuff'] in all_config: | |
1115 | k = g['stuff'] | |
a8c5fce4 | 1116 | line = ' ' * 2 + \ |
d6cc4c67 | 1117 | green(k) + ': ' + light_yellow(str(all_config[k])) |
29fd0be6 O |
1118 | printNicely(line) |
1119 | else: | |
fe9bb33b | 1120 | printNicely(red('No such config key.')) |
29fd0be6 O |
1121 | # Print specific config's default value |
1122 | elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'default': | |
1123 | key = g['stuff'].split()[0] | |
fe9bb33b | 1124 | try: |
1125 | value = get_default_config(key) | |
d6cc4c67 | 1126 | line = ' ' * 2 + green(key) + ': ' + light_magenta(value) |
fe9bb33b | 1127 | printNicely(line) |
a8e71259 | 1128 | except Exception as e: |
1129 | printNicely(red(e)) | |
fe9bb33b | 1130 | # Delete specific config key in config file |
1131 | elif len(g['stuff'].split()) == 2 and g['stuff'].split()[-1] == 'drop': | |
1132 | key = g['stuff'].split()[0] | |
1133 | try: | |
1134 | delete_config(key) | |
d6cc4c67 | 1135 | printNicely(green('Config key is dropped.')) |
a8e71259 | 1136 | except Exception as e: |
1137 | printNicely(red(e)) | |
29fd0be6 | 1138 | # Set specific config |
a8c5fce4 | 1139 | elif len(g['stuff'].split()) == 3 and g['stuff'].split()[1] == '=': |
29fd0be6 O |
1140 | key = g['stuff'].split()[0] |
1141 | value = g['stuff'].split()[-1] | |
ceec8593 | 1142 | if key == 'THEME' and not validate_theme(value): |
1143 | printNicely(red('Invalid theme\'s value.')) | |
1144 | return | |
3c01ba57 | 1145 | try: |
a8c5fce4 | 1146 | set_config(key, value) |
ceec8593 | 1147 | # Apply theme immediately |
1148 | if key == 'THEME': | |
baec5f50 | 1149 | c['THEME'] = reload_theme(value, c['THEME']) |
ceec8593 | 1150 | g['decorated_name'] = lambda x: color_func( |
a8e71259 | 1151 | c['DECORATED_NAME'])('[' + x + ']: ') |
1152 | reload_config() | |
d6cc4c67 | 1153 | printNicely(green('Updated successfully.')) |
a8e71259 | 1154 | except Exception as e: |
1155 | printNicely(red(e)) | |
29fd0be6 O |
1156 | else: |
1157 | printNicely(light_magenta('Sorry I can\'s understand.')) | |
1158 | ||
1159 | ||
632c6fa5 O |
1160 | def theme(): |
1161 | """ | |
1162 | List and change theme | |
1163 | """ | |
1164 | if not g['stuff']: | |
1165 | # List themes | |
1166 | for theme in g['themes']: | |
1f2f6159 O |
1167 | line = light_magenta(theme) |
1168 | if c['THEME'] == theme: | |
422dd385 | 1169 | line = ' ' * 2 + light_yellow('* ') + line |
ddb1e615 | 1170 | else: |
422dd385 | 1171 | line = ' ' * 4 + line |
632c6fa5 O |
1172 | printNicely(line) |
1173 | else: | |
1174 | # Change theme | |
c075e6dc | 1175 | try: |
ceec8593 | 1176 | # Load new theme |
baec5f50 | 1177 | c['THEME'] = reload_theme(g['stuff'], c['THEME']) |
ceec8593 | 1178 | # Redefine decorated_name |
1179 | g['decorated_name'] = lambda x: color_func( | |
c075e6dc | 1180 | c['DECORATED_NAME'])( |
ceec8593 | 1181 | '[' + x + ']: ') |
632c6fa5 | 1182 | printNicely(green('Theme changed.')) |
7f4a813a | 1183 | except: |
1f2f6159 | 1184 | printNicely(red('No such theme exists.')) |
632c6fa5 O |
1185 | |
1186 | ||
2d341029 | 1187 | def help_discover(): |
f405a7d0 | 1188 | """ |
2d341029 | 1189 | Discover the world |
f405a7d0 | 1190 | """ |
7e4ccbf3 | 1191 | s = ' ' * 2 |
1f24a05a | 1192 | # Discover the world |
2d341029 | 1193 | usage = '\n' |
8bc30efd | 1194 | usage += s + grey(u'\u266A' + ' Discover the world \n') |
c075e6dc O |
1195 | usage += s * 2 + light_green('trend') + ' will show global trending topics. ' + \ |
1196 | 'You can try ' + light_green('trend US') + ' or ' + \ | |
1197 | light_green('trend JP Tokyo') + '.\n' | |
1198 | usage += s * 2 + light_green('home') + ' will show your timeline. ' + \ | |
1199 | light_green('home 7') + ' will show 7 tweets.\n' | |
1200 | usage += s * 2 + light_green('mentions') + ' will show mentions timeline. ' + \ | |
1201 | light_green('mentions 7') + ' will show 7 mention tweets.\n' | |
2d0ad040 J |
1202 | usage += s * 2 + light_green('conversation 12') + ' will show the chain of replies prior to the tweet with ' + \ |
1203 | light_yellow('[id=12]') + '.\n' | |
c075e6dc | 1204 | usage += s * 2 + light_green('whois @mdo') + ' will show profile of ' + \ |
8bc30efd | 1205 | magenta('@mdo') + '.\n' |
c075e6dc | 1206 | usage += s * 2 + light_green('view @mdo') + \ |
8bc30efd | 1207 | ' will show ' + magenta('@mdo') + '\'s home.\n' |
03e08f86 O |
1208 | usage += s * 2 + light_green('s AKB48') + ' will search for "' + \ |
1209 | light_yellow('AKB48') + '" and return 5 newest tweet. ' + \ | |
1210 | 'Search can be performed with or without hashtag.\n' | |
2d341029 O |
1211 | printNicely(usage) |
1212 | ||
8bc30efd | 1213 | |
2d341029 O |
1214 | def help_tweets(): |
1215 | """ | |
1216 | Tweets | |
1217 | """ | |
1218 | s = ' ' * 2 | |
1f24a05a | 1219 | # Tweet |
2d341029 | 1220 | usage = '\n' |
8bc30efd | 1221 | usage += s + grey(u'\u266A' + ' Tweets \n') |
c075e6dc O |
1222 | usage += s * 2 + light_green('t oops ') + \ |
1223 | 'will tweet "' + light_yellow('oops') + '" immediately.\n' | |
7e4ccbf3 | 1224 | usage += s * 2 + \ |
c075e6dc O |
1225 | light_green('rt 12 ') + ' will retweet to tweet with ' + \ |
1226 | light_yellow('[id=12]') + '.\n' | |
80b70d60 O |
1227 | usage += s * 2 + \ |
1228 | light_green('quote 12 ') + ' will quote the tweet with ' + \ | |
1229 | light_yellow('[id=12]') + '. If no extra text is added, ' + \ | |
1230 | 'the quote will be canceled.\n' | |
1f24a05a | 1231 | usage += s * 2 + \ |
c075e6dc O |
1232 | light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \ |
1233 | light_yellow('[id=12]') + '.\n' | |
1234 | usage += s * 2 + light_green('rep 12 oops') + ' will reply "' + \ | |
1235 | light_yellow('oops') + '" to tweet with ' + \ | |
1236 | light_yellow('[id=12]') + '.\n' | |
7e4ccbf3 | 1237 | usage += s * 2 + \ |
c075e6dc O |
1238 | light_green('fav 12 ') + ' will favorite the tweet with ' + \ |
1239 | light_yellow('[id=12]') + '.\n' | |
7e4ccbf3 | 1240 | usage += s * 2 + \ |
c075e6dc O |
1241 | light_green('ufav 12 ') + ' will unfavorite tweet with ' + \ |
1242 | light_yellow('[id=12]') + '.\n' | |
8bc30efd | 1243 | usage += s * 2 + \ |
c075e6dc O |
1244 | light_green('del 12 ') + ' will delete tweet with ' + \ |
1245 | light_yellow('[id=12]') + '.\n' | |
1246 | usage += s * 2 + light_green('show image 12') + ' will show image in tweet with ' + \ | |
1247 | light_yellow('[id=12]') + ' in your OS\'s image viewer.\n' | |
80b70d60 O |
1248 | usage += s * 2 + light_green('open 12') + ' will open url in tweet with ' + \ |
1249 | light_yellow('[id=12]') + ' in your OS\'s default browser.\n' | |
2d341029 | 1250 | printNicely(usage) |
8bc30efd | 1251 | |
2d341029 O |
1252 | |
1253 | def help_messages(): | |
1254 | """ | |
1255 | Messages | |
1256 | """ | |
1257 | s = ' ' * 2 | |
5b2c4faf | 1258 | # Direct message |
2d341029 | 1259 | usage = '\n' |
8bc30efd | 1260 | usage += s + grey(u'\u266A' + ' Direct messages \n') |
c075e6dc O |
1261 | usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \ |
1262 | light_green('inbox 7') + ' will show newest 7 messages.\n' | |
1263 | usage += s * 2 + light_green('sent') + ' will show sent messages. ' + \ | |
1264 | light_green('sent 7') + ' will show newest 7 messages.\n' | |
1265 | usage += s * 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \ | |
8bc30efd | 1266 | magenta('@dtvd88') + '.\n' |
c075e6dc O |
1267 | usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \ |
1268 | light_yellow('[message_id=5]') + '.\n' | |
2d341029 | 1269 | printNicely(usage) |
8bc30efd | 1270 | |
2d341029 O |
1271 | |
1272 | def help_friends_and_followers(): | |
1273 | """ | |
1274 | Friends and Followers | |
1275 | """ | |
1276 | s = ' ' * 2 | |
8bc30efd | 1277 | # Follower and following |
2d341029 | 1278 | usage = '\n' |
cdccb0d6 | 1279 | usage += s + grey(u'\u266A' + ' Friends and followers \n') |
8bc30efd | 1280 | usage += s * 2 + \ |
c075e6dc | 1281 | light_green('ls fl') + \ |
8bc30efd | 1282 | ' will list all followers (people who are following you).\n' |
1283 | usage += s * 2 + \ | |
c075e6dc | 1284 | light_green('ls fr') + \ |
8bc30efd | 1285 | ' will list all friends (people who you are following).\n' |
c075e6dc | 1286 | usage += s * 2 + light_green('fl @dtvd88') + ' will follow ' + \ |
305ce127 | 1287 | magenta('@dtvd88') + '.\n' |
c075e6dc | 1288 | usage += s * 2 + light_green('ufl @dtvd88') + ' will unfollow ' + \ |
305ce127 | 1289 | magenta('@dtvd88') + '.\n' |
c075e6dc | 1290 | usage += s * 2 + light_green('mute @dtvd88') + ' will mute ' + \ |
5b2c4faf | 1291 | magenta('@dtvd88') + '.\n' |
c075e6dc | 1292 | usage += s * 2 + light_green('unmute @dtvd88') + ' will unmute ' + \ |
5b2c4faf | 1293 | magenta('@dtvd88') + '.\n' |
c075e6dc O |
1294 | usage += s * 2 + light_green('muting') + ' will list muting users.\n' |
1295 | usage += s * 2 + light_green('block @dtvd88') + ' will block ' + \ | |
305ce127 | 1296 | magenta('@dtvd88') + '.\n' |
c075e6dc | 1297 | usage += s * 2 + light_green('unblock @dtvd88') + ' will unblock ' + \ |
305ce127 | 1298 | magenta('@dtvd88') + '.\n' |
c075e6dc | 1299 | usage += s * 2 + light_green('report @dtvd88') + ' will report ' + \ |
305ce127 | 1300 | magenta('@dtvd88') + ' as a spam account.\n' |
2d341029 O |
1301 | printNicely(usage) |
1302 | ||
1303 | ||
1304 | def help_list(): | |
1305 | """ | |
1306 | Lists | |
1307 | """ | |
1308 | s = ' ' * 2 | |
1309 | # Twitter list | |
1310 | usage = '\n' | |
1311 | usage += s + grey(u'\u266A' + ' Twitter list\n') | |
1312 | usage += s * 2 + light_green('list') + \ | |
1313 | ' will show all lists you are belong to.\n' | |
1314 | usage += s * 2 + light_green('list home') + \ | |
bef33491 | 1315 | ' will show timeline of list. You will be asked for list\'s name.\n' |
a65bd34c | 1316 | usage += s * 2 + light_green('list all_mem') + \ |
2d341029 | 1317 | ' will show list\'s all members.\n' |
a65bd34c | 1318 | usage += s * 2 + light_green('list all_sub') + \ |
2d341029 | 1319 | ' will show list\'s all subscribers.\n' |
422dd385 O |
1320 | usage += s * 2 + light_green('list add') + \ |
1321 | ' will add specific person to a list owned by you.' + \ | |
1322 | ' You will be asked for list\'s name and person\'s name.\n' | |
2d341029 O |
1323 | usage += s * 2 + light_green('list rm') + \ |
1324 | ' will remove specific person from a list owned by you.' + \ | |
1325 | ' You will be asked for list\'s name and person\'s name.\n' | |
422dd385 O |
1326 | usage += s * 2 + light_green('list sub') + \ |
1327 | ' will subscribe you to a specific list.\n' | |
1328 | usage += s * 2 + light_green('list unsub') + \ | |
1329 | ' will unsubscribe you from a specific list.\n' | |
1330 | usage += s * 2 + light_green('list own') + \ | |
1331 | ' will show all list owned by you.\n' | |
1332 | usage += s * 2 + light_green('list new') + \ | |
1333 | ' will create a new list.\n' | |
1334 | usage += s * 2 + light_green('list update') + \ | |
1335 | ' will update a list owned by you.\n' | |
1336 | usage += s * 2 + light_green('list del') + \ | |
1337 | ' will delete a list owned by you.\n' | |
2d341029 | 1338 | printNicely(usage) |
8bc30efd | 1339 | |
2d341029 O |
1340 | |
1341 | def help_stream(): | |
1342 | """ | |
1343 | Stream switch | |
1344 | """ | |
1345 | s = ' ' * 2 | |
8bc30efd | 1346 | # Switch |
2d341029 | 1347 | usage = '\n' |
8bc30efd | 1348 | usage += s + grey(u'\u266A' + ' Switching streams \n') |
c075e6dc | 1349 | usage += s * 2 + light_green('switch public #AKB') + \ |
48a25fe8 | 1350 | ' will switch to public stream and follow "' + \ |
c075e6dc O |
1351 | light_yellow('AKB') + '" keyword.\n' |
1352 | usage += s * 2 + light_green('switch mine') + \ | |
48a25fe8 | 1353 | ' will switch to your personal stream.\n' |
c075e6dc | 1354 | usage += s * 2 + light_green('switch mine -f ') + \ |
48a25fe8 | 1355 | ' will prompt to enter the filter.\n' |
c075e6dc | 1356 | usage += s * 3 + light_yellow('Only nicks') + \ |
48a25fe8 | 1357 | ' filter will decide nicks will be INCLUDE ONLY.\n' |
c075e6dc | 1358 | usage += s * 3 + light_yellow('Ignore nicks') + \ |
48a25fe8 | 1359 | ' filter will decide nicks will be EXCLUDE.\n' |
c075e6dc | 1360 | usage += s * 2 + light_green('switch mine -d') + \ |
48a25fe8 | 1361 | ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n' |
2d341029 O |
1362 | printNicely(usage) |
1363 | ||
1364 | ||
1365 | def help(): | |
1366 | """ | |
1367 | Help | |
1368 | """ | |
1369 | s = ' ' * 2 | |
1370 | h, w = os.popen('stty size', 'r').read().split() | |
2d341029 O |
1371 | # Start |
1372 | usage = '\n' | |
1373 | usage += s + 'Hi boss! I\'m ready to serve you right now!\n' | |
1374 | usage += s + '-' * (int(w) - 4) + '\n' | |
1375 | usage += s + 'You are ' + \ | |
1376 | light_yellow('already') + ' on your personal stream.\n' | |
1377 | usage += s + 'Any update from Twitter will show up ' + \ | |
1378 | light_yellow('immediately') + '.\n' | |
37d1047f | 1379 | usage += s + 'In addition, following commands are available right now:\n' |
2d341029 O |
1380 | # Twitter help section |
1381 | usage += '\n' | |
1382 | usage += s + grey(u'\u266A' + ' Twitter help\n') | |
1383 | usage += s * 2 + light_green('h discover') + \ | |
1384 | ' will show help for discover commands.\n' | |
1385 | usage += s * 2 + light_green('h tweets') + \ | |
1386 | ' will show help for tweets commands.\n' | |
1387 | usage += s * 2 + light_green('h messages') + \ | |
1388 | ' will show help for messages commands.\n' | |
1389 | usage += s * 2 + light_green('h friends_and_followers') + \ | |
1390 | ' will show help for friends and followers commands.\n' | |
1391 | usage += s * 2 + light_green('h list') + \ | |
1392 | ' will show help for list commands.\n' | |
1393 | usage += s * 2 + light_green('h stream') + \ | |
1394 | ' will show help for stream commands.\n' | |
1f24a05a | 1395 | # Smart shell |
1396 | usage += '\n' | |
1397 | usage += s + grey(u'\u266A' + ' Smart shell\n') | |
c075e6dc | 1398 | usage += s * 2 + light_green('111111 * 9 / 7') + ' or any math expression ' + \ |
1f24a05a | 1399 | 'will be evaluate by Python interpreter.\n' |
c075e6dc | 1400 | usage += s * 2 + 'Even ' + light_green('cal') + ' will show the calendar' + \ |
1f24a05a | 1401 | ' for current month.\n' |
29fd0be6 | 1402 | # Config |
1f24a05a | 1403 | usage += '\n' |
29fd0be6 O |
1404 | usage += s + grey(u'\u266A' + ' Config \n') |
1405 | usage += s * 2 + light_green('theme') + ' will list available theme. ' + \ | |
c075e6dc | 1406 | light_green('theme monokai') + ' will apply ' + light_yellow('monokai') + \ |
632c6fa5 | 1407 | ' theme immediately.\n' |
29fd0be6 O |
1408 | usage += s * 2 + light_green('config') + ' will list all config.\n' |
1409 | usage += s * 3 + \ | |
1410 | light_green('config ASCII_ART') + ' will output current value of ' +\ | |
a8c5fce4 | 1411 | light_yellow('ASCII_ART') + ' config key.\n' |
29fd0be6 | 1412 | usage += s * 3 + \ |
fe9bb33b | 1413 | light_green('config TREND_MAX default') + ' will output default value of ' + \ |
1414 | light_yellow('TREND_MAX') + ' config key.\n' | |
1415 | usage += s * 3 + \ | |
1416 | light_green('config CUSTOM_CONFIG drop') + ' will drop ' + \ | |
1417 | light_yellow('CUSTOM_CONFIG') + ' config key.\n' | |
29fd0be6 | 1418 | usage += s * 3 + \ |
fe9bb33b | 1419 | light_green('config IMAGE_ON_TERM = true') + ' will set value of ' + \ |
1420 | light_yellow('IMAGE_ON_TERM') + ' config key to ' + \ | |
1421 | light_yellow('True') + '.\n' | |
29fd0be6 O |
1422 | # Screening |
1423 | usage += '\n' | |
1424 | usage += s + grey(u'\u266A' + ' Screening \n') | |
c075e6dc | 1425 | usage += s * 2 + light_green('h') + ' will show this help again.\n' |
d6cc4c67 O |
1426 | usage += s * 2 + light_green('p') + ' will pause the stream.\n' |
1427 | usage += s * 2 + light_green('r') + ' will unpause the stream.\n' | |
c075e6dc O |
1428 | usage += s * 2 + light_green('c') + ' will clear the screen.\n' |
1429 | usage += s * 2 + light_green('q') + ' will quit.\n' | |
8bc30efd | 1430 | # End |
1431 | usage += '\n' | |
7e4ccbf3 | 1432 | usage += s + '-' * (int(w) - 4) + '\n' |
8bc30efd | 1433 | usage += s + 'Have fun and hang tight! \n' |
2d341029 O |
1434 | # Show help |
1435 | d = { | |
422dd385 O |
1436 | 'discover': help_discover, |
1437 | 'tweets': help_tweets, | |
1438 | 'messages': help_messages, | |
1439 | 'friends_and_followers': help_friends_and_followers, | |
1440 | 'list': help_list, | |
1441 | 'stream': help_stream, | |
2d341029 O |
1442 | } |
1443 | if g['stuff']: | |
baec5f50 | 1444 | d.get( |
1445 | g['stuff'].strip(), | |
1446 | lambda: printNicely(red('No such command.')) | |
3d48702f | 1447 | )() |
2d341029 O |
1448 | else: |
1449 | printNicely(usage) | |
f405a7d0 O |
1450 | |
1451 | ||
d6cc4c67 O |
1452 | def pause(): |
1453 | """ | |
1454 | Pause stream display | |
1455 | """ | |
99b52f5f | 1456 | c['pause'] = True |
d6cc4c67 O |
1457 | printNicely(green('Stream is paused')) |
1458 | ||
1459 | ||
1460 | def replay(): | |
1461 | """ | |
1462 | Replay stream | |
1463 | """ | |
99b52f5f | 1464 | c['pause'] = False |
d6cc4c67 O |
1465 | printNicely(green('Stream is running back now')) |
1466 | ||
1467 | ||
843647ad | 1468 | def clear(): |
f405a7d0 | 1469 | """ |
7b674cef | 1470 | Clear screen |
f405a7d0 | 1471 | """ |
843647ad | 1472 | os.system('clear') |
f405a7d0 O |
1473 | |
1474 | ||
843647ad | 1475 | def quit(): |
b8dda704 O |
1476 | """ |
1477 | Exit all | |
1478 | """ | |
4c025026 | 1479 | try: |
1480 | save_history() | |
4c025026 | 1481 | printNicely(green('See you next time :)')) |
1482 | except: | |
1483 | pass | |
843647ad | 1484 | sys.exit() |
b8dda704 O |
1485 | |
1486 | ||
94a5f62e | 1487 | def reset(): |
f405a7d0 | 1488 | """ |
94a5f62e | 1489 | Reset prefix of line |
f405a7d0 | 1490 | """ |
c91f75f2 | 1491 | if g['reset']: |
a8e71259 | 1492 | if c.get('USER_JSON_ERROR'): |
1493 | printNicely(red('Your ~/.rainbow_config.json is messed up:')) | |
1494 | printNicely(red('>>> ' + c['USER_JSON_ERROR'])) | |
1495 | printNicely('') | |
e3885f55 | 1496 | printNicely(magenta('Need tips ? Type "h" and hit Enter key!')) |
c91f75f2 | 1497 | g['reset'] = False |
d0a726d6 | 1498 | try: |
779b0640 | 1499 | printNicely(str(eval(g['cmd']))) |
2a0cabee | 1500 | except Exception: |
d0a726d6 | 1501 | pass |
54277114 O |
1502 | |
1503 | ||
f1c1dfea O |
1504 | # Command set |
1505 | cmdset = [ | |
1506 | 'switch', | |
1507 | 'trend', | |
1508 | 'home', | |
1509 | 'view', | |
1510 | 'mentions', | |
2d0ad040 | 1511 | 'conversation', |
f1c1dfea O |
1512 | 't', |
1513 | 'rt', | |
1514 | 'quote', | |
1515 | 'allrt', | |
1516 | 'fav', | |
1517 | 'rep', | |
1518 | 'del', | |
1519 | 'ufav', | |
1520 | 's', | |
1521 | 'mes', | |
1522 | 'show', | |
1523 | 'open', | |
1524 | 'ls', | |
1525 | 'inbox', | |
1526 | 'sent', | |
1527 | 'trash', | |
1528 | 'whois', | |
1529 | 'fl', | |
1530 | 'ufl', | |
1531 | 'mute', | |
1532 | 'unmute', | |
1533 | 'muting', | |
1534 | 'block', | |
1535 | 'unblock', | |
1536 | 'report', | |
1537 | 'list', | |
1538 | 'cal', | |
1539 | 'config', | |
1540 | 'theme', | |
1541 | 'h', | |
1542 | 'p', | |
1543 | 'r', | |
1544 | 'c', | |
1545 | 'q' | |
1546 | ] | |
1547 | ||
1548 | # Handle function set | |
1549 | funcset = [ | |
1550 | switch, | |
1551 | trend, | |
1552 | home, | |
1553 | view, | |
1554 | mentions, | |
2d0ad040 | 1555 | conversation, |
f1c1dfea O |
1556 | tweet, |
1557 | retweet, | |
1558 | quote, | |
1559 | allretweet, | |
1560 | favorite, | |
1561 | reply, | |
1562 | delete, | |
1563 | unfavorite, | |
1564 | search, | |
1565 | message, | |
1566 | show, | |
1567 | urlopen, | |
1568 | ls, | |
1569 | inbox, | |
1570 | sent, | |
1571 | trash, | |
1572 | whois, | |
1573 | follow, | |
1574 | unfollow, | |
1575 | mute, | |
1576 | unmute, | |
1577 | muting, | |
1578 | block, | |
1579 | unblock, | |
1580 | report, | |
1581 | twitterlist, | |
1582 | cal, | |
1583 | config, | |
1584 | theme, | |
1585 | help, | |
1586 | pause, | |
1587 | replay, | |
1588 | clear, | |
1589 | quit | |
1590 | ] | |
1591 | ||
1592 | ||
94a5f62e | 1593 | def process(cmd): |
54277114 | 1594 | """ |
94a5f62e | 1595 | Process switch |
54277114 | 1596 | """ |
f1c1dfea | 1597 | return dict(zip(cmdset, funcset)).get(cmd, reset) |
94a5f62e | 1598 | |
1599 | ||
1600 | def listen(): | |
42fde775 | 1601 | """ |
1602 | Listen to user's input | |
1603 | """ | |
d51b4107 O |
1604 | d = dict(zip( |
1605 | cmdset, | |
1606 | [ | |
affcb149 | 1607 | ['public', 'mine'], # switch |
4592d231 | 1608 | [], # trend |
7e4ccbf3 | 1609 | [], # home |
1610 | ['@'], # view | |
305ce127 | 1611 | [], # mentions |
2d0ad040 | 1612 | [], # conversation |
7e4ccbf3 | 1613 | [], # tweet |
1614 | [], # retweet | |
80b70d60 | 1615 | [], # quote |
1f24a05a | 1616 | [], # allretweet |
f5677fb1 | 1617 | [], # favorite |
7e4ccbf3 | 1618 | [], # reply |
1619 | [], # delete | |
f5677fb1 | 1620 | [], # unfavorite |
7e4ccbf3 | 1621 | ['#'], # search |
305ce127 | 1622 | ['@'], # message |
f5677fb1 | 1623 | ['image'], # show image |
80b70d60 | 1624 | [''], # open url |
305ce127 | 1625 | ['fl', 'fr'], # list |
1626 | [], # inbox | |
1627 | [], # sent | |
1628 | [], # trash | |
e2b81717 | 1629 | ['@'], # whois |
affcb149 O |
1630 | ['@'], # follow |
1631 | ['@'], # unfollow | |
5b2c4faf | 1632 | ['@'], # mute |
1633 | ['@'], # unmute | |
1634 | ['@'], # muting | |
305ce127 | 1635 | ['@'], # block |
1636 | ['@'], # unblock | |
1637 | ['@'], # report | |
422dd385 O |
1638 | [ |
1639 | 'home', | |
1640 | 'all_mem', | |
1641 | 'all_sub', | |
1642 | 'add', | |
1643 | 'rm', | |
1644 | 'sub', | |
1645 | 'unsub', | |
1646 | 'own', | |
1647 | 'new', | |
1648 | 'update', | |
1649 | 'del' | |
1650 | ], # list | |
813a5d80 | 1651 | [], # cal |
a8c5fce4 | 1652 | [key for key in dict(get_all_config())], # config |
ceec8593 | 1653 | g['themes'], # theme |
422dd385 O |
1654 | [ |
1655 | 'discover', | |
1656 | 'tweets', | |
1657 | 'messages', | |
1658 | 'friends_and_followers', | |
1659 | 'list', | |
1660 | 'stream' | |
1661 | ], # help | |
d6cc4c67 O |
1662 | [], # pause |
1663 | [], # reconnect | |
7e4ccbf3 | 1664 | [], # clear |
1665 | [], # quit | |
d51b4107 | 1666 | ] |
7e4ccbf3 | 1667 | )) |
d51b4107 | 1668 | init_interactive_shell(d) |
f5677fb1 | 1669 | read_history() |
819569e8 | 1670 | reset() |
b2b933a9 | 1671 | while True: |
99b52f5f | 1672 | # raw_input |
1dd312f5 | 1673 | if g['prefix']: |
ceec8593 | 1674 | line = raw_input(g['decorated_name'](c['PREFIX'])) |
1dd312f5 O |
1675 | else: |
1676 | line = raw_input() | |
f1c1dfea O |
1677 | # Save cmd to compare with readline buffer |
1678 | g['cmd'] = line.strip() | |
1679 | # Get short cmd to pass to handle function | |
843647ad O |
1680 | try: |
1681 | cmd = line.split()[0] | |
1682 | except: | |
1683 | cmd = '' | |
b8c1f42a | 1684 | try: |
9683e61d | 1685 | # Lock the semaphore |
99b52f5f | 1686 | c['lock'] = True |
9683e61d | 1687 | # Save cmd to global variable and call process |
b8c1f42a | 1688 | g['stuff'] = ' '.join(line.split()[1:]) |
9683e61d | 1689 | # Process the command |
b8c1f42a | 1690 | process(cmd)() |
9683e61d | 1691 | # Not re-display |
99b52f5f | 1692 | if cmd in ['switch', 't', 'rt', 'rep']: |
9683e61d O |
1693 | g['prefix'] = False |
1694 | else: | |
1695 | g['prefix'] = True | |
1696 | # Release the semaphore lock | |
99b52f5f | 1697 | c['lock'] = False |
eadd85a8 | 1698 | except Exception: |
b8c1f42a | 1699 | printNicely(red('OMG something is wrong with Twitter right now.')) |
54277114 O |
1700 | |
1701 | ||
42fde775 | 1702 | def stream(domain, args, name='Rainbow Stream'): |
54277114 | 1703 | """ |
f405a7d0 | 1704 | Track the stream |
54277114 | 1705 | """ |
54277114 | 1706 | # The Logo |
42fde775 | 1707 | art_dict = { |
632c6fa5 O |
1708 | c['USER_DOMAIN']: name, |
1709 | c['PUBLIC_DOMAIN']: args.track_keywords, | |
1f2f6159 | 1710 | c['SITE_DOMAIN']: name, |
42fde775 | 1711 | } |
687567eb | 1712 | if c['ASCII_ART']: |
c075e6dc | 1713 | ascii_art(art_dict[domain]) |
91476ec3 O |
1714 | # These arguments are optional: |
1715 | stream_args = dict( | |
e3927852 | 1716 | timeout=0.5, # To check g['stream_stop'] after each 0.5 s |
cb45dc23 | 1717 | block=True, |
1718 | heartbeat_timeout=c['HEARTBEAT_TIMEOUT'] * 60) | |
91476ec3 O |
1719 | # Track keyword |
1720 | query_args = dict() | |
1721 | if args.track_keywords: | |
1722 | query_args['track'] = args.track_keywords | |
91476ec3 | 1723 | # Get stream |
2a6238f5 | 1724 | stream = TwitterStream( |
22be990e | 1725 | auth=authen(), |
42fde775 | 1726 | domain=domain, |
2a6238f5 | 1727 | **stream_args) |
2a0cabee O |
1728 | try: |
1729 | if domain == c['USER_DOMAIN']: | |
1730 | tweet_iter = stream.user(**query_args) | |
1731 | elif domain == c['SITE_DOMAIN']: | |
1732 | tweet_iter = stream.site(**query_args) | |
42fde775 | 1733 | else: |
2a0cabee O |
1734 | if args.track_keywords: |
1735 | tweet_iter = stream.statuses.filter(**query_args) | |
1736 | else: | |
1737 | tweet_iter = stream.statuses.sample() | |
92983945 BS |
1738 | # Block new stream until other one exits |
1739 | StreamLock.acquire() | |
1740 | g['stream_stop'] = False | |
72c02928 VNM |
1741 | for tweet in tweet_iter: |
1742 | if tweet is None: | |
a1222228 | 1743 | printNicely("-- None --") |
72c02928 | 1744 | elif tweet is Timeout: |
335e7803 O |
1745 | if(g['stream_stop']): |
1746 | StreamLock.release() | |
1747 | break | |
72c02928 VNM |
1748 | elif tweet is HeartbeatTimeout: |
1749 | printNicely("-- Heartbeat Timeout --") | |
cb45dc23 | 1750 | guide = light_magenta("You can use ") + \ |
1751 | light_green("switch") + \ | |
1752 | light_magenta(" command to return to your stream.\n") | |
1753 | guide += light_magenta("Type ") + \ | |
1754 | light_green("h stream") + \ | |
1755 | light_magenta(" for more details.") | |
1756 | printNicely(guide) | |
1757 | sys.stdout.write(g['decorated_name'](c['PREFIX'])) | |
1758 | sys.stdout.flush() | |
8715dda0 O |
1759 | StreamLock.release() |
1760 | break | |
72c02928 VNM |
1761 | elif tweet is Hangup: |
1762 | printNicely("-- Hangup --") | |
1763 | elif tweet.get('text'): | |
1764 | draw( | |
1765 | t=tweet, | |
72c02928 | 1766 | keyword=args.track_keywords, |
8b3456f9 | 1767 | humanize=False, |
9683e61d | 1768 | check_semaphore=True, |
72c02928 VNM |
1769 | fil=args.filter, |
1770 | ig=args.ignore, | |
1771 | ) | |
4824b181 O |
1772 | # Current readline buffer |
1773 | current_buffer = readline.get_line_buffer().strip() | |
335e7803 | 1774 | # There is an unexpected behaviour in MacOSX readline + Python 2: |
3d48702f O |
1775 | # after completely delete a word after typing it, |
1776 | # somehow readline buffer still contains | |
1777 | # the 1st character of that word | |
f1c1dfea | 1778 | if current_buffer and g['cmd'] != current_buffer: |
3d48702f | 1779 | sys.stdout.write( |
7c437a0f | 1780 | g['decorated_name'](c['PREFIX']) + str2u(current_buffer)) |
4824b181 | 1781 | sys.stdout.flush() |
335e7803 O |
1782 | elif not c['HIDE_PROMPT']: |
1783 | sys.stdout.write(g['decorated_name'](c['PREFIX'])) | |
1784 | sys.stdout.flush() | |
14db58c7 | 1785 | elif tweet.get('direct_message'): |
8141aca6 | 1786 | print_message(tweet['direct_message'], check_semaphore=True) |
2a0cabee O |
1787 | except TwitterHTTPError: |
1788 | printNicely('') | |
c075e6dc | 1789 | printNicely( |
2a0cabee | 1790 | magenta("We have maximum connection problem with twitter'stream API right now :(")) |
54277114 O |
1791 | |
1792 | ||
1793 | def fly(): | |
1794 | """ | |
1795 | Main function | |
1796 | """ | |
531f5682 | 1797 | # Initial |
42fde775 | 1798 | args = parse_arguments() |
2a0cabee | 1799 | try: |
fe9bb33b | 1800 | init(args) |
2a0cabee O |
1801 | except TwitterHTTPError: |
1802 | printNicely('') | |
1803 | printNicely( | |
e3927852 | 1804 | magenta("We have connection problem with twitter'stream API right now :(")) |
4c025026 | 1805 | printNicely(magenta("Let's try again later.")) |
2a0cabee | 1806 | save_history() |
2a0cabee | 1807 | sys.exit() |
92983945 | 1808 | # Spawn stream thread |
baec5f50 | 1809 | th = threading.Thread( |
1810 | target=stream, | |
1811 | args=( | |
1812 | c['USER_DOMAIN'], | |
1813 | args, | |
1814 | g['original_name'])) | |
92983945 BS |
1815 | th.daemon = True |
1816 | th.start() | |
42fde775 | 1817 | # Start listen process |
819569e8 | 1818 | time.sleep(0.5) |
c91f75f2 | 1819 | g['reset'] = True |
1dd312f5 | 1820 | g['prefix'] = True |
0f6e4daf | 1821 | listen() |