diff --git a/twython/twitter_endpoints.py b/twython/twitter_endpoints.py index aeb9941..9bc6806 100644 --- a/twython/twitter_endpoints.py +++ b/twython/twitter_endpoints.py @@ -160,9 +160,13 @@ api_table = { 'method': 'POST', }, 'myTotals': { - 'url' : '/account/totals.json', + 'url': '/account/totals.json', 'method': 'GET', }, + 'removeProfileBanner': { + 'url': '/account/remove_profile_banner.json', + 'method': 'POST', + }, # Favorites methods 'getFavorites': { diff --git a/twython/twython.py b/twython/twython.py index 3fa2b5d..f16ab68 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -185,15 +185,14 @@ class Twython(object): 'content': content, } - # wrap the json loads in a try, and defer an error # why? twitter will return invalid json with an error code in the headers json_error = False try: content = simplejson.loads(content) except ValueError: - json_error= True - content= {} + json_error = True + content = {} if response.status_code > 304: # If there is no error message, use a default. @@ -210,8 +209,8 @@ class Twython(object): error_code=response.status_code, retry_after=response.headers.get('retry-after')) - # if we have a json error here , then it's not an official TwitterAPI error - if json_error: + # if we have a json error here, then it's not an official TwitterAPI error + if json_error and not response.status_code in (200, 201, 202): raise TwythonError('Response was not valid JSON, unable to decode.') return content @@ -400,8 +399,24 @@ class Twython(object): for tweet in self.searchGen(search_query, **kwargs): yield tweet + def bulkUserLookup(self, **kwargs): + """Stub for a method that has been deprecated, kept for now to raise errors + properly if people are relying on this (which they are...). + """ + warnings.warn( + "This function has been deprecated. Please migrate to .lookupUser() - params should be the same.", + DeprecationWarning, + stacklevel=2 + ) + # The following methods are apart from the other Account methods, # because they rely on a whole multipart-data posting function set. + + ## Media Uploading functions ############################################## + + def _media_update(self, url, file_, **params): + return self.post(url, params=params, files=file_) + def updateProfileBackgroundImage(self, file_, tile=True, version=1): """Updates the authenticating user's profile background image. @@ -417,16 +432,6 @@ class Twython(object): {'image': (file_, open(file_, 'rb'))}, **{'tile': tile}) - def bulkUserLookup(self, **kwargs): - """Stub for a method that has been deprecated, kept for now to raise errors - properly if people are relying on this (which they are...). - """ - warnings.warn( - "This function has been deprecated. Please migrate to .lookupUser() - params should be the same.", - DeprecationWarning, - stacklevel=2 - ) - def updateProfileImage(self, file_, version=1): """Updates the authenticating user's profile image (avatar). @@ -453,8 +458,22 @@ class Twython(object): {'media': (file_, open(file_, 'rb'))}, **params) - def _media_update(self, url, file_, **params): - return self.post(url, params=params, files=file_) + def updateProfileBannerImage(self, file_, version=1, **params): + """Updates the users profile banner + + :param file_: (required) A string to the location of the file + :param version: (optional) A number, default 1 because that's the + only API version Twitter has now + + **params - You may pass items that are taken in this doc + (https://dev.twitter.com/docs/api/1/post/account/update_profile_banner) + """ + url = 'https://api.twitter.com/%d/account/update_profile_banner.json' % version + return self._media_update(url, + {'banner': (file_, open(file_, 'rb'))}, + **params) + + ########################################################################### def getProfileImageUrl(self, username, size='normal', version=1): """Gets the URL for the user's profile image. @@ -548,3 +567,16 @@ class Twython(object): if isinstance(text, (str, unicode)): return Twython.unicode2utf8(text) return str(text) + +if __name__ == '__main__': + app_key = 'KEkNAu84zC5brBehnhAz9g' + app_secret = 'Z0KOh2Oyf1yDVnQAvRemIslaZfeDfaG79TJ4JoJHXbk' + oauth_token = '142832463-U6l4WX5pnxSY9wdDWE0Ahzz03yYuhiUvsIjBAyOH' + oauth_token_secret = 'PJBXfanIZ89hLLDI7ylNDvWyqALVxBMOBELhLW0A' + + t = Twython(app_key=app_key, + app_secret=app_secret, + oauth_token=oauth_token, + oauth_token_secret=oauth_token_secret) + + print t.updateProfileBannerImage('/Users/mikehelmick/Desktop/Stuff/Screen Shot 2012-08-15 at 2.54.58 PM.png')