Skip to content

Commit

Permalink
[rostwitter] Add status_code to check error
Browse files Browse the repository at this point in the history
  • Loading branch information
iory committed Sep 22, 2022
1 parent 219438d commit 3293b1e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions rostwitter/python/rostwitter/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ def _post_update_with_reply(self, texts, media_list=None,
data['media_ids'] = media_ids
if in_reply_to_status_id is not None:
data['in_reply_to_status_id'] = in_reply_to_status_id
json = self._request_url(url, 'POST', data=data)
data = simplejson.loads(json.content)
in_reply_to_status_id = data['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')
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 @@ -89,7 +94,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')
rospy.logwarn('upload media failed. status_code: {}'
.format(r.status_code))
media_ids = ','.join(media_ids)
return media_ids

Expand All @@ -111,8 +117,13 @@ def post_media(self, status, media, in_reply_to_status_id=None):
url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'
data = {'status': status}
data['media'] = open(str(media), 'rb').read()
json = self._request_url(url, 'POST', data=data)
data = simplejson.loads(json.content)
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))
if len(texts) > 1:
data = self._post_update_with_reply(
texts[1:],
Expand Down

0 comments on commit 3293b1e

Please sign in to comment.