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