X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Frainbow.py;h=43466ec61213f3c4a1138ef53b75d2823939bb85;hp=71957448716fe6bcd05f3207d215125dd49de888;hb=17bc529db5669ed5c44a3b74b13d9671ed2243b1;hpb=a5301bc04171f5a7cb1a928ced38e912628cfa85 diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 7195744..43466ec 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -10,6 +10,7 @@ import signal import argparse import time import requests +import webbrowser from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup from twitter.api import * @@ -36,6 +37,7 @@ cmdset = [ 'mentions', 't', 'rt', + 'quote', 'allrt', 'fav', 'rep', @@ -44,6 +46,7 @@ cmdset = [ 's', 'mes', 'show', + 'open', 'ls', 'inbox', 'sent', @@ -137,10 +140,11 @@ def get_decorated_name(): g['decorated_name'] = color_func(c['DECORATED_NAME'])('[' + name + ']: ') g['ascii_art'] = True - files = os.listdir('rainbowstream/colorset') + files = os.listdir(os.path.dirname(__file__)+'/colorset') themes = [f.split('.')[0] for f in files if f.split('.')[-1] == 'json'] - themes += ['user'] + themes += ['custom'] g['themes'] = themes + db.theme_store(c['theme']) def switch(): @@ -309,6 +313,33 @@ def retweet(): t.statuses.retweet(id=tid, include_entities=False, trim_user=True) +def quote(): + """ + Quote a tweet + """ + t = Twitter(auth=authen()) + try: + id = int(g['stuff'].split()[0]) + except: + printNicely(red('Sorry I can\'t understand.')) + return + tid = db.rainbow_to_tweet_query(id)[0].tweet_id + tweet = t.statuses.show(id=tid) + screen_name = tweet['user']['screen_name'] + text = tweet['text'] + quote = '\"@' + screen_name + ': ' + text + '\"' + quote = quote.encode('utf8') + notice = light_magenta('Compose mode ') + notice += light_yellow('(Enter nothing will cancel the quote)') + notice += light_magenta(':') + printNicely(notice) + extra = raw_input(quote) + if extra: + t.statuses.update(status=quote+extra) + else: + printNicely(light_magenta('No text added.')) + + def allretweet(): """ List all retweet @@ -463,6 +494,26 @@ def show(): printNicely(red('Sorry I can\'t show this image.')) +def urlopen(): + """ + Open url + """ + t = Twitter(auth=authen()) + try: + if not g['stuff'].isdigit(): + return + tid = db.rainbow_to_tweet_query(g['stuff'])[0].tweet_id + tweet = t.statuses.show(id=tid) + link_ary = [u for u in tweet['text'].split() if u.startswith('http://')] + if not link_ary: + printNicely(light_magenta('No url here @.@!')) + return + for link in link_ary: + webbrowser.open(link) + except: + printNicely(red('Sorry I can\'t open url in this tweet.')) + + def list(): """ List friends for followers @@ -750,17 +801,8 @@ def cal(): # Format rel = os.popen('cal').read().split('\n') month = rel.pop(0) - month = random_rainbow(month) date = rel.pop(0) - date = ' '.join([cycle_color(i) for i in date.split(' ')]) - today = str(int(os.popen('date +\'%d\'').read().strip())) - # Display - printNicely(month) - printNicely(date) - for line in rel: - ary = line.split(' ') - ary = map(lambda x: on_grey(x) if x == today else grey(x), ary) - printNicely(' '.join(ary)) + show_calendar(month, date, rel) def theme(): @@ -769,37 +811,62 @@ def theme(): """ if not g['stuff']: # List themes - line = ' ' * 2 + light_yellow('* ') for theme in g['themes']: + line = '' # Detect custom config - if theme == 'user': + if theme == 'custom': line += light_magenta('custom') - exists = db.theme_query() - themes = [t.theme_name for t in exists] - if themes[0] == 'user': - line += light_magenta(' (applied)') + custom_path = os.environ.get( + 'HOME', + os.environ.get('USERPROFILE', + '')) + os.sep + '.rainbow_config.json' + if not os.path.exists(custom_path): + line += light_magenta(' (create your own config file at ~/.rainbow_config.json)') else: - line += light_magenta(' (not specified)') + line += light_magenta(' (loaded)') else: line += light_magenta(theme) + if c['theme'] == theme : + line = ' '*2 + light_yellow('* ') + line + else: + line = ' '*4 + line printNicely(line) + elif g['stuff'] == 'current_as_default': + # Set default + path = os.path.dirname(__file__) + '/colorset/init' + f = open(path,'w') + f.write(c['theme']) + f.close() + os.system('chmod 777 ' + path) + printNicely(light_green('Okay it will be applied from next time :)')) else: # Change theme try: # Load new config - new_config = 'rainbowstream/colorset/' + g['stuff'] + '.json' + if g['stuff'] != 'custom': + new_config = os.path.dirname(__file__) + '/colorset/' + g['stuff'] + '.json' + else: + new_config = os.environ.get( + 'HOME',os.environ.get( + 'USERPROFILE', + '')) + os.sep + '.rainbow_config.json' new_config = load_config(new_config) - if new_config + if new_config: for nc in new_config: c[nc] = new_config[nc] - # Update db - theme_update(g['stuff']) + # Update db and reset colors + db.theme_update(g['stuff']) + c['theme'] = g['stuff'] + reset_cycle() g['decorated_name'] = color_func( c['DECORATED_NAME'])( '[@' + g['original_name'] + ']: ') printNicely(green('Theme changed.')) except: - printNicely(red('Sorry, config file is broken!')) + if g['stuff'] == 'custom': + printNicely(red('~/.rainbow_config.json is not exists!')) + else: + printNicely(red('No such theme exists.')) def help(): @@ -844,6 +911,10 @@ def help(): usage += s * 2 + \ light_green('rt 12 ') + ' will retweet to tweet with ' + \ light_yellow('[id=12]') + '.\n' + usage += s * 2 + \ + light_green('quote 12 ') + ' will quote the tweet with ' + \ + light_yellow('[id=12]') + '. If no extra text is added, ' + \ + 'the quote will be canceled.\n' usage += s * 2 + \ light_green('allrt 12 20 ') + ' will list 20 newest retweet of the tweet with ' + \ light_yellow('[id=12]') + '.\n' @@ -861,6 +932,8 @@ def help(): light_yellow('[id=12]') + '.\n' usage += s * 2 + light_green('show image 12') + ' will show image in tweet with ' + \ light_yellow('[id=12]') + ' in your OS\'s image viewer.\n' + usage += s * 2 + light_green('open 12') + ' will open url in tweet with ' + \ + light_yellow('[id=12]') + ' in your OS\'s default browser.\n' # Direct message usage += '\n' @@ -876,7 +949,7 @@ def help(): # Follower and following usage += '\n' - usage += s + grey(u'\u266A' + ' Fiends and followers \n') + usage += s + grey(u'\u266A' + ' Friends and followers \n') usage += s * 2 + \ light_green('ls fl') + \ ' will list all followers (people who are following you).\n' @@ -966,8 +1039,8 @@ def reset(): printNicely(magenta('Need tips ? Type "h" and hit Enter key!')) g['reset'] = False try: - printNicely(eval(g['cmd'])) - except: + printNicely(str(eval(g['cmd']))) + except Exception: pass @@ -985,6 +1058,7 @@ def process(cmd): mentions, tweet, retweet, + quote, allretweet, favorite, reply, @@ -993,6 +1067,7 @@ def process(cmd): search, message, show, + urlopen, list, inbox, sent, @@ -1029,6 +1104,7 @@ def listen(): [], # mentions [], # tweet [], # retweet + [], # quote [], # allretweet [], # favorite [], # reply @@ -1037,6 +1113,7 @@ def listen(): ['#'], # search ['@'], # message ['image'], # show image + [''], # open url ['fl', 'fr'], # list [], # inbox [], # sent @@ -1051,7 +1128,7 @@ def listen(): ['@'], # unblock ['@'], # report [], # cal - g['themes'], # theme + g['themes'] + ['current_as_default'], # theme [], # help [], # clear [], # quit @@ -1074,7 +1151,7 @@ def listen(): try: g['stuff'] = ' '.join(line.split()[1:]) process(cmd)() - except Exception as e: + except Exception,e : print e printNicely(red('OMG something is wrong with Twitter right now.')) # Not redisplay prefix @@ -1115,18 +1192,18 @@ def stream(domain, args, name='Rainbow Stream'): domain=domain, **stream_args) - if domain == c['USER_DOMAIN']: - tweet_iter = stream.user(**query_args) - elif domain == c['SITE_DOMAIN']: - tweet_iter = stream.site(**query_args) - else: - if args.track_keywords: - tweet_iter = stream.statuses.filter(**query_args) + try: + if domain == c['USER_DOMAIN']: + tweet_iter = stream.user(**query_args) + elif domain == c['SITE_DOMAIN']: + tweet_iter = stream.site(**query_args) else: - tweet_iter = stream.statuses.sample() + if args.track_keywords: + tweet_iter = stream.statuses.filter(**query_args) + else: + tweet_iter = stream.statuses.sample() - # Iterate over the stream. - try: + # Iterate over the stream. for tweet in tweet_iter: if tweet is None: printNicely("-- None --") @@ -1144,10 +1221,10 @@ def stream(domain, args, name='Rainbow Stream'): fil=args.filter, ig=args.ignore, ) - except: + except TwitterHTTPError: + printNicely('') printNicely( - magenta("I'm afraid we have problem with twitter'S maximum connection.")) - printNicely(magenta("Let's try again later.")) + magenta("We have maximum connection problem with twitter'stream API right now :(")) def fly(): @@ -1156,11 +1233,22 @@ def fly(): """ # Spawn stream process args = parse_arguments() - get_decorated_name() + try: + get_decorated_name() + + except TwitterHTTPError: + printNicely('') + printNicely( + magenta("I'm afraid we have maximum connection problem with twitter right now :(")) + printNicely(magenta("Let's try again later.")) + save_history() + os.system('rm -rf rainbow.db') + sys.exit() + p = Process( target=stream, args=( - c['USER_DOMAIN'], + c['USER_DOMAIN'], args, g['original_name'])) p.start()