Moved around some code, support for removing and updating Profile Banners
Line 213 needs to check for status code as well now because remove/updating banner does not return content, only status code
This commit is contained in:
parent
cc31322102
commit
a3967390e1
2 changed files with 54 additions and 18 deletions
|
|
@ -163,6 +163,10 @@ api_table = {
|
||||||
'url': '/account/totals.json',
|
'url': '/account/totals.json',
|
||||||
'method': 'GET',
|
'method': 'GET',
|
||||||
},
|
},
|
||||||
|
'removeProfileBanner': {
|
||||||
|
'url': '/account/remove_profile_banner.json',
|
||||||
|
'method': 'POST',
|
||||||
|
},
|
||||||
|
|
||||||
# Favorites methods
|
# Favorites methods
|
||||||
'getFavorites': {
|
'getFavorites': {
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,6 @@ class Twython(object):
|
||||||
'content': content,
|
'content': content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# wrap the json loads in a try, and defer an error
|
# wrap the json loads in a try, and defer an error
|
||||||
# why? twitter will return invalid json with an error code in the headers
|
# why? twitter will return invalid json with an error code in the headers
|
||||||
json_error = False
|
json_error = False
|
||||||
|
|
@ -211,7 +210,7 @@ class Twython(object):
|
||||||
retry_after=response.headers.get('retry-after'))
|
retry_after=response.headers.get('retry-after'))
|
||||||
|
|
||||||
# if we have a json error here, then it's not an official TwitterAPI error
|
# if we have a json error here, then it's not an official TwitterAPI error
|
||||||
if json_error:
|
if json_error and not response.status_code in (200, 201, 202):
|
||||||
raise TwythonError('Response was not valid JSON, unable to decode.')
|
raise TwythonError('Response was not valid JSON, unable to decode.')
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
@ -400,8 +399,24 @@ class Twython(object):
|
||||||
for tweet in self.searchGen(search_query, **kwargs):
|
for tweet in self.searchGen(search_query, **kwargs):
|
||||||
yield tweet
|
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,
|
# The following methods are apart from the other Account methods,
|
||||||
# because they rely on a whole multipart-data posting function set.
|
# 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):
|
def updateProfileBackgroundImage(self, file_, tile=True, version=1):
|
||||||
"""Updates the authenticating user's profile background image.
|
"""Updates the authenticating user's profile background image.
|
||||||
|
|
||||||
|
|
@ -417,16 +432,6 @@ class Twython(object):
|
||||||
{'image': (file_, open(file_, 'rb'))},
|
{'image': (file_, open(file_, 'rb'))},
|
||||||
**{'tile': tile})
|
**{'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):
|
def updateProfileImage(self, file_, version=1):
|
||||||
"""Updates the authenticating user's profile image (avatar).
|
"""Updates the authenticating user's profile image (avatar).
|
||||||
|
|
||||||
|
|
@ -453,8 +458,22 @@ class Twython(object):
|
||||||
{'media': (file_, open(file_, 'rb'))},
|
{'media': (file_, open(file_, 'rb'))},
|
||||||
**params)
|
**params)
|
||||||
|
|
||||||
def _media_update(self, url, file_, **params):
|
def updateProfileBannerImage(self, file_, version=1, **params):
|
||||||
return self.post(url, params=params, files=file_)
|
"""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):
|
def getProfileImageUrl(self, username, size='normal', version=1):
|
||||||
"""Gets the URL for the user's profile image.
|
"""Gets the URL for the user's profile image.
|
||||||
|
|
@ -548,3 +567,16 @@ class Twython(object):
|
||||||
if isinstance(text, (str, unicode)):
|
if isinstance(text, (str, unicode)):
|
||||||
return Twython.unicode2utf8(text)
|
return Twython.unicode2utf8(text)
|
||||||
return str(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')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue