v1.5.0 release
- requests is now the default url/http library, thanks to Mike Helmick
- Initial pass at a Streaming API is now included (Twython.stream()), due to how easy
requests makes it. Would actually be sad if we *didn't* have this... thanks, Kenneth. >_>;
- Return of shortenURL, for people who may have relied on it before.
- Deleted streaming handler that existed before but never got implemented fully.
- Exceptions now prefixed with Twython, but brought back originals with a more verbose error directing
people to new ones, deprecate fully in future.
- Twython3k now has an OAuth fix for callback_urls, though it still relies on httplib2. Thanks @jbouvier!
- Added a list of contributors to the README files, something which I should have done long ago. Thank you all.
This commit is contained in:
parent
1a6852de54
commit
9deced8f8b
6 changed files with 266 additions and 152 deletions
|
|
@ -88,4 +88,30 @@ My hope is that Twython is so simple that you'd never *have* to ask any question
|
||||||
you feel the need to contact me for this (or other) reasons, you can hit me up
|
you feel the need to contact me for this (or other) reasons, you can hit me up
|
||||||
at ryan@venodesigns.net.
|
at ryan@venodesigns.net.
|
||||||
|
|
||||||
|
You can also follow me on Twitter - **[@ryanmcgrath](http://twitter.com/ryanmcgrath)**.
|
||||||
|
|
||||||
Twython is released under an MIT License - see the LICENSE file for more information.
|
Twython is released under an MIT License - see the LICENSE file for more information.
|
||||||
|
|
||||||
|
Special Thanks to...
|
||||||
|
-----------------------------------------------------------------------------------------------------
|
||||||
|
This is a list of all those who have contributed code to Twython in some way, shape, or form. I think it's
|
||||||
|
exhaustive, but I could be wrong - if you think your name should be here and it's not, please contact
|
||||||
|
me and let me know (or just issue a pull request on GitHub, and leave a note about it so I can just accept it ;)).
|
||||||
|
|
||||||
|
- **[Mike Helmick (michaelhelmick)](https://github.com/michaelhelmick)**, multiple fixes and proper `requests` integration.
|
||||||
|
- **[kracekumar](https://github.com/kracekumar)**, early `requests` work and various fixes.
|
||||||
|
- **[Erik Scheffers (eriks5)](https://github.com/eriks5)**, various fixes regarding OAuth callback URLs.
|
||||||
|
- **[Jordan Bouvier (jbouvier)](https://github.com/jbouvier)**, various fixes regarding OAuth callback URLs.
|
||||||
|
- **[Dick Brouwer (dikbrouwer)](https://github.com/dikbrouwer)**, fixes for OAuth Verifier in `get_authorized_tokens`.
|
||||||
|
- **[hades](https://github.com/hades)**, Fixes to various initial OAuth issues and updates to `Twython3k` to stay current.
|
||||||
|
- **[Alex Sutton (alexdsutton)](https://github.com/alexsdutton/twython/)**, fix for parameter substitution regular expression (catch underscores!).
|
||||||
|
- **[Levgen Pyvovarov (bsn)](https://github.com/bsn)**, Various argument fixes, cyrillic text support.
|
||||||
|
- **[Mark Liu (mliu7)](https://github.com/mliu7)**, Missing parameter fix for `addListMember`.
|
||||||
|
- **[Randall Degges (rdegges)](https://github.com/rdegges)**, PEP-8 fixes, MANIFEST.in, installer fixes.
|
||||||
|
- **[Idris Mokhtarzada (idris)](https://github.com/idris)**, Fixes for various example code pieces.
|
||||||
|
- **[Jonathan Elsas (jelsas)](https://github.com/jelsas)**, Fix for original Streaming API stub causing import errors.
|
||||||
|
- **[LuqueDaniel](https://github.com/LuqueDaniel)**, Extended example code where necessary.
|
||||||
|
- **[Mesar Hameed (mhameed)](https://github.com/mhameed)**, Commit to swap `__getattr__` trick for a more debuggable solution.
|
||||||
|
- **[Remy DeCausemaker (decause)](https://github.com/decause)**, PEP-8 contributions.
|
||||||
|
- **[mckellister](https://github.com/mckellister)**, Fixes to `Exception`s raised by Twython (Rate Limits, etc).
|
||||||
|
- **[tatz_tsuchiya](http://d.hatena.ne.jp/tatz_tsuchiya/20120115/1326623451), Fix for `lambda` scoping in key injection phase.
|
||||||
|
|
|
||||||
51
README.txt
51
README.txt
|
|
@ -16,7 +16,7 @@ for those types of use cases. Twython cannot help you with that or fix the annoy
|
||||||
If you need OAuth, though, Twython now supports it, and ships with a skeleton Django application to get you started.
|
If you need OAuth, though, Twython now supports it, and ships with a skeleton Django application to get you started.
|
||||||
Enjoy!
|
Enjoy!
|
||||||
|
|
||||||
Requirements
|
Requirements (2.7 and below; for 3k, read section further down)
|
||||||
-----------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------
|
||||||
Twython (for versions of Python before 2.6) requires a library called
|
Twython (for versions of Python before 2.6) requires a library called
|
||||||
"simplejson". Depending on your flavor of package manager, you can do the following...
|
"simplejson". Depending on your flavor of package manager, you can do the following...
|
||||||
|
|
@ -35,20 +35,23 @@ Installing Twython is fairly easy. You can...
|
||||||
|
|
||||||
...or, you can clone the repo and install it the old fashioned way.
|
...or, you can clone the repo and install it the old fashioned way.
|
||||||
|
|
||||||
|
|
||||||
git clone git://github.com/ryanmcgrath/twython.git
|
git clone git://github.com/ryanmcgrath/twython.git
|
||||||
cd twython
|
cd twython
|
||||||
sudo python setup.py install
|
sudo python setup.py install
|
||||||
|
|
||||||
Example Use
|
Example Use
|
||||||
-----------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------
|
||||||
from twython import Twython
|
``` python
|
||||||
|
from twython import Twython
|
||||||
|
|
||||||
twitter = Twython()
|
twitter = Twython()
|
||||||
results = twitter.searchTwitter(q="bert")
|
results = twitter.search(q = "bert")
|
||||||
|
|
||||||
# More function definitions can be found by reading over twython/twitter_endpoints.py, as well
|
# More function definitions can be found by reading over twython/twitter_endpoints.py, as well
|
||||||
# as skimming the source file. Both are kept human-readable, and are pretty well documented or
|
# as skimming the source file. Both are kept human-readable, and are pretty well documented or
|
||||||
# very self documenting.
|
# very self documenting.
|
||||||
|
```
|
||||||
|
|
||||||
A note about the development of Twython (specifically, 1.3)
|
A note about the development of Twython (specifically, 1.3)
|
||||||
----------------------------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------------------------
|
||||||
|
|
@ -65,17 +68,19 @@ Arguments to functions are now exact keyword matches for the Twitter API documen
|
||||||
whatever query parameter arguments you read on Twitter's documentation (http://dev.twitter.com/doc) gets mapped
|
whatever query parameter arguments you read on Twitter's documentation (http://dev.twitter.com/doc) gets mapped
|
||||||
as a named argument to any Twitter function.
|
as a named argument to any Twitter function.
|
||||||
|
|
||||||
For example: the search API looks for arguments under the name "q", so you pass q="query_here" to searchTwitter().
|
For example: the search API looks for arguments under the name "q", so you pass q="query_here" to search().
|
||||||
|
|
||||||
Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up
|
Doing this allows us to be incredibly flexible in querying the Twitter API, so changes to the API aren't held up
|
||||||
from you using them by this library.
|
from you using them by this library.
|
||||||
|
|
||||||
Twython 3k
|
Twython 3k
|
||||||
-----------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------
|
||||||
There's an experimental version of Twython that's made for Python 3k. This is currently not guaranteed
|
There's an experimental version of Twython that's made for Python 3k. This is currently not guaranteed to
|
||||||
to work (especially with regards to OAuth), but it's provided so that others can grab it and hack on it.
|
work in all situations, but it's provided so that others can grab it and hack on it.
|
||||||
If you choose to try it out, be aware of this.
|
If you choose to try it out, be aware of this.
|
||||||
|
|
||||||
|
**OAuth is now working thanks to updates from [Hades](https://github.com/hades). You'll need to grab
|
||||||
|
his [Python 3 branch for python-oauth2](https://github.com/hades/python-oauth2/tree/python3) to have it work, though.**
|
||||||
|
|
||||||
Questions, Comments, etc?
|
Questions, Comments, etc?
|
||||||
-----------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------
|
||||||
|
|
@ -83,4 +88,30 @@ My hope is that Twython is so simple that you'd never *have* to ask any question
|
||||||
you feel the need to contact me for this (or other) reasons, you can hit me up
|
you feel the need to contact me for this (or other) reasons, you can hit me up
|
||||||
at ryan@venodesigns.net.
|
at ryan@venodesigns.net.
|
||||||
|
|
||||||
|
You can also follow me on Twitter - **[@ryanmcgrath](http://twitter.com/ryanmcgrath)**.
|
||||||
|
|
||||||
Twython is released under an MIT License - see the LICENSE file for more information.
|
Twython is released under an MIT License - see the LICENSE file for more information.
|
||||||
|
|
||||||
|
Special Thanks to...
|
||||||
|
-----------------------------------------------------------------------------------------------------
|
||||||
|
This is a list of all those who have contributed code to Twython in some way, shape, or form. I think it's
|
||||||
|
exhaustive, but I could be wrong - if you think your name should be here and it's not, please contact
|
||||||
|
me and let me know (or just issue a pull request on GitHub, and leave a note about it so I can just accept it ;)).
|
||||||
|
|
||||||
|
- **[Mike Helmick (michaelhelmick)](https://github.com/michaelhelmick)**, multiple fixes and proper `requests` integration.
|
||||||
|
- **[kracekumar](https://github.com/kracekumar)**, early `requests` work and various fixes.
|
||||||
|
- **[Erik Scheffers (eriks5)](https://github.com/eriks5)**, various fixes regarding OAuth callback URLs.
|
||||||
|
- **[Jordan Bouvier (jbouvier)](https://github.com/jbouvier)**, various fixes regarding OAuth callback URLs.
|
||||||
|
- **[Dick Brouwer (dikbrouwer)](https://github.com/dikbrouwer)**, fixes for OAuth Verifier in `get_authorized_tokens`.
|
||||||
|
- **[hades](https://github.com/hades)**, Fixes to various initial OAuth issues and updates to `Twython3k` to stay current.
|
||||||
|
- **[Alex Sutton (alexdsutton)](https://github.com/alexsdutton/twython/)**, fix for parameter substitution regular expression (catch underscores!).
|
||||||
|
- **[Levgen Pyvovarov (bsn)](https://github.com/bsn)**, Various argument fixes, cyrillic text support.
|
||||||
|
- **[Mark Liu (mliu7)](https://github.com/mliu7)**, Missing parameter fix for `addListMember`.
|
||||||
|
- **[Randall Degges (rdegges)](https://github.com/rdegges)**, PEP-8 fixes, MANIFEST.in, installer fixes.
|
||||||
|
- **[Idris Mokhtarzada (idris)](https://github.com/idris)**, Fixes for various example code pieces.
|
||||||
|
- **[Jonathan Elsas (jelsas)](https://github.com/jelsas)**, Fix for original Streaming API stub causing import errors.
|
||||||
|
- **[LuqueDaniel](https://github.com/LuqueDaniel)**, Extended example code where necessary.
|
||||||
|
- **[Mesar Hameed (mhameed)](https://github.com/mhameed)**, Commit to swap `__getattr__` trick for a more debuggable solution.
|
||||||
|
- **[Remy DeCausemaker (decause)](https://github.com/decause)**, PEP-8 contributions.
|
||||||
|
- **[mckellister](https://github.com/mckellister)**, Fixes to `Exception`s raised by Twython (Rate Limits, etc).
|
||||||
|
- **[tatz_tsuchiya](http://d.hatena.ne.jp/tatz_tsuchiya/20120115/1326623451), Fix for `lambda` scoping in key injection phase.
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -4,7 +4,7 @@ from setuptools import setup
|
||||||
from setuptools import find_packages
|
from setuptools import find_packages
|
||||||
|
|
||||||
__author__ = 'Ryan McGrath <ryan@venodesigns.net>'
|
__author__ = 'Ryan McGrath <ryan@venodesigns.net>'
|
||||||
__version__ = '1.4.6'
|
__version__ = '1.5.0'
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
# Basic package information.
|
# Basic package information.
|
||||||
|
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
"""
|
|
||||||
TwythonStreamer is an implementation of the Streaming API for Twython.
|
|
||||||
Pretty self explanatory by reading the code below. It's worth noting
|
|
||||||
that the end user should, ideally, never import this library, but rather
|
|
||||||
this is exposed via a linking method in Twython's core.
|
|
||||||
|
|
||||||
Questions, comments? ryan@venodesigns.net
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
|
||||||
__version__ = "1.0.0"
|
|
||||||
|
|
||||||
import urllib
|
|
||||||
import urllib2
|
|
||||||
import urlparse
|
|
||||||
import httplib
|
|
||||||
import httplib2
|
|
||||||
import re
|
|
||||||
|
|
||||||
from urllib2 import HTTPError
|
|
||||||
|
|
||||||
# There are some special setups (like, oh, a Django application) where
|
|
||||||
# simplejson exists behind the scenes anyway. Past Python 2.6, this should
|
|
||||||
# never really cause any problems to begin with.
|
|
||||||
try:
|
|
||||||
# Python 2.6 and up
|
|
||||||
import json as simplejson
|
|
||||||
except ImportError:
|
|
||||||
try:
|
|
||||||
# Python 2.6 and below (2.4/2.5, 2.3 is not guranteed to work with this library to begin with)
|
|
||||||
import simplejson
|
|
||||||
except ImportError:
|
|
||||||
try:
|
|
||||||
# This case gets rarer by the day, but if we need to, we can pull it from Django provided it's there.
|
|
||||||
from django.utils import simplejson
|
|
||||||
except:
|
|
||||||
# Seriously wtf is wrong with you if you get this Exception.
|
|
||||||
raise Exception("Twython requires the simplejson library (or Python 2.6) to work. http://www.undefined.org/python/")
|
|
||||||
|
|
||||||
class TwythonStreamingError(Exception):
|
|
||||||
def __init__(self, msg):
|
|
||||||
self.msg = msg
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return str(self.msg)
|
|
||||||
|
|
||||||
feeds = {
|
|
||||||
"firehose": "http://stream.twitter.com/firehose.json",
|
|
||||||
"gardenhose": "http://stream.twitter.com/gardenhose.json",
|
|
||||||
"spritzer": "http://stream.twitter.com/spritzer.json",
|
|
||||||
"birddog": "http://stream.twitter.com/birddog.json",
|
|
||||||
"shadow": "http://stream.twitter.com/shadow.json",
|
|
||||||
"follow": "http://stream.twitter.com/follow.json",
|
|
||||||
"track": "http://stream.twitter.com/track.json",
|
|
||||||
}
|
|
||||||
|
|
||||||
class Stream(object):
|
|
||||||
def __init__(self, username = None, password = None, feed = "spritzer", user_agent = "Twython Streaming"):
|
|
||||||
pass
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
||||||
__version__ = "1.4.6"
|
__version__ = "1.5.0"
|
||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
import re
|
import re
|
||||||
|
|
@ -95,6 +95,20 @@ class TwythonAPILimit(TwythonError):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
class APILimit(TwythonError):
|
||||||
|
"""
|
||||||
|
Raised when you've hit an API limit. Try to avoid these, read the API
|
||||||
|
docs if you're running into issues here, Twython does not concern itself with
|
||||||
|
this matter beyond telling you that you've done goofed.
|
||||||
|
|
||||||
|
DEPRECATED, import and catch TwythonAPILimit instead.
|
||||||
|
"""
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = '%s\n Notice: APILimit is deprecated and soon to be removed, catch on TwythonAPILimit instead!' % msg
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class TwythonRateLimitError(TwythonError):
|
class TwythonRateLimitError(TwythonError):
|
||||||
"""
|
"""
|
||||||
|
|
@ -121,6 +135,18 @@ class TwythonAuthError(TwythonError):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
|
class AuthError(TwythonError):
|
||||||
|
"""
|
||||||
|
Raised when you try to access a protected resource and it fails due to some issue with
|
||||||
|
your authentication.
|
||||||
|
"""
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = '%s\n Notice: AuthError is deprecated and soon to be removed, catch on TwythonAuthError instead!' % msg
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class Twython(object):
|
class Twython(object):
|
||||||
def __init__(self, twitter_token=None, twitter_secret=None, oauth_token=None, oauth_token_secret=None, \
|
def __init__(self, twitter_token=None, twitter_secret=None, oauth_token=None, oauth_token_secret=None, \
|
||||||
headers=None, callback_url=None):
|
headers=None, callback_url=None):
|
||||||
|
|
@ -140,72 +166,70 @@ class Twython(object):
|
||||||
** Note: versioning is not currently used by search.twitter functions;
|
** Note: versioning is not currently used by search.twitter functions;
|
||||||
when Twitter moves their junk, it'll be supported.
|
when Twitter moves their junk, it'll be supported.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
OAuthHook.consumer_key = twitter_token
|
OAuthHook.consumer_key = twitter_token
|
||||||
OAuthHook.consumer_secret = twitter_secret
|
OAuthHook.consumer_secret = twitter_secret
|
||||||
|
|
||||||
# Needed for hitting that there API.
|
# Needed for hitting that there API.
|
||||||
self.request_token_url = 'http://twitter.com/oauth/request_token'
|
self.request_token_url = 'http://twitter.com/oauth/request_token'
|
||||||
self.access_token_url = 'http://twitter.com/oauth/access_token'
|
self.access_token_url = 'http://twitter.com/oauth/access_token'
|
||||||
self.authorize_url = 'http://twitter.com/oauth/authorize'
|
self.authorize_url = 'http://twitter.com/oauth/authorize'
|
||||||
self.authenticate_url = 'http://twitter.com/oauth/authenticate'
|
self.authenticate_url = 'http://twitter.com/oauth/authenticate'
|
||||||
|
|
||||||
self.twitter_token = twitter_token
|
self.twitter_token = twitter_token
|
||||||
self.twitter_secret = twitter_secret
|
self.twitter_secret = twitter_secret
|
||||||
self.oauth_token = oauth_token
|
self.oauth_token = oauth_token
|
||||||
self.oauth_secret = oauth_token_secret
|
self.oauth_secret = oauth_token_secret
|
||||||
self.callback_url = callback_url
|
self.callback_url = callback_url
|
||||||
|
|
||||||
# If there's headers, set them, otherwise be an embarassing parent for their own good.
|
# If there's headers, set them, otherwise be an embarassing parent for their own good.
|
||||||
self.headers = headers
|
self.headers = headers
|
||||||
if self.headers is None:
|
if self.headers is None:
|
||||||
self.headers = {'User-agent': 'Twython Python Twitter Library v1.4.6'}
|
self.headers = {'User-agent': 'Twython Python Twitter Library v' + __version__}
|
||||||
|
|
||||||
self.client = None
|
self.client = None
|
||||||
|
|
||||||
if self.twitter_token is not None and self.twitter_secret is not None:
|
if self.twitter_token is not None and self.twitter_secret is not None:
|
||||||
self.client = requests.session(hooks={'pre_request': OAuthHook()})
|
self.client = requests.session(hooks={'pre_request': OAuthHook()})
|
||||||
|
|
||||||
if self.oauth_token is not None and self.oauth_secret is not None:
|
if self.oauth_token is not None and self.oauth_secret is not None:
|
||||||
self.oauth_hook = OAuthHook(self.oauth_token, self.oauth_secret)
|
self.oauth_hook = OAuthHook(self.oauth_token, self.oauth_secret)
|
||||||
self.client = requests.session(hooks={'pre_request': self.oauth_hook})
|
self.client = requests.session(hooks={'pre_request': self.oauth_hook})
|
||||||
|
|
||||||
# Filter down through the possibilities here - if they have a token, if they're first stage, etc.
|
# Filter down through the possibilities here - if they have a token, if they're first stage, etc.
|
||||||
if self.client is None:
|
if self.client is None:
|
||||||
# If they don't do authentication, but still want to request unprotected resources, we need an opener.
|
# If they don't do authentication, but still want to request unprotected resources, we need an opener.
|
||||||
self.client = requests.session()
|
self.client = requests.session()
|
||||||
|
|
||||||
# register available funcs to allow listing name when debugging.
|
# register available funcs to allow listing name when debugging.
|
||||||
def setFunc(key):
|
def setFunc(key):
|
||||||
return lambda **kwargs: self._constructFunc(key, **kwargs)
|
return lambda **kwargs: self._constructFunc(key, **kwargs)
|
||||||
for key in api_table.keys():
|
for key in api_table.keys():
|
||||||
self.__dict__[key] = setFunc(key)
|
self.__dict__[key] = setFunc(key)
|
||||||
|
|
||||||
def _constructFunc(self, api_call, **kwargs):
|
def _constructFunc(self, api_call, **kwargs):
|
||||||
# Go through and replace any mustaches that are in our API url.
|
# Go through and replace any mustaches that are in our API url.
|
||||||
fn = api_table[api_call]
|
fn = api_table[api_call]
|
||||||
base = re.sub(
|
base = re.sub(
|
||||||
'\{\{(?P<m>[a-zA-Z_]+)\}\}',
|
'\{\{(?P<m>[a-zA-Z_]+)\}\}',
|
||||||
# The '1' here catches the API version. Slightly
|
# The '1' here catches the API version. Slightly hilarious.
|
||||||
# hilarious.
|
|
||||||
lambda m: "%s" % kwargs.get(m.group(1), '1'),
|
lambda m: "%s" % kwargs.get(m.group(1), '1'),
|
||||||
base_url + fn['url']
|
base_url + fn['url']
|
||||||
)
|
)
|
||||||
|
|
||||||
method = fn['method'].lower()
|
method = fn['method'].lower()
|
||||||
if not method in ('get', 'post', 'delete'):
|
if not method in ('get', 'post', 'delete'):
|
||||||
raise TwythonError('Method must be of GET, POST or DELETE')
|
raise TwythonError('Method must be of GET, POST or DELETE')
|
||||||
|
|
||||||
if method == 'get':
|
if method == 'get':
|
||||||
myargs = ['%s=%s' % (key, value) for (key, value) in kwargs.iteritems()]
|
myargs = ['%s=%s' % (key, value) for (key, value) in kwargs.iteritems()]
|
||||||
else:
|
else:
|
||||||
myargs = kwargs
|
myargs = kwargs
|
||||||
|
|
||||||
func = getattr(self.client, method)
|
func = getattr(self.client, method)
|
||||||
response = func(base, data=myargs)
|
response = func(base, data=myargs)
|
||||||
|
|
||||||
return simplejson.loads(response.content.decode('utf-8'))
|
return simplejson.loads(response.content.decode('utf-8'))
|
||||||
|
|
||||||
def get_authentication_tokens(self):
|
def get_authentication_tokens(self):
|
||||||
"""
|
"""
|
||||||
get_auth_url(self)
|
get_auth_url(self)
|
||||||
|
|
@ -213,41 +237,41 @@ class Twython(object):
|
||||||
Returns an authorization URL for a user to hit.
|
Returns an authorization URL for a user to hit.
|
||||||
"""
|
"""
|
||||||
callback_url = self.callback_url or 'oob'
|
callback_url = self.callback_url or 'oob'
|
||||||
|
|
||||||
request_args = {}
|
request_args = {}
|
||||||
request_args['oauth_callback'] = callback_url
|
request_args['oauth_callback'] = callback_url
|
||||||
method = 'get'
|
method = 'get'
|
||||||
|
|
||||||
if not OAUTH_LIB_SUPPORTS_CALLBACK:
|
if not OAUTH_LIB_SUPPORTS_CALLBACK:
|
||||||
method = 'post'
|
method = 'post'
|
||||||
|
|
||||||
func = getattr(self.client, method)
|
func = getattr(self.client, method)
|
||||||
response = func(self.request_token_url, data=request_args)
|
response = func(self.request_token_url, data=request_args)
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise TwythonAuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (response.status_code, response.content))
|
raise TwythonAuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (response.status_code, response.content))
|
||||||
|
|
||||||
request_tokens = dict(parse_qsl(response.content))
|
request_tokens = dict(parse_qsl(response.content))
|
||||||
if not request_tokens:
|
if not request_tokens:
|
||||||
raise TwythonError('Unable to decode request tokens.')
|
raise TwythonError('Unable to decode request tokens.')
|
||||||
|
|
||||||
oauth_callback_confirmed = request_tokens.get('oauth_callback_confirmed') == 'true'
|
oauth_callback_confirmed = request_tokens.get('oauth_callback_confirmed') == 'true'
|
||||||
|
|
||||||
if not OAUTH_LIB_SUPPORTS_CALLBACK and callback_url != 'oob' and oauth_callback_confirmed:
|
if not OAUTH_LIB_SUPPORTS_CALLBACK and callback_url != 'oob' and oauth_callback_confirmed:
|
||||||
import warnings
|
import warnings
|
||||||
warnings.warn("oauth2 library doesn't support OAuth 1.0a type callback, but remote requires it")
|
warnings.warn("oauth2 library doesn't support OAuth 1.0a type callback, but remote requires it")
|
||||||
oauth_callback_confirmed = False
|
oauth_callback_confirmed = False
|
||||||
|
|
||||||
auth_url_params = {
|
auth_url_params = {
|
||||||
'oauth_token': request_tokens['oauth_token'],
|
'oauth_token': request_tokens['oauth_token'],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use old-style callback argument
|
# Use old-style callback argument
|
||||||
if OAUTH_CALLBACK_IN_URL or (callback_url != 'oob' and not oauth_callback_confirmed):
|
if OAUTH_CALLBACK_IN_URL or (callback_url != 'oob' and not oauth_callback_confirmed):
|
||||||
auth_url_params['oauth_callback'] = callback_url
|
auth_url_params['oauth_callback'] = callback_url
|
||||||
|
|
||||||
request_tokens['auth_url'] = self.authenticate_url + '?' + urllib.urlencode(auth_url_params)
|
request_tokens['auth_url'] = self.authenticate_url + '?' + urllib.urlencode(auth_url_params)
|
||||||
|
|
||||||
return request_tokens
|
return request_tokens
|
||||||
|
|
||||||
def get_authorized_tokens(self):
|
def get_authorized_tokens(self):
|
||||||
|
|
@ -256,20 +280,41 @@ class Twython(object):
|
||||||
|
|
||||||
Returns authorized tokens after they go through the auth_url phase.
|
Returns authorized tokens after they go through the auth_url phase.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
response = self.client.get(self.access_token_url)
|
response = self.client.get(self.access_token_url)
|
||||||
authorized_tokens = dict(parse_qsl(response.content))
|
authorized_tokens = dict(parse_qsl(response.content))
|
||||||
if not authorized_tokens:
|
if not authorized_tokens:
|
||||||
raise TwythonError('Unable to decode authorized tokens.')
|
raise TwythonError('Unable to decode authorized tokens.')
|
||||||
|
|
||||||
return authorized_tokens
|
return authorized_tokens
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------------------------------------------------
|
||||||
# The following methods are all different in some manner or require special attention with regards to the Twitter API.
|
# The following methods are all different in some manner or require special attention with regards to the Twitter API.
|
||||||
# Because of this, we keep them separate from all the other endpoint definitions - ideally this should be change-able,
|
# Because of this, we keep them separate from all the other endpoint definitions - ideally this should be change-able,
|
||||||
# but it's not high on the priority list at the moment.
|
# but it's not high on the priority list at the moment.
|
||||||
# ------------------------------------------------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def shortenURL(url_to_shorten, shortener = "http://is.gd/api.php", query="longurl"):
|
||||||
|
"""
|
||||||
|
shortenURL(url_to_shorten, shortener = "http://is.gd/api.php", query="longurl")
|
||||||
|
|
||||||
|
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. ;)
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
url_to_shorten - URL to shorten.
|
||||||
|
shortener = In case you want to use a url shortening service other than is.gd.
|
||||||
|
"""
|
||||||
|
request = requests.get('http://is.gd/api.php' , params = {
|
||||||
|
'query': url_to_shorten
|
||||||
|
})
|
||||||
|
|
||||||
|
if r.status_code in [301, 201, 200]:
|
||||||
|
return request.text
|
||||||
|
else:
|
||||||
|
raise TwythonError('shortenURL() failed with a %s error code.' % r.status_code)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def constructApiURL(base_url, params):
|
def constructApiURL(base_url, params):
|
||||||
return base_url + "?" + "&".join(["%s=%s" % (Twython.unicode2utf8(key), urllib.quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()])
|
return base_url + "?" + "&".join(["%s=%s" % (Twython.unicode2utf8(key), urllib.quote_plus(Twython.unicode2utf8(value))) for (key, value) in params.iteritems()])
|
||||||
|
|
@ -286,14 +331,14 @@ class Twython(object):
|
||||||
kwargs['user_id'] = ','.join(map(str, ids))
|
kwargs['user_id'] = ','.join(map(str, ids))
|
||||||
if screen_names:
|
if screen_names:
|
||||||
kwargs['screen_name'] = ','.join(screen_names)
|
kwargs['screen_name'] = ','.join(screen_names)
|
||||||
|
|
||||||
lookupURL = Twython.constructApiURL("http://api.twitter.com/%d/users/lookup.json" % version, kwargs)
|
lookupURL = Twython.constructApiURL("http://api.twitter.com/%d/users/lookup.json" % version, kwargs)
|
||||||
try:
|
try:
|
||||||
response = self.client.post(lookupURL, headers=self.headers)
|
response = self.client.post(lookupURL, headers=self.headers)
|
||||||
return simplejson.loads(response.content.decode('utf-8'))
|
return simplejson.loads(response.content.decode('utf-8'))
|
||||||
except RequestException, e:
|
except RequestException, e:
|
||||||
raise TwythonError("bulkUserLookup() failed with a %s error code." % e.code, e.code)
|
raise TwythonError("bulkUserLookup() failed with a %s error code." % e.code, e.code)
|
||||||
|
|
||||||
def search(self, **kwargs):
|
def search(self, **kwargs):
|
||||||
"""search(search_query, **kwargs)
|
"""search(search_query, **kwargs)
|
||||||
|
|
||||||
|
|
@ -314,16 +359,16 @@ class Twython(object):
|
||||||
retry_wait_seconds,
|
retry_wait_seconds,
|
||||||
retry_wait_seconds,
|
retry_wait_seconds,
|
||||||
response.status_code)
|
response.status_code)
|
||||||
|
|
||||||
return simplejson.loads(response.content.decode('utf-8'))
|
return simplejson.loads(response.content.decode('utf-8'))
|
||||||
except RequestException, e:
|
except RequestException, e:
|
||||||
raise TwythonError("getSearchTimeline() failed with a %s error code." % e.code, e.code)
|
raise TwythonError("getSearchTimeline() failed with a %s error code." % e.code, e.code)
|
||||||
|
|
||||||
def searchTwitter(self, **kwargs):
|
def searchTwitter(self, **kwargs):
|
||||||
"""use search() ,this is a fall back method to support searchTwitter()
|
"""use search() ,this is a fall back method to support searchTwitter()
|
||||||
"""
|
"""
|
||||||
return self.search(**kwargs)
|
return self.search(**kwargs)
|
||||||
|
|
||||||
def searchGen(self, search_query, **kwargs):
|
def searchGen(self, search_query, **kwargs):
|
||||||
"""searchGen(search_query, **kwargs)
|
"""searchGen(search_query, **kwargs)
|
||||||
|
|
||||||
|
|
@ -341,13 +386,13 @@ class Twython(object):
|
||||||
data = simplejson.loads(response.content.decode('utf-8'))
|
data = simplejson.loads(response.content.decode('utf-8'))
|
||||||
except RequestException, e:
|
except RequestException, e:
|
||||||
raise TwythonError("searchGen() failed with a %s error code." % e.code, e.code)
|
raise TwythonError("searchGen() failed with a %s error code." % e.code, e.code)
|
||||||
|
|
||||||
if not data['results']:
|
if not data['results']:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
for tweet in data['results']:
|
for tweet in data['results']:
|
||||||
yield tweet
|
yield tweet
|
||||||
|
|
||||||
if 'page' not in kwargs:
|
if 'page' not in kwargs:
|
||||||
kwargs['page'] = '2'
|
kwargs['page'] = '2'
|
||||||
else:
|
else:
|
||||||
|
|
@ -360,15 +405,15 @@ class Twython(object):
|
||||||
except e:
|
except e:
|
||||||
raise TwythonError("searchGen() failed with %s error code" % \
|
raise TwythonError("searchGen() failed with %s error code" % \
|
||||||
e.code, e.code)
|
e.code, e.code)
|
||||||
|
|
||||||
for tweet in self.searchGen(search_query, **kwargs):
|
for tweet in self.searchGen(search_query, **kwargs):
|
||||||
yield tweet
|
yield tweet
|
||||||
|
|
||||||
def searchTwitterGen(self, search_query, **kwargs):
|
def searchTwitterGen(self, search_query, **kwargs):
|
||||||
"""use searchGen(), this is a fallback method to support
|
"""use searchGen(), this is a fallback method to support
|
||||||
searchTwitterGen()"""
|
searchTwitterGen()"""
|
||||||
return self.searchGen(search_query, **kwargs)
|
return self.searchGen(search_query, **kwargs)
|
||||||
|
|
||||||
def isListMember(self, list_id, id, username, version=1):
|
def isListMember(self, list_id, id, username, version=1):
|
||||||
""" isListMember(self, list_id, id, version)
|
""" isListMember(self, list_id, id, version)
|
||||||
|
|
||||||
|
|
@ -387,7 +432,7 @@ class Twython(object):
|
||||||
return simplejson.loads(response.content.decode('utf-8'))
|
return simplejson.loads(response.content.decode('utf-8'))
|
||||||
except RequestException, e:
|
except RequestException, e:
|
||||||
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
|
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
|
||||||
|
|
||||||
def isListSubscriber(self, username, list_id, id, version=1):
|
def isListSubscriber(self, username, list_id, id, version=1):
|
||||||
""" isListSubscriber(self, list_id, id, version)
|
""" isListSubscriber(self, list_id, id, version)
|
||||||
|
|
||||||
|
|
@ -406,7 +451,7 @@ class Twython(object):
|
||||||
return simplejson.loads(response.content.decode('utf-8'))
|
return simplejson.loads(response.content.decode('utf-8'))
|
||||||
except RequestException, e:
|
except RequestException, e:
|
||||||
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
|
raise TwythonError("isListMember() failed with a %d error code." % e.code, e.code)
|
||||||
|
|
||||||
# The following methods are apart from the other Account methods, because they rely on a whole multipart-data posting function set.
|
# The following methods are apart from the other Account methods, because they rely on a whole multipart-data posting function set.
|
||||||
def updateProfileBackgroundImage(self, file_, tile=True, version=1):
|
def updateProfileBackgroundImage(self, file_, tile=True, version=1):
|
||||||
""" updateProfileBackgroundImage(filename, tile=True)
|
""" updateProfileBackgroundImage(filename, tile=True)
|
||||||
|
|
@ -418,8 +463,10 @@ class Twython(object):
|
||||||
tile - Optional (defaults to True). If set to true the background image will be displayed tiled. The image will not be tiled otherwise.
|
tile - Optional (defaults to True). If set to true the background image will be displayed tiled. The image will not be tiled otherwise.
|
||||||
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 (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.
|
||||||
"""
|
"""
|
||||||
return self._media_update('http://api.twitter.com/%d/account/update_profile_background_image.json' % version, {'image': (file_, open(file_, 'rb'))}, params={'tile': tile})
|
return self._media_update('http://api.twitter.com/%d/account/update_profile_background_image.json' % version, {
|
||||||
|
'image': (file_, open(file_, 'rb'))
|
||||||
|
}, params = {'tile': tile})
|
||||||
|
|
||||||
def updateProfileImage(self, file_, version=1):
|
def updateProfileImage(self, file_, version=1):
|
||||||
""" updateProfileImage(filename)
|
""" updateProfileImage(filename)
|
||||||
|
|
||||||
|
|
@ -429,8 +476,10 @@ class Twython(object):
|
||||||
image - Required. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down.
|
image - Required. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down.
|
||||||
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 (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.
|
||||||
"""
|
"""
|
||||||
return self._media_update('http://api.twitter.com/%d/account/update_profile_image.json' % version, {'image': (file_, open(file_, 'rb'))})
|
return self._media_update('http://api.twitter.com/%d/account/update_profile_image.json' % version, {
|
||||||
|
'image': (file_, open(file_, 'rb'))
|
||||||
|
})
|
||||||
|
|
||||||
# statuses/update_with_media
|
# statuses/update_with_media
|
||||||
def updateStatusWithMedia(self, file_, version=1, **params):
|
def updateStatusWithMedia(self, file_, version=1, **params):
|
||||||
""" updateStatusWithMedia(filename)
|
""" updateStatusWithMedia(filename)
|
||||||
|
|
@ -441,8 +490,10 @@ class Twython(object):
|
||||||
image - Required. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down.
|
image - Required. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down.
|
||||||
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 (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.
|
||||||
"""
|
"""
|
||||||
return self._media_update('https://upload.twitter.com/%d/statuses/update_with_media.json' % version, {'media': (file_, open(file_, 'rb'))}, **params)
|
return self._media_update('https://upload.twitter.com/%d/statuses/update_with_media.json' % version, {
|
||||||
|
'media': (file_, open(file_, 'rb'))
|
||||||
|
}, **params)
|
||||||
|
|
||||||
def _media_update(self, url, file_, params=None):
|
def _media_update(self, url, file_, params=None):
|
||||||
params = params or {}
|
params = params or {}
|
||||||
|
|
||||||
|
|
@ -459,7 +510,7 @@ class Twython(object):
|
||||||
header.. that MIGHT be why it's not working..
|
header.. that MIGHT be why it's not working..
|
||||||
I haven't debugged enough.
|
I haven't debugged enough.
|
||||||
|
|
||||||
- Mike Helmick
|
- Mike Helmick
|
||||||
***
|
***
|
||||||
|
|
||||||
self.oauth_hook.header_auth = True
|
self.oauth_hook.header_auth = True
|
||||||
|
|
@ -474,24 +525,24 @@ class Twython(object):
|
||||||
'oauth_token': self.oauth_token,
|
'oauth_token': self.oauth_token,
|
||||||
'oauth_timestamp': int(time.time()),
|
'oauth_timestamp': int(time.time()),
|
||||||
}
|
}
|
||||||
|
|
||||||
#create a fake request with your upload url and parameters
|
#create a fake request with your upload url and parameters
|
||||||
faux_req = oauth.Request(method='POST', url=url, parameters=oauth_params)
|
faux_req = oauth.Request(method='POST', url=url, parameters=oauth_params)
|
||||||
|
|
||||||
#sign the fake request.
|
#sign the fake request.
|
||||||
signature_method = oauth.SignatureMethod_HMAC_SHA1()
|
signature_method = oauth.SignatureMethod_HMAC_SHA1()
|
||||||
|
|
||||||
class dotdict(dict):
|
class dotdict(dict):
|
||||||
"""
|
"""
|
||||||
This is a helper func. because python-oauth2 wants a
|
This is a helper func. because python-oauth2 wants a
|
||||||
dict in dot notation.
|
dict in dot notation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
return self.get(attr, None)
|
return self.get(attr, None)
|
||||||
__setattr__ = dict.__setitem__
|
__setattr__ = dict.__setitem__
|
||||||
__delattr__ = dict.__delitem__
|
__delattr__ = dict.__delitem__
|
||||||
|
|
||||||
consumer = {
|
consumer = {
|
||||||
'key': self.oauth_hook.consumer_key,
|
'key': self.oauth_hook.consumer_key,
|
||||||
'secret': self.oauth_hook.consumer_secret
|
'secret': self.oauth_hook.consumer_secret
|
||||||
|
|
@ -500,15 +551,15 @@ class Twython(object):
|
||||||
'key': self.oauth_token,
|
'key': self.oauth_token,
|
||||||
'secret': self.oauth_secret
|
'secret': self.oauth_secret
|
||||||
}
|
}
|
||||||
|
|
||||||
faux_req.sign_request(signature_method, dotdict(consumer), dotdict(token))
|
faux_req.sign_request(signature_method, dotdict(consumer), dotdict(token))
|
||||||
|
|
||||||
#create a dict out of the fake request signed params
|
#create a dict out of the fake request signed params
|
||||||
self.headers.update(faux_req.to_header())
|
self.headers.update(faux_req.to_header())
|
||||||
|
|
||||||
req = requests.post(url, data=params, files=file_, headers=self.headers)
|
req = requests.post(url, data=params, files=file_, headers=self.headers)
|
||||||
return req.content
|
return req.content
|
||||||
|
|
||||||
def getProfileImageUrl(self, username, size=None, version=1):
|
def getProfileImageUrl(self, username, size=None, version=1):
|
||||||
""" getProfileImageUrl(username)
|
""" getProfileImageUrl(username)
|
||||||
|
|
||||||
|
|
@ -522,16 +573,58 @@ class Twython(object):
|
||||||
url = "http://api.twitter.com/%s/users/profile_image/%s.json" % (version, username)
|
url = "http://api.twitter.com/%s/users/profile_image/%s.json" % (version, username)
|
||||||
if size:
|
if size:
|
||||||
url = self.constructApiURL(url, {'size': size})
|
url = self.constructApiURL(url, {'size': size})
|
||||||
|
|
||||||
#client.follow_redirects = False
|
#client.follow_redirects = False
|
||||||
response = self.client.get(url, allow_redirects=False)
|
response = self.client.get(url, allow_redirects=False)
|
||||||
image_url = response.headers.get('location')
|
image_url = response.headers.get('location')
|
||||||
|
|
||||||
if response.status_code in (301, 302, 303, 307) and image_url is not None:
|
if response.status_code in (301, 302, 303, 307) and image_url is not None:
|
||||||
return image_url
|
return image_url
|
||||||
|
|
||||||
raise TwythonError("getProfileImageUrl() failed with a %d error code." % response.status_code, response.status_code)
|
raise TwythonError("getProfileImageUrl() failed with a %d error code." % response.status_code, response.status_code)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def stream(data, callback):
|
||||||
|
"""
|
||||||
|
A Streaming API endpoint, because requests (by the lovely Kenneth Reitz) makes this not
|
||||||
|
stupidly annoying to implement. In reality, Twython does absolutely *nothing special* here,
|
||||||
|
but people new to programming expect this type of function to exist for this library, so we
|
||||||
|
provide it for convenience.
|
||||||
|
|
||||||
|
Seriously, this is nothing special. :)
|
||||||
|
|
||||||
|
For the basic stream you're probably accessing, you'll want to pass the following as data dictionary
|
||||||
|
keys. If you need to use OAuth (newer streams), passing secrets/etc as keys SHOULD work...
|
||||||
|
|
||||||
|
username - Required. User name, self explanatory.
|
||||||
|
password - Required. The Streaming API doesn't use OAuth, so we do this the old school way. It's all
|
||||||
|
done over SSL (https://), so you're not left totally vulnerable.
|
||||||
|
endpoint - Optional. Override the endpoint you're using with the Twitter Streaming API. This is defaulted to the one
|
||||||
|
that everyone has access to, but if Twitter <3's you feel free to set this to your wildest desires.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
data - Required. Dictionary of attributes to attach to the request (see: params https://dev.twitter.com/docs/streaming-api/methods)
|
||||||
|
callback - Required. Callback function to be fired when tweets come in (this is an event-based-ish API).
|
||||||
|
"""
|
||||||
|
endpoint = 'https://stream.twitter.com/1/statuses/filter.json'
|
||||||
|
if 'endpoint' in data:
|
||||||
|
endpoint = data.pop('endpoint')
|
||||||
|
|
||||||
|
needs_basic_auth = False
|
||||||
|
if 'username' in data:
|
||||||
|
needs_basic_auth = True
|
||||||
|
username = data.pop('username')
|
||||||
|
password = data.pop('password')
|
||||||
|
|
||||||
|
if needs_basic_auth:
|
||||||
|
stream = requests.post(endpoint, data = data, auth = (username, password))
|
||||||
|
else:
|
||||||
|
stream = requests.post(endpoint, data = data)
|
||||||
|
|
||||||
|
for line in stream.iter_lines():
|
||||||
|
if line:
|
||||||
|
callback(json.loads(line))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unicode2utf8(text):
|
def unicode2utf8(text):
|
||||||
try:
|
try:
|
||||||
|
|
@ -540,7 +633,7 @@ class Twython(object):
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return text
|
return text
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encode(text):
|
def encode(text):
|
||||||
if isinstance(text, (str, unicode)):
|
if isinstance(text, (str, unicode)):
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
||||||
__version__ = "1.4.6"
|
__version__ = "1.4.7"
|
||||||
|
|
||||||
import cgi
|
import cgi
|
||||||
import urllib.request, urllib.parse, urllib.error
|
import urllib.request, urllib.parse, urllib.error
|
||||||
|
|
@ -37,16 +37,8 @@ try:
|
||||||
# Python 2.6 and up
|
# Python 2.6 and up
|
||||||
import json as simplejson
|
import json as simplejson
|
||||||
except ImportError:
|
except ImportError:
|
||||||
try:
|
# Seriously wtf is wrong with you if you get this Exception.
|
||||||
# Python 2.6 and below (2.4/2.5, 2.3 is not guranteed to work with this library to begin with)
|
raise Exception("Twython3k requires a json library to work. http://www.undefined.org/python/")
|
||||||
import simplejson
|
|
||||||
except ImportError:
|
|
||||||
try:
|
|
||||||
# This case gets rarer by the day, but if we need to, we can pull it from Django provided it's there.
|
|
||||||
from django.utils import simplejson
|
|
||||||
except:
|
|
||||||
# Seriously wtf is wrong with you if you get this Exception.
|
|
||||||
raise Exception("Twython requires the simplejson library (or Python 2.6) to work. http://www.undefined.org/python/")
|
|
||||||
|
|
||||||
# Try and gauge the old OAuth2 library spec. Versions 1.5 and greater no longer have the callback
|
# Try and gauge the old OAuth2 library spec. Versions 1.5 and greater no longer have the callback
|
||||||
# url as part of the request object; older versions we need to patch for Python 2.5... ugh. ;P
|
# url as part of the request object; older versions we need to patch for Python 2.5... ugh. ;P
|
||||||
|
|
@ -81,7 +73,7 @@ class TwythonError(AttributeError):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class APILimit(TwythonError):
|
class TwythonAPILimit(TwythonError):
|
||||||
"""
|
"""
|
||||||
Raised when you've hit an API limit. Try to avoid these, read the API
|
Raised when you've hit an API limit. Try to avoid these, read the API
|
||||||
docs if you're running into issues here, Twython does not concern itself with
|
docs if you're running into issues here, Twython does not concern itself with
|
||||||
|
|
@ -94,7 +86,22 @@ class APILimit(TwythonError):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class AuthError(TwythonError):
|
class APILimit(TwythonError):
|
||||||
|
"""
|
||||||
|
Raised when you've hit an API limit. Try to avoid these, read the API
|
||||||
|
docs if you're running into issues here, Twython does not concern itself with
|
||||||
|
this matter beyond telling you that you've done goofed.
|
||||||
|
|
||||||
|
DEPRECATED, you should be importing TwythonAPILimit instead. :)
|
||||||
|
"""
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = '%s\n Notice: APILimit is deprecated and soon to be removed, catch TwythonAPILimit instead!' % msg
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
|
class TwythonAuthError(TwythonError):
|
||||||
"""
|
"""
|
||||||
Raised when you try to access a protected resource and it fails due to some issue with
|
Raised when you try to access a protected resource and it fails due to some issue with
|
||||||
your authentication.
|
your authentication.
|
||||||
|
|
@ -105,6 +112,19 @@ class AuthError(TwythonError):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
class AuthError(TwythonError):
|
||||||
|
"""
|
||||||
|
Raised when you try to access a protected resource and it fails due to some issue with
|
||||||
|
your authentication.
|
||||||
|
|
||||||
|
DEPRECATED, you should be importing TwythonAuthError instead.
|
||||||
|
"""
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = '%s\n Notice: AuthLimit is deprecated and soon to be removed, catch TwythonAPILimit instead!' % msg
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class Twython(object):
|
class Twython(object):
|
||||||
def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None, client_args={}):
|
def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None, client_args={}):
|
||||||
|
|
@ -189,10 +209,16 @@ class Twython(object):
|
||||||
callback_url = self.callback_url or 'oob'
|
callback_url = self.callback_url or 'oob'
|
||||||
|
|
||||||
request_args = {}
|
request_args = {}
|
||||||
|
method = 'GET'
|
||||||
if OAUTH_LIB_SUPPORTS_CALLBACK:
|
if OAUTH_LIB_SUPPORTS_CALLBACK:
|
||||||
request_args['callback_url'] = callback_url
|
request_args['callback_url'] = callback_url
|
||||||
|
else:
|
||||||
|
# This is a hack for versions of oauth that don't support the callback URL. This is also
|
||||||
|
# done differently than the Python2 version of Twython, which uses Requests internally (as opposed to httplib2).
|
||||||
|
request_args['body'] = urllib.urlencode({'oauth_callback': callback_url})
|
||||||
|
method = 'POST'
|
||||||
|
|
||||||
resp, content = self.client.request(self.request_token_url, "GET", **request_args)
|
resp, content = self.client.request(self.request_token_url, method, **request_args)
|
||||||
|
|
||||||
if resp['status'] != '200':
|
if resp['status'] != '200':
|
||||||
raise AuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (resp['status'], content))
|
raise AuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (resp['status'], content))
|
||||||
|
|
@ -213,7 +239,6 @@ class Twython(object):
|
||||||
'oauth_token' : request_tokens['oauth_token'],
|
'oauth_token' : request_tokens['oauth_token'],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use old-style callback argument
|
|
||||||
if OAUTH_CALLBACK_IN_URL or (callback_url!='oob' and not oauth_callback_confirmed):
|
if OAUTH_CALLBACK_IN_URL or (callback_url!='oob' and not oauth_callback_confirmed):
|
||||||
auth_url_params['oauth_callback'] = callback_url
|
auth_url_params['oauth_callback'] = callback_url
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue