X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Frainbow.py;h=6a565f9fb2bcb48864a25e5b9ef9ce52daf8278e;hp=f58d59c314dc158b3694394dd592054f949c7af3;hb=dfbc72883a8e61e8f3d4ccd37169488e8e67d1fb;hpb=571ea706e5688d01bd83bafbbe601a0d94ac90ab diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index f58d59c..6a565f9 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -7,6 +7,8 @@ import time import threading import requests import webbrowser +import traceback +import pkg_resources from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup from twitter.api import * @@ -21,6 +23,7 @@ from .consumer import * from .interactive import * from .c_image import * from .py3patch import * +from .emoji import * # Global values g = {} @@ -108,6 +111,35 @@ def build_mute_dict(dict_data=False): return screen_name_list +def debug_option(): + """ + Save traceback when run in debug mode + """ + if g['debug']: + g['traceback'].append(traceback.format_exc()) + + +def upgrade_center(): + """ + Check latest and notify to upgrade + """ + try: + current = pkg_resources.get_distribution("rainbowstream").version + url = 'https://raw.githubusercontent.com/DTVD/rainbowstream/master/setup.py' + readme = requests.get(url).content + latest = readme.split("version = \'")[1].split("\'")[0] + if current != latest: + notice = light_magenta('RainbowStream latest version is ') + notice += light_green(latest) + notice += light_magenta(' while your current version is ') + notice += light_yellow(current) + '\n' + notice += light_magenta('You should upgrade with ') + notice += light_green('pip install -U rainbowstream') + printNicely(notice) + except: + pass + + def init(args): """ Init function @@ -115,6 +147,8 @@ def init(args): # Handle Ctrl C ctrl_c_handler = lambda signum, frame: quit() signal.signal(signal.SIGINT, ctrl_c_handler) + # Upgrade notify + upgrade_center() # Get name t = Twitter(auth=authen()) credential = t.account.verify_credentials() @@ -122,6 +156,8 @@ def init(args): name = credential['name'] if not get_config('PREFIX'): set_config('PREFIX', screen_name) + c['PREFIX'] = emojize(c['PREFIX']) + g['PREFIX'] = u2str(c['PREFIX']) c['original_name'] = g['original_name'] = screen_name[1:] g['full_name'] = name g['decorated_name'] = lambda x: color_func( @@ -132,8 +168,15 @@ def init(args): g['themes'] = themes g['pause'] = False g['message_threads'] = {} + # Events + g['events'] = [] # Startup cmd g['cmd'] = '' + # Debug option default = True + g['debug'] = True + g['traceback'] = [] + # Retweet of mine events + c['events'] = [] # Semaphore init c['lock'] = False # Init tweet dict and message dict @@ -195,6 +238,19 @@ def home(): printNicely('') +def notification(): + """ + Show notifications + """ + g['events'] = g['events'] + c['events'] + if g['events']: + for e in g['events']: + print_event(e) + printNicely('') + else: + printNicely(magenta('Nothing at this time.')) + + def mentions(): """ Mentions timeline @@ -221,7 +277,8 @@ def whois(): include_entities=False) show_profile(user) except: - printNicely(red('Omg no user.')) + debug_option() + printNicely(red('No user.')) else: printNicely(red('A name should begin with a \'@\'')) @@ -249,13 +306,24 @@ def search(): Search """ t = Twitter(auth=authen()) - g['stuff'] = g['stuff'].strip() - rel = t.search.tweets(q=g['stuff'])['statuses'] + # Setup query + query = g['stuff'].strip() + type = c['SEARCH_TYPE'] + if type not in ['mixed', 'recent', 'popular']: + type = 'mixed' + max_record = c['SEARCH_MAX_RECORD'] + count = min(max_record, 100) + # Perform search + rel = t.search.tweets( + q=query, + type=type, + count=count + )['statuses'] + # Return results if rel: printNicely('Newest tweets:') - for i in reversed(xrange(c['SEARCH_MAX_RECORD'])): - draw(t=rel[i], - keyword=g['stuff']) + for i in reversed(xrange(count)): + draw(t=rel[i], keyword=query) printNicely('') else: printNicely(magenta('I\'m afraid there is no result')) @@ -448,6 +516,7 @@ def show(): img = Image.open(BytesIO(res.content)) img.show() except: + debug_option() printNicely(red('Sorry I can\'t show this image.')) @@ -470,6 +539,7 @@ def urlopen(): for link in link_ary: webbrowser.open(link) except: + debug_option() printNicely(red('Sorry I can\'t open url in this tweet.')) @@ -551,6 +621,7 @@ def thread(): g['original_name'], g['full_name']) except Exception: + debug_option() printNicely(red('No such thread.')) @@ -571,6 +642,7 @@ def message(): else: printNicely(red('A name should begin with a \'@\'')) except: + debug_option() printNicely(red('Sorry I can\'t understand.')) @@ -679,6 +751,7 @@ def mute(): else: printNicely(red(rel)) except: + debug_option() printNicely(red('Something is wrong, can not mute now :(')) else: printNicely(red('A name should begin with a \'@\'')) @@ -877,6 +950,7 @@ def list_add(t): screen_name=user_name) printNicely(green('Added.')) except: + debug_option() printNicely(light_magenta('I\'m sorry we can not add him/her.')) @@ -896,6 +970,7 @@ def list_remove(t): screen_name=user_name) printNicely(green('Gone.')) except: + debug_option() printNicely(light_magenta('I\'m sorry we can not remove him/her.')) @@ -911,6 +986,7 @@ def list_subscribe(t): owner_screen_name=owner) printNicely(green('Done.')) except: + debug_option() printNicely( light_magenta('I\'m sorry you can not subscribe to this list.')) @@ -927,6 +1003,7 @@ def list_unsubscribe(t): owner_screen_name=owner) printNicely(green('Done.')) except: + debug_option() printNicely( light_magenta('I\'m sorry you can not unsubscribe to this list.')) @@ -963,6 +1040,7 @@ def list_new(t): description=description) printNicely(green(name + ' list is created.')) except: + debug_option() printNicely(red('Oops something is wrong with Twitter :(')) @@ -990,6 +1068,7 @@ def list_update(t): description=description) printNicely(green(slug + ' list is updated.')) except: + debug_option() printNicely(red('Oops something is wrong with Twitter :(')) @@ -1004,6 +1083,7 @@ def list_delete(t): owner_screen_name=g['original_name']) printNicely(green(slug + ' list is deleted.')) except: + debug_option() printNicely(red('Oops something is wrong with Twitter :(')) @@ -1214,6 +1294,8 @@ def help_discover(): light_green('trend JP Tokyo') + '.\n' usage += s * 2 + light_green('home') + ' will show your timeline. ' + \ light_green('home 7') + ' will show 7 tweets.\n' + usage += s * 2 + \ + light_green('notification') + ' will show your recent notification.\n' usage += s * 2 + light_green('mentions') + ' will show mentions timeline. ' + \ light_green('mentions 7') + ' will show 7 mention tweets.\n' usage += s * 2 + light_green('whois @mdo') + ' will show profile of ' + \ @@ -1523,6 +1605,7 @@ cmdset = [ 'switch', 'trend', 'home', + 'notification', 'view', 'mentions', 't', @@ -1567,6 +1650,7 @@ funcset = [ switch, trend, home, + notification, view, mentions, tweet, @@ -1624,6 +1708,7 @@ def listen(): ['public', 'mine'], # switch [], # trend [], # home + [], # notification ['@'], # view [], # mentions [], # tweet @@ -1689,7 +1774,8 @@ def listen(): try: # raw_input if g['prefix']: - line = raw_input(g['decorated_name'](c['PREFIX'])) + # Only use PREFIX as a string with raw_input + line = raw_input(g['decorated_name'](g['PREFIX'])) else: line = raw_input() # Save cmd to compare with readline buffer @@ -1715,6 +1801,7 @@ def listen(): except EOFError: printNicely('') except Exception: + debug_option() printNicely(red('OMG something is wrong with Twitter right now.')) @@ -1813,6 +1900,9 @@ def stream(domain, args, name='Rainbow Stream'): while c['lock']: time.sleep(0.5) print_message(tweet['direct_message']) + elif tweet.get('event'): + g['events'].append(tweet) + print_event(tweet) except TwitterHTTPError: printNicely('') printNicely(