Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit 17ab355

Browse files
committed
Merge pull request #180 from greedo/multiple_media_entities
Multiple media entities
2 parents 5f0627c + c82f1d1 commit 17ab355

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

twitter/api.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def __init__(self,
117117
shortner=None,
118118
base_url=None,
119119
stream_url=None,
120+
upload_url=None,
120121
use_gzip_compression=False,
121122
debugHTTP=False,
122123
timeout=None):
@@ -178,6 +179,11 @@ def __init__(self,
178179
self.stream_url = 'https://stream.twitter.com/1.1'
179180
else:
180181
self.stream_url = stream_url
182+
183+
if upload_url is None:
184+
self.upload_url = 'https://upload.twitter.com/1.1'
185+
else:
186+
self.upload_url = upload_url
181187

182188
if consumer_key is not None and (access_token_key is None or
183189
access_token_secret is None):
@@ -951,6 +957,74 @@ def PostMedia(self,
951957

952958
return Status.NewFromJsonDict(data)
953959

960+
def PostMultipleMedia(self, status, media, possibly_sensitive=None,
961+
in_reply_to_status_id=None, latitude=None,
962+
longitude=None, place_id=None,
963+
display_coordinates=False):
964+
'''
965+
Post a twitter status message from the authenticated user with
966+
multiple pictures attached.
967+
968+
Args:
969+
status:
970+
the text of your update
971+
media:
972+
location of multiple media elements(PNG, JPG, GIF)
973+
possibly_sensitive:
974+
set true is content is "advanced"
975+
in_reply_to_status_id:
976+
ID of a status that this is in reply to
977+
lat:
978+
location in latitude
979+
long:
980+
location in longitude
981+
place_id:
982+
A place in the world identified by a Twitter place ID
983+
display_coordinates:
984+
985+
Returns:
986+
A twitter.Status instance representing the message posted.
987+
'''
988+
if not self.__auth:
989+
raise TwitterError("The twitter.Api instance must be authenticated.")
990+
991+
if type(media) is not list:
992+
raise TwitterError("Must by multiple media elements")
993+
994+
url = '%s/media/upload.json' % self.upload_url
995+
996+
if isinstance(status, unicode) or self._input_encoding is None:
997+
u_status = status
998+
else:
999+
u_status = unicode(status, self._input_encoding)
1000+
1001+
media_ids = ''
1002+
for m in range(0,len(media)):
1003+
1004+
data = {}
1005+
if not hasattr(media[m], 'read'):
1006+
if media[m].startswith('http'):
1007+
data['media'] = urllib2.urlopen(media[m]).read()
1008+
else:
1009+
data['media'] = open(str(media[m]), 'rb').read()
1010+
else:
1011+
data['media'] = media[m].read()
1012+
1013+
json = self._RequestUrl(url, 'POST', data=data)
1014+
data = self._ParseAndCheckTwitter(json.content)
1015+
1016+
media_ids += str(data['media_id_string'])
1017+
if m is not len(media)-1:
1018+
media_ids += ","
1019+
1020+
data = {'status': status, 'media_ids' : media_ids}
1021+
1022+
url = '%s/statuses/update.json' % self.base_url
1023+
1024+
json = self._RequestUrl(url, 'POST', data=data)
1025+
data = self._ParseAndCheckTwitter(json.content)
1026+
return Status.NewFromJsonDict(data)
1027+
9541028
def PostUpdates(self,
9551029
status,
9561030
continuation=None,
@@ -3463,6 +3537,8 @@ def _RequestUrl(self, url, verb, data=None):
34633537
A JSON object.
34643538
'''
34653539
if verb == 'POST':
3540+
if data.has_key('media_ids'):
3541+
url = self._BuildUrl(url, extra_params={'media_ids' : data['media_ids']})
34663542
if data.has_key('media'):
34673543
try:
34683544
return requests.post(

0 commit comments

Comments
 (0)