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