add support for python 3
[rainbowstream.git] / rainbowstream / rainbow.py
index fe060fb3de0af1a5e411ff50f166877644b7ae41..af674eb3d6b91a4c2d8ec35fe8306c0d081c92b6 100644 (file)
@@ -10,13 +10,13 @@ import signal
 import argparse
 import time
 import requests
+import webbrowser
 
 from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup
 from twitter.api import *
 from twitter.oauth import OAuth, read_token_file
 from twitter.oauth_dance import oauth_dance
 from twitter.util import printNicely
-from StringIO import StringIO
 
 from .draw import *
 from .colors import *
@@ -25,6 +25,8 @@ from .consumer import *
 from .interactive import *
 from .db import *
 from .c_image import *
+from .py3patch import *
+
 
 g = {}
 db = RainbowDB()
@@ -36,6 +38,7 @@ cmdset = [
     'mentions',
     't',
     'rt',
+    'quote',
     'allrt',
     'fav',
     'rep',
@@ -44,6 +47,7 @@ cmdset = [
     's',
     'mes',
     'show',
+    'open',
     'ls',
     'inbox',
     'sent',
@@ -310,6 +314,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
@@ -464,6 +495,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
@@ -781,6 +832,14 @@ def theme():
             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:
@@ -799,7 +858,7 @@ def theme():
             # Update db and reset colors
             db.theme_update(g['stuff'])
             c['theme'] = g['stuff']
-            notify_cycle()
+            reset_cycle()
             g['decorated_name'] = color_func(
                 c['DECORATED_NAME'])(
                 '[@' + g['original_name'] + ']: ')
@@ -853,6 +912,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'
@@ -870,6 +933,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'
@@ -885,7 +950,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'
@@ -994,6 +1059,7 @@ def process(cmd):
             mentions,
             tweet,
             retweet,
+            quote,
             allretweet,
             favorite,
             reply,
@@ -1002,6 +1068,7 @@ def process(cmd):
             search,
             message,
             show,
+            urlopen,
             list,
             inbox,
             sent,
@@ -1038,6 +1105,7 @@ def listen():
             [],  # mentions
             [],  # tweet
             [],  # retweet
+            [],  # quote
             [],  # allretweet
             [],  # favorite
             [],  # reply
@@ -1046,6 +1114,7 @@ def listen():
             ['#'],  # search
             ['@'],  # message
             ['image'],  # show image
+            [''],  # open url
             ['fl', 'fr'],  # list
             [],  # inbox
             [],  # sent
@@ -1060,7 +1129,7 @@ def listen():
             ['@'],  # unblock
             ['@'],  # report
             [],  # cal
-            g['themes'],  # theme
+            g['themes'] + ['current_as_default'],  # theme
             [],  # help
             [],  # clear
             [],  # quit