Skip to content

Commit

Permalink
[rostwitter] Avoid error when tweet post returning error code
Browse files Browse the repository at this point in the history
  • Loading branch information
iory authored and knorth55 committed Sep 22, 2022
1 parent 490865f commit 662f0b5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
38 changes: 23 additions & 15 deletions rostwitter/python/rostwitter/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ def _request_url(self, url, verb, data=None):
)
return 0 # if not a POST or GET request

def _check_post_request(self, request):
valid = True
if request.status_code == 200:
data = simplejson.loads(request.content)
if 'errors' in data:
for error in data['errors']:
rospy.logwarn('Tweet error code: {}, message: {}'
.format(error['code'], error['message']))
valid = False
else:
rospy.logwarn('post tweet failed. status_code: {}'
.format(request.status_code))
valid = False
if valid:
return data

def _post_update_with_reply(self, texts, media_list=None,
in_reply_to_status_id=None):
split_media_list = []
Expand All @@ -75,13 +91,9 @@ def _post_update_with_reply(self, texts, media_list=None,
if in_reply_to_status_id is not None:
data['in_reply_to_status_id'] = in_reply_to_status_id
r = self._request_url(url, 'POST', data=data)
data = simplejson.loads(r.content)
if r.status_code == 200:
rospy.loginfo('post update with reply success')
data = self._check_post_request(r)
if data is not None:
in_reply_to_status_id = data['id']
else:
rospy.logwarn('post update with reply failed. status_code: {}'
.format(r.status_code))
return data

def _upload_media(self, media_list):
Expand All @@ -94,8 +106,8 @@ def _upload_media(self, media_list):
rospy.loginfo('upload media success')
media_ids.append(str(r.json()['media_id']))
else:
rospy.logwarn('upload media failed. status_code: {}'
.format(r.status_code))
rospy.logerr('upload media failed. status_code: {}'
.format(r.status_code))
media_ids = ','.join(media_ids)
return media_ids

Expand All @@ -108,7 +120,8 @@ def post_update(self, status, in_reply_to_status_id=None):
texts,
media_list=mlist,
in_reply_to_status_id=in_reply_to_status_id)
in_reply_to_status_id = data['id']
if data is not None:
in_reply_to_status_id = data['id']
return data

def post_media(self, status, media, in_reply_to_status_id=None):
Expand All @@ -118,12 +131,7 @@ def post_media(self, status, media, in_reply_to_status_id=None):
data = {'status': status}
data['media'] = open(str(media), 'rb').read()
r = self._request_url(url, 'POST', data=data)
data = simplejson.loads(r.content)
if r.status_code == 200:
rospy.loginfo('post media success')
else:
rospy.logwarn('post media failed. status_code: {}'
.format(r.status_code))
data = self._check_post_request(r)
if len(texts) > 1:
data = self._post_update_with_reply(
texts[1:],
Expand Down
5 changes: 2 additions & 3 deletions rostwitter/scripts/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ def tweet_cb(self, msg):
''.join([message] if len(message) < 128 else message[0:128]+'......'))

ret = self.api.post_update(message)
if 'errors' in ret:
rospy.logerr('Failed to post: {}'.format(ret))
rospy.loginfo(rospy.get_name() + " receiving %s", ret)
if ret is not None:
rospy.loginfo(rospy.get_name() + " receiving %s", ret)


if __name__ == '__main__':
Expand Down

0 comments on commit 662f0b5

Please sign in to comment.