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