From: motatoes Date: Fri, 26 May 2017 08:20:29 +0000 (-0400) Subject: added tweeting image support X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=commitdiff_plain;h=648e8ac7ae794c1452f3d3b144358c0b9e62fc9e added tweeting image support Also removes error swallowing --- diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index e0c86aa..f1db50d 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -491,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(): """ @@ -1614,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 [,,...] 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' @@ -2122,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