Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PostRetweet function implements retweet API

Added a PostRetweet function which implements the retweet API.

https://dev.twitter.com/docs/api/1/post/statuses/retweet/%3Aid

Also works with Twitter API v1.1.
  • Loading branch information...
commit 640be9e06af4d66bef307834e74b97eea746993e 1 parent d37a785
@sebastianw sebastianw authored
Showing with 35 additions and 0 deletions.
  1. +1 −0  testdata/retweet.json
  2. +27 −0 twitter.py
  3. +7 −0 twitter_test.py
View
1  testdata/retweet.json
@@ -0,0 +1 @@
+{"created_at":"Sun Jun 03 18:15:29 +0000 2007","text":"\u041c\u043e\u0451 \u0441\u0443\u0434\u043d\u043e \u043d\u0430 \u0432\u043e\u0437\u0434\u0443\u0448\u043d\u043e\u0439 \u043f\u043e\u0434\u0443\u0448\u043a\u0435 \u043f\u043e\u043b\u043d\u043e \u0443\u0433\u0440\u0435\u0439","id":89512102,"user":{"name":"Kesuke Miyagi","description":"\u79c1\u306e\u30db\u30d0\u30fc\u30af\u30e9\u30d5\u30c8 \u306f\u9c3b\u304c\u4e00\u676f\u3067\u3059\u3002","location":"Okinawa, Japan","profile_image_url":"http://assets1.twitter.com/system/user/profile_image/718443/normal/kesuke.png?1169966399","url":null,"id":718443,"screen_name":"kesuke","protected":false}}
View
27 twitter.py
@@ -2871,6 +2871,33 @@ def PostUpdates(self, status, continuation=None, **kwargs):
results.append(self.PostUpdate(lines[-1], **kwargs))
return results
+ def PostRetweet(self, original_id):
+ '''Retweet a tweet with the Retweet API.
+
+ The twitter.Api instance must be authenticated.
+
+ Args:
+ original_id:
+ The numerical id of the tweet that will be retweeted
+ Returns:
+ A twitter.Status instance representing the original tweet with retweet details embedded.
+ '''
+ if not self._oauth_consumer:
+ raise TwitterError("The twitter.Api instance must be authenticated.")
+
+ try:
+ if int(original_id) <= 0:
+ raise TwitterError("'original_id' must be a positive number")
+ except ValueError:
+ raise TwitterError("'original_id' must be an integer")
+
+ url = '%s/statuses/retweet/%s.json' % (self.base_url, original_id)
+
+ data = {'id': original_id}
+ json = self._FetchUrl(url, post_data=data)
+ data = self._ParseAndCheckTwitter(json)
+ return Status.NewFromJsonDict(data)
+
def GetUserRetweets(self, count=None, since_id=None, max_id=None, include_entities=False):
'''Fetch the sequence of retweets made by a single user.
View
7 twitter_test.py
@@ -415,6 +415,13 @@ def testPostUpdate(self):
# This is rather arbitrary, but spot checking is better than nothing
self.assertEqual(u'Моё судно на воздушной подушке полно угрей', status.text)
+ def testPostRetweet(self):
+ '''Test the twitter.Api PostRetweet method'''
+ self._AddHandler('https://api.twitter.com/1/statuses/retweet/89512102.json',
+ curry(self._OpenTestData, 'retweet.json'))
+ status = self._api.PostRetweet(89512102)
+ self.assertEqual(89512102, status.id)
+
def testPostUpdateLatLon(self):
'''Test the twitter.Api PostUpdate method, when used in conjunction with latitude and longitude'''
self._AddHandler('https://api.twitter.com/1/statuses/update.json',
Please sign in to comment.
Something went wrong with that request. Please try again.