X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Frainbow.py;h=5f7ab72b03d64138d5bb4ff1da55fcce2913727f;hp=05bbeabfa541b4742e5e6682fa148e12d2862b81;hb=ac03309ca1b4fcaf8f85509862af86a632161622;hpb=99cd1fba83efcd1dd6f01570ea79776b4e41f1cf;ds=sidebyside diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 05bbeab..5f7ab72 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,7 @@ 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 @@ -133,10 +168,13 @@ 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'] = [] + # Events + c['events'] = [] # Semaphore init c['lock'] = False # Init tweet dict and message dict @@ -202,8 +240,8 @@ def notification(): """ Show notifications """ - if g['events']: - for e in g['events']: + if c['events']: + for e in c['events']: print_event(e) printNicely('') else: @@ -236,7 +274,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 \'@\'')) @@ -474,6 +513,7 @@ def show(): img = Image.open(BytesIO(res.content)) img.show() except: + debug_option() printNicely(red('Sorry I can\'t show this image.')) @@ -496,6 +536,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.')) @@ -577,6 +618,7 @@ def thread(): g['original_name'], g['full_name']) except Exception: + debug_option() printNicely(red('No such thread.')) @@ -597,6 +639,7 @@ def message(): else: printNicely(red('A name should begin with a \'@\'')) except: + debug_option() printNicely(red('Sorry I can\'t understand.')) @@ -705,6 +748,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 \'@\'')) @@ -797,10 +841,10 @@ def report(): def get_slug(): """ - Get Slug Decorator + Get slug """ # Get list name - list_name = raw_input(light_magenta('Give me the list\'s name: ')) + list_name = raw_input(light_magenta('Give me the list\'s name ("@owner/list_name"): ')) # Get list name and owner try: owner, slug = list_name.split('/') @@ -903,6 +947,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.')) @@ -922,6 +967,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.')) @@ -937,6 +983,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.')) @@ -953,6 +1000,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.')) @@ -989,6 +1037,7 @@ def list_new(t): description=description) printNicely(green(name + ' list is created.')) except: + debug_option() printNicely(red('Oops something is wrong with Twitter :(')) @@ -1016,6 +1065,7 @@ def list_update(t): description=description) printNicely(green(slug + ' list is updated.')) except: + debug_option() printNicely(red('Oops something is wrong with Twitter :(')) @@ -1030,6 +1080,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 :(')) @@ -1115,13 +1166,47 @@ def switch(): g['original_name'])) th.daemon = True th.start() + # Stream base on list + elif target == 'list': + owner, slug = get_slug() + # Force python 2 not redraw readline buffer + g['cmd'] = '/'.join([owner,slug]) + printNicely(light_yellow('getting list members ...')) + # Get members + t = Twitter(auth=authen()) + members = [] + next_cursor = -1 + while next_cursor != 0: + m = t.lists.members( + slug=slug, + owner_screen_name=owner, + cursor=next_cursor, + include_entities=False) + for u in m['users']: + members.append('@' + u['screen_name']) + next_cursor = m['next_cursor'] + printNicely(light_yellow('... done.')) + # Build thread filter array + args.filter = members + # Kill old thread + g['stream_stop'] = True + # Start new thread + th = threading.Thread( + target=stream, + args=( + c['USER_DOMAIN'], + args, + slug)) + th.daemon = True + th.start() printNicely('') if args.filter: - printNicely(cyan('Only: ' + str(args.filter))) + printNicely(cyan('Include: ' + str(len(args.filter)) + ' people.')) if args.ignore: - printNicely(red('Ignore: ' + str(args.ignore))) + printNicely(red('Ignore: ' + str(len(args.ignore)) + ' people.')) printNicely('') - except: + except Exception: + debug_option() printNicely(red('Sorry I can\'t understand.')) @@ -1404,6 +1489,8 @@ def help_stream(): ' filter will decide nicks will be EXCLUDE.\n' usage += s * 2 + light_green('switch mine -d') + \ ' will use the config\'s ONLY_LIST and IGNORE_LIST.\n' + usage += s * 2 + light_green('switch list') + \ + ' will switch to a Twitter list\'s stream. You will be asked for list name\n' printNicely(usage) @@ -1651,7 +1738,7 @@ def listen(): d = dict(zip( cmdset, [ - ['public', 'mine'], # switch + ['public', 'mine', 'list'], # switch [], # trend [], # home [], # notification @@ -1720,6 +1807,7 @@ def listen(): try: # raw_input if g['prefix']: + # Only use PREFIX as a string with raw_input line = raw_input(g['decorated_name'](g['PREFIX'])) else: line = raw_input() @@ -1746,6 +1834,7 @@ def listen(): except EOFError: printNicely('') except Exception: + debug_option() printNicely(red('OMG something is wrong with Twitter right now.')) @@ -1804,7 +1893,7 @@ def stream(domain, args, name='Rainbow Stream'): light_green("h stream") + \ light_magenta(" for more details.") printNicely(guide) - sys.stdout.write(g['decorated_name'](g['PREFIX'])) + sys.stdout.write(g['decorated_name'](c['PREFIX'])) sys.stdout.flush() StreamLock.release() break @@ -1832,10 +1921,10 @@ def stream(domain, args, name='Rainbow Stream'): # the 1st character of that word if current_buffer and g['cmd'] != current_buffer: sys.stdout.write( - g['decorated_name'](g['PREFIX']) + str2u(current_buffer)) + g['decorated_name'](c['PREFIX']) + str2u(current_buffer)) sys.stdout.flush() elif not c['HIDE_PROMPT']: - sys.stdout.write(g['decorated_name'](g['PREFIX'])) + sys.stdout.write(g['decorated_name'](c['PREFIX'])) sys.stdout.flush() elif tweet.get('direct_message'): # Check the semaphore pause and lock (stream process only) @@ -1845,7 +1934,7 @@ def stream(domain, args, name='Rainbow Stream'): time.sleep(0.5) print_message(tweet['direct_message']) elif tweet.get('event'): - g['events'].append(tweet) + c['events'].append(tweet) print_event(tweet) except TwitterHTTPError: printNicely('')