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