requirements for repeatable install
[rainbowstream.git] / rainbowstream / rainbow.py
index a7007ccee7aeb02de7398bc9f80398a0ec14f9c4..f1db50dec3144f7b8f2ddfb640da9fa05a356597 100644 (file)
@@ -89,6 +89,16 @@ def parse_arguments():
         '--proxy-type',
         default='SOCKS5',
         help='Proxy type (HTTP, SOCKS4, SOCKS5; Default: SOCKS5).')
+    parser.add_argument(
+        '-ta',
+        '--twitter-auth',
+        default=os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.rainbow_oauth',
+        help='Specify which OAuth profile to use for twitter. Default: ~/.rainbow_oauth.')
+    parser.add_argument(
+        '-pa',
+        '--pocket-auth',
+        default=os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.rainbow_pckt_oauth',
+        help='Specify which OAuth profile to use for pocket. Default: ~/.rainbow_pckt_oauth.')
     return parser.parse_args()
 
 
@@ -122,11 +132,7 @@ def authen():
     Authenticate with Twitter OAuth
     """
     # When using rainbow stream you must authorize.
-    twitter_credential = os.environ.get(
-        'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_oauth'
+    twitter_credential = g['twitter_oauth_path']
     if not os.path.exists(twitter_credential):
         oauth_dance('Rainbow Stream',
                     CONSUMER_KEY,
@@ -144,12 +150,7 @@ def pckt_authen():
     """
     Authenticate with Pocket OAuth
     """
-    pocket_credential = os.environ.get(
-         'HOME',
-        os.environ.get(
-            'USERPROFILE',
-            '')) + os.sep + '.rainbow_pckt_oauth'
-
+    pocket_credential = g['pocket_oauth_path']
     if not os.path.exists(pocket_credential):
         request_token = Pocket.get_request_token(consumer_key=PCKT_CONSUMER_KEY)
         auth_url = Pocket.get_auth_url(code=request_token, redirect_uri="/")
@@ -238,6 +239,9 @@ def init(args):
     # Handle Ctrl C
     ctrl_c_handler = lambda signum, frame: quit()
     signal.signal(signal.SIGINT, ctrl_c_handler)
+    # Set OAuth file path (needs to happen before authen is called)
+    g['twitter_oauth_path'] = args.twitter_auth
+    g['pocket_oauth_path'] = args.pocket_auth
     # Upgrade notify
     upgrade_center()
     # Get name
@@ -476,7 +480,7 @@ def search():
     # Return results
     if rel:
         printNicely('Newest tweets:')
-        for i in reversed(xrange(count)):
+        for i in reversed(xrange(min(len(rel), count))):
             draw(t=rel[i], keyword=query)
         printNicely('')
     else:
@@ -487,9 +491,33 @@ def tweet():
     """
     Tweet
     """
-    t = Twitter(auth=authen())
-    t.statuses.update(status=g['stuff'])
+    # Regex to check if tweet contains '--i' pattern
+    pattern = '(.*) --i (.+)'
+    m = re.match(pattern, g['stuff'])
 
+    if m is None:
+        # text only tweet
+        t = Twitter(auth=authen())
+        t.statuses.update(status=g['stuff'])
+    else:
+        # A tweet with media items
+        body = m.group(1)
+        imagePaths = m.group(2)
+
+        # Generating image ids
+        imageIds = []
+        for impath in imagePaths.split(','):
+            imagedata = open(impath, 'rb').read()
+
+            # upload media
+            t_up = Twitter(domain='upload.twitter.com',
+                            auth=authen())
+            img_id = t_up.media.upload(media=imagedata)["media_id_string"]
+            imageIds.append(img_id)
+
+        # send your tweet with the list of media ids:
+        t = Twitter(auth=authen())
+        t.statuses.update(status=body, media_ids=",".join(imageIds))        
 
 def pocket():
     """
@@ -1610,7 +1638,9 @@ def help_tweets():
     usage = '\n'
     usage += s + grey(u'\u266A' + ' Tweets \n')
     usage += s * 2 + light_green('t oops ') + \
-        'will tweet "' + light_yellow('oops') + '" immediately.\n'
+        'will tweet "' + light_yellow('oops') + '" immediately.\n' + \
+         s * 3 + ' Optionally you can add --i <img1path>[,<img2path>,...] argument, e.g:\n' + \
+         s * 3 + light_yellow(' t This tweet has images --i /path/to/test1.png,relative_test.jpg') + '\n'
     usage += s * 2 + \
         light_green('rt 12 ') + ' will retweet to tweet with ' + \
         light_yellow('[id=12]') + '.\n'
@@ -2118,9 +2148,6 @@ def listen():
             printNicely('')
         except TwitterHTTPError as e:
             detail_twitter_error(e)
-        except Exception:
-            debug_option()
-            printNicely(red('OMG something is wrong with Twitter API right now.'))
         finally:
             # Release the semaphore lock
             c['lock'] = False