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

Commit f222e6f

Browse files
committed
cleaned up the code a bit
1 parent 6012902 commit f222e6f

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

twitter/api.py

Lines changed: 28 additions & 19 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
requests_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):
@@ -912,7 +918,6 @@ def PostMedia(self, status, media, possibly_sensitive=None,
912918
raise TwitterError("The twitter.Api instance must be authenticated.")
913919

914920
url = '%s/statuses/update_with_media.json' % self.base_url
915-
print url
916921

917922
if isinstance(status, unicode) or self._input_encoding is None:
918923
u_status = status
@@ -938,7 +943,6 @@ def PostMedia(self, status, media, possibly_sensitive=None,
938943
if display_coordinates:
939944
data['display_coordinates'] = 'true'
940945

941-
print media
942946
json = self._RequestUrl(url, 'POST', data=data)
943947
data = self._ParseAndCheckTwitter(json.content)
944948
return Status.NewFromJsonDict(data)
@@ -948,14 +952,14 @@ def PostMultipleMedia(self, status, media, possibly_sensitive=None,
948952
longitude=None, place_id=None,
949953
display_coordinates=False):
950954
'''
951-
Post a twitter status message from the authenticated user with a
952-
picture attached.
955+
Post a twitter status message from the authenticated user with
956+
multiple pictures attached.
953957
954958
Args:
955959
status:
956960
the text of your update
957961
media:
958-
location of media(PNG, JPG, GIF)
962+
location of multiple media elements(PNG, JPG, GIF)
959963
possibly_sensitive:
960964
set true is content is "advanced"
961965
in_reply_to_status_id:
@@ -974,38 +978,42 @@ def PostMultipleMedia(self, status, media, possibly_sensitive=None,
974978
if not self.__auth:
975979
raise TwitterError("The twitter.Api instance must be authenticated.")
976980

977-
upload_url = self.base_url.replace('api', 'upload')
978-
url = '%s/media/upload.json' % upload_url
981+
if type(media) is not list:
982+
raise TwitterError("Must by multiple media elements")
983+
984+
url = '%s/media/upload.json' % self.upload_url
979985

980986
if isinstance(status, unicode) or self._input_encoding is None:
981987
u_status = status
982988
else:
983989
u_status = unicode(status, self._input_encoding)
984990

985-
data = {}
986-
987-
if type(media) is not list:
988-
raise TwitterError("Must by multiple media elements")
989-
990991
media_ids = ''
991-
for m in media:
992+
for m in range(0,len(media)):
992993

993-
if m.startswith('http'):
994-
data['media'] = urllib2.urlopen(m).read()
994+
data = {}
995+
if not hasattr(media[m], 'read'):
996+
if media[m].startswith('http'):
997+
data['media'] = urllib2.urlopen(media[m]).read()
998+
else:
999+
data['media'] = open(str(media[m]), 'rb').read()
9951000
else:
996-
data['media'] = open(str(m), 'rb').read()
1001+
data['media'] = media[m].read()
9971002

9981003
json = self._RequestUrl(url, 'POST', data=data)
9991004
data = self._ParseAndCheckTwitter(json.content)
1005+
10001006
media_ids += str(data['media_id_string'])
1007+
if m is not len(media)-1:
1008+
media_ids += ","
10011009

1002-
data = {'status': status}
1010+
data = {'status': status, 'media_ids' : media_ids}
10031011

10041012
url = '%s/statuses/update.json' % self.base_url
1005-
url += "?media_ids="+media_ids
10061013

10071014
json = self._RequestUrl(url, 'POST', data=data)
10081015
data = self._ParseAndCheckTwitter(json.content)
1016+
return Status.NewFromJsonDict(data)
10091017

10101018
def PostUpdates(self, status, continuation=None, **kwargs):
10111019
'''Post one or more twitter status messages from the authenticated user.
@@ -3434,8 +3442,9 @@ def _RequestUrl(self, url, verb, data=None):
34343442
A JSON object.
34353443
'''
34363444
if verb == 'POST':
3445+
if data.has_key('media_ids'):
3446+
url = self._BuildUrl(url, extra_params={'media_ids' : data['media_ids']})
34373447
if data.has_key('media'):
3438-
print "sending data"
34393448
try:
34403449
return requests.post(
34413450
url,

0 commit comments

Comments
 (0)