From 3293b1e93d54c191f5fe29c7e2c660d0ccba2cb4 Mon Sep 17 00:00:00 2001 From: iory Date: Thu, 22 Sep 2022 12:19:50 +0900 Subject: [PATCH] [rostwitter] Add status_code to check error --- rostwitter/python/rostwitter/twitter.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/rostwitter/python/rostwitter/twitter.py b/rostwitter/python/rostwitter/twitter.py index c9d1981bb..b0374709c 100644 --- a/rostwitter/python/rostwitter/twitter.py +++ b/rostwitter/python/rostwitter/twitter.py @@ -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): @@ -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 @@ -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:],