diff --git a/HISTORY.rst b/HISTORY.rst index 015f38d..fc82b5e 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -22,6 +22,9 @@ just define it - Headers now always include the User-Agent as Twython vXX unless User-Agent is overwritten - Removed senseless TwythonError thrown if method is not GET or POST, who cares -- if the user passes something other than GET or POST just let Twitter return the error that they messed up - Removed conversion to unicode of (int, bool) params passed to a requests. ``requests`` isn't greedy about variables that can't be converted to unicode anymore +- Removed `bulkUserLookup` (please use `lookupUser` instead), removed `getProfileImageUrl` (will be completely removed from Twitter API on May 7th, 2013) +- Updated shortenUrl to actually work for those using it, but it is being deprecated since `requests` makes it easy for developers to implement their own url shortening in their app (see https://github.com/ryanmcgrath/twython/issues/184) +- Twython Deprecation Warnings will now be seen in shell when using Python 2.7 and greater 2.7.3 (2013-04-12) ++++++++++++++++++ diff --git a/twython/advisory.py b/twython/advisory.py new file mode 100644 index 0000000..edff80e --- /dev/null +++ b/twython/advisory.py @@ -0,0 +1,5 @@ +class TwythonDeprecationWarning(DeprecationWarning): + """Custom DeprecationWarning to be raised when methods/variables are being deprecated in Twython. + Python 2.7 > ignores DeprecationWarning so we want to specifcally bubble up ONLY Twython Deprecation Warnings + """ + pass diff --git a/twython/compat.py b/twython/compat.py index 48caa46..8da417e 100644 --- a/twython/compat.py +++ b/twython/compat.py @@ -13,13 +13,12 @@ try: except ImportError: import json -try: - from urlparse import parse_qsl -except ImportError: - from cgi import parse_qsl - if is_py2: from urllib import urlencode, quote_plus + try: + from urlparse import parse_qsl + except ImportError: + from cgi import parse_qsl builtin_str = str bytes = str @@ -29,7 +28,7 @@ if is_py2: elif is_py3: - from urllib.parse import urlencode, quote_plus + from urllib.parse import urlencode, quote_plus, parse_qsl builtin_str = str str = str diff --git a/twython/endpoints.py b/twython/endpoints.py index 8fabb36..d63fc03 100644 --- a/twython/endpoints.py +++ b/twython/endpoints.py @@ -1,18 +1,18 @@ """ - A huge map of every Twitter API endpoint to a function definition in Twython. +A huge map of every Twitter API endpoint to a function definition in Twython. - Parameters that need to be embedded in the URL are treated with mustaches, e.g: +Parameters that need to be embedded in the URL are treated with mustaches, e.g: - {{version}}, etc +{{version}}, etc - When creating new endpoint definitions, keep in mind that the name of the mustache - will be replaced with the keyword that gets passed in to the function at call time. +When creating new endpoint definitions, keep in mind that the name of the mustache +will be replaced with the keyword that gets passed in to the function at call time. - i.e, in this case, if I pass version = 47 to any function, {{version}} will be replaced - with 47, instead of defaulting to 1.1 (said defaulting takes place at conversion time). +i.e, in this case, if I pass version = 47 to any function, {{version}} will be replaced +with 47, instead of defaulting to 1.1 (said defaulting takes place at conversion time). - This map is organized the order functions are documented at: - https://dev.twitter.com/docs/api/1.1 +This map is organized the order functions are documented at: +https://dev.twitter.com/docs/api/1.1 """ api_table = { diff --git a/twython/twython.py b/twython/twython.py index 02ba05e..19f2f68 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -1,15 +1,17 @@ import re import warnings -warnings.simplefilter('default') # For Python 2.7 > import requests from requests_oauthlib import OAuth1 +from .advisory import TwythonDeprecationWarning from .compat import json, urlencode, parse_qsl, quote_plus from .endpoints import api_table from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError from .version import __version__ +warnings.simplefilter('always', TwythonDeprecationWarning) # For Python 2.7 > + class Twython(object): def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, @@ -42,14 +44,14 @@ class Twython(object): if twitter_token or twitter_secret: warnings.warn( 'Instead of twitter_token or twitter_secret, please use app_key or app_secret (respectively).', - DeprecationWarning, + TwythonDeprecationWarning, stacklevel=2 ) if callback_url: warnings.warn( 'Please pass callback_url to the get_authentication_tokens method rather than Twython.__init__', - DeprecationWarning, + TwythonDeprecationWarning, stacklevel=2 ) @@ -267,7 +269,7 @@ class Twython(object): # ------------------------------------------------------------------------------------------------------------------------ @staticmethod - def shortenURL(url_to_shorten, shortener='http://is.gd/api.php'): + def shortenURL(url_to_shorten, shortener='http://is.gd/create.php'): """Shortens url specified by url_to_shorten. Note: Twitter automatically shortens all URLs behind their own custom t.co shortener now, but we keep this here for anyone who was previously using it for alternative purposes. ;) @@ -276,11 +278,18 @@ class Twython(object): :param shortener: (optional) In case you want to use a different URL shortening service """ + warnings.warn( + 'With requests it\'s easy enough for a developer to implement url shortenting themselves. Please see: https://github.com/ryanmcgrath/twython/issues/184', + TwythonDeprecationWarning, + stacklevel=2 + ) + if shortener == '': raise TwythonError('Please provide a URL shortening service.') request = requests.get(shortener, params={ - 'query': url_to_shorten + 'format': 'json', + 'url': url_to_shorten }) if request.status_code in [301, 201, 200]: @@ -290,7 +299,7 @@ class Twython(object): @staticmethod def constructApiURL(base_url, params): - return base_url + "?" + "&".join(["%s=%s" % (Twython.unicode2utf8(key), quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()]) + return base_url + '?' + '&'.join(['%s=%s' % (Twython.unicode2utf8(key), quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()]) def searchGen(self, search_query, **kwargs): """ Returns a generator of tweets that match a specified query. @@ -312,29 +321,14 @@ class Twython(object): for tweet in content['results']: yield tweet - if 'page' not in kwargs: - kwargs['page'] = 2 - else: - try: - kwargs['page'] = int(kwargs['page']) - kwargs['page'] += 1 - kwargs['page'] = str(kwargs['page']) - except TypeError: - raise TwythonError("searchGen() exited because page takes type str") + try: + kwargs['page'] = 2 if not 'page' in kwargs else (int(kwargs['page']) + 1) + except (TypeError, ValueError): + raise TwythonError('Unable to generate next page of search results, `page` is not a number.') 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. @@ -407,14 +401,6 @@ class Twython(object): ########################################################################### - def getProfileImageUrl(self, username, size='normal', version='1'): - warnings.warn( - "This function has been deprecated. Twitter API v1.1 will not have a dedicated endpoint \ - for this functionality.", - DeprecationWarning, - stacklevel=2 - ) - @staticmethod def stream(data, callback): """A Streaming API endpoint, because requests (by Kenneth Reitz)