Merging in a changeset to fix updateProfileColors() to use a proper POST method, instead of using a GET (thanks to Pedro Varangot for the submitted patch)

This commit is contained in:
Ryan McGrath 2010-02-22 23:03:30 -05:00
parent 844c1ae235
commit 30fbacb066
2 changed files with 119 additions and 20 deletions

View file

@ -139,12 +139,13 @@ class setup:
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
"""
version = version or self.apiVersion
try:
if rate_for == "requestingIP":
return simplejson.load(self.opener.open("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
else:
if self.authenticated is True:
return simplejson.load(self.opener.open("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
return simplejson.load(urllib2.urlopen("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
else:
raise TwythonError("You need to be authenticated to check a rate limit status on an account.")
except HTTPError, e:
@ -873,8 +874,14 @@ class setup:
else:
raise AuthError("updateDeliveryDevice() requires you to be authenticated.")
def updateProfileColors(self, version = None, **kwargs):
"""updateProfileColors(**kwargs)
def updateProfileColors(self,
profile_background_color = None,
profile_text_color = None,
profile_link_color = None,
profile_sidebar_fill_color = None,
profile_sidebar_border_color = None,
version = None):
"""updateProfileColors()
Sets one or more hex values that control the color scheme of the authenticating user's profile page on api.twitter.com.
@ -890,10 +897,51 @@ class setup:
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
"""
version = version or self.apiVersion
if self.authenticated is True:
useAmpersands = False
updateProfileColorsQueryString = ""
def checkValidColor(str):
if len(str) != 6:
return False
for c in str:
if c not in "1234567890abcdefABCDEF": return False
return True
if profile_background_color is not None:
if checkValidColor(profile_background_color):
updateProfileColorsQueryString += "profile_background_color=" + profile_background_color
useAmpersands = True
else:
raise TwythonError("Invalid background color. Try an hexadecimal 6 digit number.")
if profile_text_color is not None:
if checkValidColor(profile_text_color):
updateProfileColorsQueryString += "profile_text_color=" + profile_text_color
useAmpersands = True
else:
raise TwythonError("Invalid text color. Try an hexadecimal 6 digit number.")
if profile_link_color is not None:
if checkValidColor(profile_link_color):
updateProfileColorsQueryString += "profile_link_color=" + profile_link_color
useAmpersands = True
else:
raise TwythonError("Invalid profile link color. Try an hexadecimal 6 digit number.")
if profile_sidebar_fill_color is not None:
if checkValidColor(profile_sidebar_fill_color):
updateProfileColorsQueryString += "profile_sidebar_fill_color=" + profile_sidebar_fill_color
useAmpersands = True
else:
raise TwythonError("Invalid sidebar fill color. Try an hexadecimal 6 digit number.")
if profile_sidebar_border_color is not None:
if checkValidColor(profile_sidebar_border_color):
updateProfileColorsQueryString += "profile_sidebar_border_color=" + profile_sidebar_border_color
useAmpersands = True
else:
raise TwythonError("Invalid sidebar border color. Try an hexadecimal 6 digit number.")
try:
return self.opener.open(self.constructApiURL("http://api.twitter.com/%d/account/update_profile_colors.json?" % version, kwargs))
return self.opener.open("http://api.twitter.com/%d/account/update_profile_colors.json?" % version, updateProfileColorsQueryString)
except HTTPError, e:
raise TwythonError("updateProfileColors() failed with a %s error code." % `e.code`, e.code)
else:

View file

@ -21,9 +21,15 @@ __version__ = "1.0"
"""Twython - Easy Twitter utilities in Python"""
try:
import json as simplejson
import simplejson
except ImportError:
raise Exception("Twython requires a json library to work. (Try http://www.undefined.org/python/ ?")
try:
import json as simplejson
except ImportError:
try:
from django.utils import simplejson
except:
raise Exception("Twython requires the simplejson library (or Python 2.6) to work. http://www.undefined.org/python/")
class TwythonError(Exception):
def __init__(self, msg, error_code=None):
@ -133,12 +139,13 @@ class setup:
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
"""
version = version or self.apiVersion
try:
if rate_for == "requestingIP":
return simplejson.load(self.opener.open("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
else:
if self.authenticated is True:
return simplejson.load(self.opener.open("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
return simplejson.load(urllib.request.urlopen("http://api.twitter.com/%d/account/rate_limit_status.json" % version))
else:
raise TwythonError("You need to be authenticated to check a rate limit status on an account.")
except HTTPError as e:
@ -453,10 +460,7 @@ class setup:
apiURL = "http://api.twitter.com/%d/users/show.json?screen_name=%s" % (version, screen_name)
if apiURL != "":
try:
if self.authenticated is True:
return simplejson.load(self.opener.open(apiURL))
else:
return simplejson.load(self.opener.open(apiURL))
return simplejson.load(self.opener.open(apiURL))
except HTTPError as e:
raise TwythonError("showUser() failed with a %s error code." % repr(e.code), e.code)
@ -870,8 +874,14 @@ class setup:
else:
raise AuthError("updateDeliveryDevice() requires you to be authenticated.")
def updateProfileColors(self, version = None, **kwargs):
"""updateProfileColors(**kwargs)
def updateProfileColors(self,
profile_background_color = None,
profile_text_color = None,
profile_link_color = None,
profile_sidebar_fill_color = None,
profile_sidebar_border_color = None,
version = None):
"""updateProfileColors()
Sets one or more hex values that control the color scheme of the authenticating user's profile page on api.twitter.com.
@ -887,10 +897,51 @@ class setup:
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
"""
version = version or self.apiVersion
if self.authenticated is True:
useAmpersands = False
updateProfileColorsQueryString = ""
def checkValidColor(str):
if len(str) != 6:
return False
for c in str:
if c not in "1234567890abcdefABCDEF": return False
return True
if profile_background_color is not None:
if checkValidColor(profile_background_color):
updateProfileColorsQueryString += "profile_background_color=" + profile_background_color
useAmpersands = True
else:
raise TwythonError("Invalid background color. Try an hexadecimal 6 digit number.")
if profile_text_color is not None:
if checkValidColor(profile_text_color):
updateProfileColorsQueryString += "profile_text_color=" + profile_text_color
useAmpersands = True
else:
raise TwythonError("Invalid text color. Try an hexadecimal 6 digit number.")
if profile_link_color is not None:
if checkValidColor(profile_link_color):
updateProfileColorsQueryString += "profile_link_color=" + profile_link_color
useAmpersands = True
else:
raise TwythonError("Invalid profile link color. Try an hexadecimal 6 digit number.")
if profile_sidebar_fill_color is not None:
if checkValidColor(profile_sidebar_fill_color):
updateProfileColorsQueryString += "profile_sidebar_fill_color=" + profile_sidebar_fill_color
useAmpersands = True
else:
raise TwythonError("Invalid sidebar fill color. Try an hexadecimal 6 digit number.")
if profile_sidebar_border_color is not None:
if checkValidColor(profile_sidebar_border_color):
updateProfileColorsQueryString += "profile_sidebar_border_color=" + profile_sidebar_border_color
useAmpersands = True
else:
raise TwythonError("Invalid sidebar border color. Try an hexadecimal 6 digit number.")
try:
return self.opener.open(self.constructApiURL("http://api.twitter.com/%d/account/update_profile_colors.json?" % version, kwargs))
return self.opener.open("http://api.twitter.com/%d/account/update_profile_colors.json?" % version, updateProfileColorsQueryString)
except HTTPError as e:
raise TwythonError("updateProfileColors() failed with a %s error code." % repr(e.code), e.code)
else: