From 5ac7d5aefa4fac3b3c2833a1d37ed129c6fe5ded Mon Sep 17 00:00:00 2001 From: Kei Okada Date: Tue, 9 Aug 2022 16:19:14 +0900 Subject: [PATCH 1/2] rostwitter: enable to receive base64 image --- rostwitter/python/rostwitter/twitter.py | 13 ++++++++- rostwitter/scripts/tweet.py | 37 +++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/rostwitter/python/rostwitter/twitter.py b/rostwitter/python/rostwitter/twitter.py index cdb020e15..b20b3f919 100644 --- a/rostwitter/python/rostwitter/twitter.py +++ b/rostwitter/python/rostwitter/twitter.py @@ -1,5 +1,6 @@ # originally from https://raw.githubusercontent.com/bear/python-twitter/v1.1/twitter.py # NOQA +import base64 import json as simplejson import requests from requests_oauthlib import OAuth1 @@ -9,6 +10,7 @@ except ImportError: from io import StringIO ## for Python 3 +import os import rospy @@ -71,7 +73,16 @@ def post_media(self, status, media): status = status[:116] url = 'https://api.twitter.com/1.1/statuses/update_with_media.json' data = {'status': StringIO(status)} - data['media'] = open(str(media), 'rb').read() + if os.path.exists(str(media)): + data['media'] = open(str(media), 'rb').read() + else: + try: + if base64.b64encode(base64.b64decode(media)) == media: + data['media'] = base64.b64decode(media) + else: + raise Exception + except: + rospy.logwarn('tweet media is neither file nor base64 data') json = self._request_url(url, 'POST', data=data) data = simplejson.loads(json.content) return data diff --git a/rostwitter/scripts/tweet.py b/rostwitter/scripts/tweet.py index 1efc3107d..bba58d7cf 100755 --- a/rostwitter/scripts/tweet.py +++ b/rostwitter/scripts/tweet.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import base64 import os import re import sys @@ -10,6 +11,13 @@ from rostwitter.twitter import Twitter from rostwitter.util import load_oauth_settings +# https://stackoverflow.com/questions/12315398/check-if-a-string-is-encoded-in-base64-using-python +def isBase64(s): + try: + return base64.b64encode(base64.b64decode(s)) == s + except Exception as e: + print(e) + return False class Tweet(object): def __init__(self): @@ -28,7 +36,7 @@ def __init__(self): def tweet_cb(self, msg): message = msg.data - rospy.loginfo(rospy.get_name() + " sending %s", message) + rospy.loginfo(rospy.get_name() + " sending %s", message[0:256]) # search word start from / and end with {.jpeg,.jpg,.png,.gif} m = re.search('/\S+\.(jpeg|jpg|png|gif)', message) @@ -40,17 +48,30 @@ def tweet_cb(self, msg): rospy.loginfo( rospy.get_name() + " tweet %s with file %s", message, filename) - # 140 - len("http://t.co/ssssssssss") ret = self.api.post_media(message[0:116], filename) - if 'errors' in ret: - rospy.logerr('Failed to post: {}'.format(ret)) - # ret = self.api.post_update(message) else: rospy.logerr(rospy.get_name() + " %s could not find", filename) - else: + + # search base64 encoding string + m = re.search('/9j.*$', message) # jpeg image starts from /9j ???? + if m: + image = m.group(0) + message = re.sub("/9j.*$", "", message) + if isBase64(image): + rospy.loginfo( + rospy.get_name() + " tweet %s with base64 image %s", + message, image[0:128]) + ret = self.api.post_media(message[0:116], image) + else: + rospy.logerr(rospy.get_name() + " %s is not base64 string", image) + + # post message if not media found + if m == None: ret = self.api.post_update(message[0:140]) - if 'errors' in ret: - rospy.logerr('Failed to post: {}'.format(ret)) + + # show results + if ret and 'errors' in ret: + rospy.logerr('Failed to post: {}'.format(ret)) # seg faults if message is longer than 140 byte ??? rospy.loginfo(rospy.get_name() + " receiving %s", ret) From f2324b7b86c389c6adb8728e540d356097af6bc9 Mon Sep 17 00:00:00 2001 From: iory Date: Tue, 9 Aug 2022 23:45:11 +0900 Subject: [PATCH 2/2] [rostwitter] Fixed typo wit -> with --- rostwitter/python/rostwitter/twitter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rostwitter/python/rostwitter/twitter.py b/rostwitter/python/rostwitter/twitter.py index b20b3f919..1294a8cde 100644 --- a/rostwitter/python/rostwitter/twitter.py +++ b/rostwitter/python/rostwitter/twitter.py @@ -69,7 +69,7 @@ def post_update(self, status): def post_media(self, status, media): # 116 = 140 - len("http://t.co/ssssssssss") if len(status) > 116: - rospy.logwarn('tweet wit media is too longer > 116 characters') + rospy.logwarn('tweet with media is too longer > 116 characters') status = status[:116] url = 'https://api.twitter.com/1.1/statuses/update_with_media.json' data = {'status': StringIO(status)}