Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rostwitter: enable to receive base64 image #371

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions rostwitter/python/rostwitter/twitter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +10,7 @@
except ImportError:
from io import StringIO ## for Python 3

import os
import rospy


Expand Down Expand Up @@ -67,11 +69,20 @@ 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)}
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
37 changes: 29 additions & 8 deletions rostwitter/scripts/tweet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import base64
import os
import re
import sys
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)

Expand Down