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