[ci skip]
This commit is contained in:
Mike Helmick 2013-05-16 12:40:25 -04:00
parent 008c53048a
commit ea0b646fd1
3 changed files with 39 additions and 113 deletions

View file

@ -56,7 +56,10 @@ api_table = {
'url': '/statuses/retweet/{{id}}.json', 'url': '/statuses/retweet/{{id}}.json',
'method': 'POST', 'method': 'POST',
}, },
# See twython.py for update_status_with_media 'update_status_with_media': {
'url': '/statuses/update_with_media.json',
'method': 'POST',
},
'get_oembed_tweet': { 'get_oembed_tweet': {
'url': '/statuses/oembed.json', 'url': '/statuses/oembed.json',
'method': 'GET', 'method': 'GET',
@ -169,12 +172,18 @@ api_table = {
'url': '/account/update_profile.json', 'url': '/account/update_profile.json',
'method': 'POST', 'method': 'POST',
}, },
# See twython.py for update_profile_background_image 'update_profile_background_image': {
'url': '/account/update_profile_banner.json',
'method': 'POST',
},
'update_profile_colors': { 'update_profile_colors': {
'url': '/account/update_profile_colors.json', 'url': '/account/update_profile_colors.json',
'method': 'POST', 'method': 'POST',
}, },
# See twython.py for update_profile_image 'update_profile_image': {
'url': '/account/update_profile_image.json',
'method': 'POST',
},
'list_blocks': { 'list_blocks': {
'url': '/blocks/list.json', 'url': '/blocks/list.json',
'method': 'GET', 'method': 'GET',
@ -215,7 +224,10 @@ api_table = {
'url': '/account/remove_profile_banner.json', 'url': '/account/remove_profile_banner.json',
'method': 'POST', 'method': 'POST',
}, },
# See twython.py for update_profile_banner 'update_profile_background_image': {
'url': '/account/update_profile_background_image.json',
'method': 'POST',
},
'get_profile_banner_sizes': { 'get_profile_banner_sizes': {
'url': '/users/profile_banner.json', 'url': '/users/profile_banner.json',
'method': 'GET', 'method': 'GET',

16
twython/helpers.py Normal file
View file

@ -0,0 +1,16 @@
def _transparent_params(_params):
params = {}
files = {}
for k, v in _params.items():
if hasattr(v, 'read') and callable(v.read):
files[k] = v
elif isinstance(v, bool):
if v:
params[k] = 'true'
else:
params[k] = 'false'
elif isinstance(v, basestring):
params[k] = v
else:
continue
return params, files

View file

@ -9,6 +9,7 @@ from .advisory import TwythonDeprecationWarning
from .compat import json, urlencode, parse_qsl, quote_plus from .compat import json, urlencode, parse_qsl, quote_plus
from .endpoints import api_table from .endpoints import api_table
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
from .helpers import _transparent_params
warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 > warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 >
@ -126,7 +127,7 @@ class Twython(object):
return content return content
def _request(self, url, method='GET', params=None, files=None, api_call=None): def _request(self, url, method='GET', params=None, api_call=None):
'''Internal response generator, no sense in repeating the same '''Internal response generator, no sense in repeating the same
code twice, right? ;) code twice, right? ;)
''' '''
@ -134,6 +135,7 @@ class Twython(object):
params = params or {} params = params or {}
func = getattr(self.client, method) func = getattr(self.client, method)
params, files = _transparent_params(params)
if method == 'get': if method == 'get':
response = func(url, params=params) response = func(url, params=params)
else: else:
@ -201,7 +203,7 @@ class Twython(object):
we haven't gotten around to putting it in Twython yet. :) we haven't gotten around to putting it in Twython yet. :)
''' '''
def request(self, endpoint, method='GET', params=None, files=None, version='1.1'): def request(self, endpoint, method='GET', params=None, version='1.1'):
# In case they want to pass a full Twitter URL # In case they want to pass a full Twitter URL
# i.e. https://search.twitter.com/ # i.e. https://search.twitter.com/
if endpoint.startswith('http://') or endpoint.startswith('https://'): if endpoint.startswith('http://') or endpoint.startswith('https://'):
@ -209,15 +211,15 @@ class Twython(object):
else: else:
url = '%s/%s.json' % (self.api_url % version, endpoint) url = '%s/%s.json' % (self.api_url % version, endpoint)
content = self._request(url, method=method, params=params, files=files, api_call=url) content = self._request(url, method=method, params=params, api_call=url)
return content return content
def get(self, endpoint, params=None, version='1.1'): def get(self, endpoint, params=None, version='1.1'):
return self.request(endpoint, params=params, version=version) return self.request(endpoint, params=params, version=version)
def post(self, endpoint, params=None, files=None, version='1.1'): def post(self, endpoint, params=None, version='1.1'):
return self.request(endpoint, 'POST', params=params, files=files, version=version) return self.request(endpoint, 'POST', params=params, version=version)
# End Dynamic Request Methods # End Dynamic Request Methods
@ -380,110 +382,6 @@ class Twython(object):
for tweet in self.searchGen(search_query, **kwargs): for tweet in self.searchGen(search_query, **kwargs):
yield tweet yield tweet
# 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 updateProfileBackgroundImage(self, file_, version='1.1', **params):
warnings.warn(
'This method is deprecated, please use `update_profile_background_image` instead.',
TwythonDeprecationWarning,
stacklevel=2
)
return self.update_profile_background_image(file_, version, **params)
def update_profile_background_image(self, file_, version='1.1', **params):
"""Updates the authenticating user's profile background image.
:param file_: (required) A string to the location of the file
(less than 800KB in size, larger than 2048px width will scale down)
:param version: (optional) A number, default 1.1 because that's the
current API version for Twitter (Legacy = 1)
**params - You may pass items that are stated in this doc
(https://dev.twitter.com/docs/api/1.1/post/account/update_profile_background_image)
"""
return self.post('account/update_profile_background_image',
params=params,
files={'image': (file_, open(file_, 'rb'))},
version=version)
def updateProfileImage(self, file_, version='1.1', **params):
warnings.warn(
'This method is deprecated, please use `update_profile_image` instead.',
TwythonDeprecationWarning,
stacklevel=2
)
return self.update_profile_image(file_, version, **params)
def update_profile_image(self, file_, version='1.1', **params):
"""Updates the authenticating user's profile image (avatar).
:param file_: (required) A string to the location of the file
:param version: (optional) A number, default 1.1 because that's the
current API version for Twitter (Legacy = 1)
**params - You may pass items that are stated in this doc
(https://dev.twitter.com/docs/api/1.1/post/account/update_profile_image)
"""
return self.post('account/update_profile_image',
params=params,
files={'image': (file_, open(file_, 'rb'))},
version=version)
def updateStatusWithMedia(self, file_, version='1.1', **params):
warnings.warn(
'This method is deprecated, please use `update_status_with_media` instead.',
TwythonDeprecationWarning,
stacklevel=2
)
return self.update_status_with_media(file_, version, **params)
def update_status_with_media(self, file_, version='1.1', **params):
"""Updates the users status with media
:param file_: (required) A string to the location of the file
:param version: (optional) A number, default 1.1 because that's the
current API version for Twitter (Legacy = 1)
**params - You may pass items that are taken in this doc
(https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media)
"""
return self.post('statuses/update_with_media',
params=params,
files={'media': (file_, open(file_, 'rb'))},
version=version)
def updateProfileBannerImage(self, file_, version='1.1', **params):
warnings.warn(
'This method is deprecated, please use `update_profile_banner_image` instead.',
TwythonDeprecationWarning,
stacklevel=2
)
return self.update_profile_banner_image(file_, version, **params)
def update_profile_banner_image(self, file_, version='1.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 for Twitter that supports this call
**params - You may pass items that are taken in this doc
(https://dev.twitter.com/docs/api/1.1/post/account/update_profile_banner)
"""
return self.post('account/update_profile_banner',
params=params,
files={'banner': (file_, open(file_, 'rb'))},
version=version)
###########################################################################
@staticmethod @staticmethod
def unicode2utf8(text): def unicode2utf8(text):
try: try: