Merge 1a8230bd3e into d6d8823dc2
This commit is contained in:
commit
71d02b166f
6 changed files with 443 additions and 368 deletions
5
.idea/encodings.xml
generated
Normal file
5
.idea/encodings.xml
generated
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
|
||||||
|
</project>
|
||||||
|
|
||||||
8
.idea/misc.xml
generated
Normal file
8
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DependencyValidationManager">
|
||||||
|
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.1 (C:/projects/critiqus/environment/Scripts/python.exe)" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
|
|
||||||
9
.idea/modules.xml
generated
Normal file
9
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/twython.iml" filepath="$PROJECT_DIR$/.idea/twython.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
||||||
9
.idea/twython.iml
generated
Normal file
9
.idea/twython.iml
generated
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
|
|
||||||
7
.idea/vcs.xml
generated
Normal file
7
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
||||||
|
|
@ -46,7 +46,8 @@ except ImportError:
|
||||||
from django.utils import simplejson
|
from django.utils import simplejson
|
||||||
except:
|
except:
|
||||||
# Seriously wtf is wrong with you if you get this Exception.
|
# 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/")
|
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
|
||||||
|
|
@ -72,6 +73,7 @@ class TwythonError(AttributeError):
|
||||||
|
|
||||||
from twython import TwythonError, APILimit, AuthError
|
from twython import TwythonError, APILimit, AuthError
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, msg, error_code=None):
|
def __init__(self, msg, error_code=None):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
if error_code == 400:
|
if error_code == 400:
|
||||||
|
|
@ -87,6 +89,7 @@ class APILimit(TwythonError):
|
||||||
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
|
||||||
this matter beyond telling you that you've done goofed.
|
this matter beyond telling you that you've done goofed.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
|
|
@ -99,6 +102,7 @@ class AuthError(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.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
||||||
|
|
@ -107,7 +111,8 @@ class AuthError(TwythonError):
|
||||||
|
|
||||||
|
|
||||||
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={}):
|
||||||
"""setup(self, oauth_token = None, headers = None)
|
"""setup(self, oauth_token = None, headers = None)
|
||||||
|
|
||||||
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
|
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
|
||||||
|
|
@ -173,18 +178,21 @@ class Twython(object):
|
||||||
|
|
||||||
I'll hate myself for saying this, but this is heavily inspired by Ruby's "method_missing".
|
I'll hate myself for saying this, but this is heavily inspired by Ruby's "method_missing".
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, **kwargs):
|
def get(self, **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_]+)\}\}',
|
||||||
lambda m: "%s" % kwargs.get(m.group(1), '1'), # The '1' here catches the API version. Slightly hilarious.
|
lambda m: "%s" % kwargs.get(m.group(1), '1'),
|
||||||
|
# The '1' here catches the API version. Slightly hilarious.
|
||||||
base_url + fn['url']
|
base_url + fn['url']
|
||||||
)
|
)
|
||||||
|
|
||||||
# Then open and load that shiiit, yo. TODO: check HTTP method and junk, handle errors/authentication
|
# Then open and load that shiiit, yo. TODO: check HTTP method and junk, handle errors/authentication
|
||||||
if fn['method'] == 'POST':
|
if fn['method'] == 'POST':
|
||||||
resp, content = self.client.request(base, fn['method'], urllib.urlencode(dict([k, Twython.encode(v)] for k, v in kwargs.items())), headers = self.headers)
|
resp, content = self.client.request(base, fn['method'], urllib.urlencode(
|
||||||
|
dict([k, Twython.encode(v)] for k, v in kwargs.items())), headers=self.headers)
|
||||||
else:
|
else:
|
||||||
url = base + "?" + "&".join(["%s=%s" % (key, value) for (key, value) in kwargs.iteritems()])
|
url = base + "?" + "&".join(["%s=%s" % (key, value) for (key, value) in kwargs.iteritems()])
|
||||||
resp, content = self.client.request(url, fn['method'], headers=self.headers)
|
resp, content = self.client.request(url, fn['method'], headers=self.headers)
|
||||||
|
|
@ -211,7 +219,8 @@ class Twython(object):
|
||||||
resp, content = self.client.request(self.request_token_url, "GET", **request_args)
|
resp, content = self.client.request(self.request_token_url, "GET", **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))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request_tokens = dict(urlparse.parse_qsl(content))
|
request_tokens = dict(urlparse.parse_qsl(content))
|
||||||
|
|
@ -222,6 +231,7 @@ class Twython(object):
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
@ -257,7 +267,9 @@ class Twython(object):
|
||||||
|
|
||||||
@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()])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def shortenURL(url_to_shorten, shortener="http://is.gd/api.php", query="longurl"):
|
def shortenURL(url_to_shorten, shortener="http://is.gd/api.php", query="longurl"):
|
||||||
|
|
@ -270,7 +282,8 @@ class Twython(object):
|
||||||
shortener - In case you want to use a url shortening service other than is.gd.
|
shortener - In case you want to use a url shortening service other than is.gd.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
content = urllib2.urlopen(shortener + "?" + urllib.urlencode({query: Twython.unicode2utf8(url_to_shorten)})).read()
|
content = urllib2.urlopen(
|
||||||
|
shortener + "?" + urllib.urlencode({query: Twython.unicode2utf8(url_to_shorten)})).read()
|
||||||
return content
|
return content
|
||||||
except HTTPError, e:
|
except HTTPError, e:
|
||||||
raise TwythonError("shortenURL() failed with a %s error code." % `e.code`)
|
raise TwythonError("shortenURL() failed with a %s error code." % `e.code`)
|
||||||
|
|
@ -322,7 +335,8 @@ class Twython(object):
|
||||||
|
|
||||||
e.g x.searchTwitter(q="jjndf", page="2")
|
e.g x.searchTwitter(q="jjndf", page="2")
|
||||||
"""
|
"""
|
||||||
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs)
|
searchURL = Twython.constructApiURL(
|
||||||
|
"http://search.twitter.com/search.json?q=%s" % Twython.unicode2utf8(search_query), kwargs)
|
||||||
try:
|
try:
|
||||||
resp, content = self.client.request(searchURL, "GET", headers=self.headers)
|
resp, content = self.client.request(searchURL, "GET", headers=self.headers)
|
||||||
data = simplejson.loads(content)
|
data = simplejson.loads(content)
|
||||||
|
|
@ -357,7 +371,9 @@ class Twython(object):
|
||||||
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.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/members/%s.json" % (version, username, list_id, `id`), headers = self.headers)
|
resp, content = self.client.request(
|
||||||
|
"http://api.twitter.com/%d/%s/%s/members/%s.json" % (version, username, list_id, `id`),
|
||||||
|
headers=self.headers)
|
||||||
return simplejson.loads(content)
|
return simplejson.loads(content)
|
||||||
except HTTPError, e:
|
except HTTPError, 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)
|
||||||
|
|
@ -376,7 +392,9 @@ class Twython(object):
|
||||||
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.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
resp, content = self.client.request("http://api.twitter.com/%d/%s/%s/following/%s.json" % (version, username, list_id, `id`), headers = self.headers)
|
resp, content = self.client.request(
|
||||||
|
"http://api.twitter.com/%d/%s/%s/following/%s.json" % (version, username, list_id, `id`),
|
||||||
|
headers=self.headers)
|
||||||
return simplejson.loads(content)
|
return simplejson.loads(content)
|
||||||
except HTTPError, e:
|
except HTTPError, 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)
|
||||||
|
|
@ -398,7 +416,9 @@ class Twython(object):
|
||||||
fields = []
|
fields = []
|
||||||
content_type, body = Twython.encode_multipart_formdata(fields, files)
|
content_type, body = Twython.encode_multipart_formdata(fields, files)
|
||||||
headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
|
headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
|
||||||
r = urllib2.Request("http://api.twitter.com/%d/account/update_profile_background_image.json?tile=%s" % (version, tile), body, headers)
|
r = urllib2.Request(
|
||||||
|
"http://api.twitter.com/%d/account/update_profile_background_image.json?tile=%s" % (version, tile), body
|
||||||
|
, headers)
|
||||||
return urllib2.urlopen(r).read()
|
return urllib2.urlopen(r).read()
|
||||||
except HTTPError, e:
|
except HTTPError, e:
|
||||||
raise TwythonError("updateProfileBackgroundImage() failed with a %d error code." % e.code, e.code)
|
raise TwythonError("updateProfileBackgroundImage() failed with a %d error code." % e.code, e.code)
|
||||||
|
|
@ -447,6 +467,23 @@ class Twython(object):
|
||||||
|
|
||||||
raise TwythonError("getProfileImageUrl() failed with a %d error code." % resp.status, resp.status)
|
raise TwythonError("getProfileImageUrl() failed with a %d error code." % resp.status, resp.status)
|
||||||
|
|
||||||
|
def getUserProfile(self, **kwargs):
|
||||||
|
"""getUserProfile(screen_name, **kwargs)
|
||||||
|
|
||||||
|
Returns user profile that match a specified screen_name.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
See the documentation at https://dev.twitter.com/docs/api/1/get/users/show. Pass in the API supported arguments as named parameters.
|
||||||
|
|
||||||
|
e.g x.searchTwitter(screen_name="lacion"[, user_id=123[, include_entities=True]])
|
||||||
|
"""
|
||||||
|
userShowURL = Twython.constructApiURL("http://api.twitter.com/version/users/show.json", kwargs)
|
||||||
|
try:
|
||||||
|
resp, content = self.client.request(searchURL, "GET", headers=self.headers)
|
||||||
|
return simplejson.loads(content)
|
||||||
|
except HTTPError, e:
|
||||||
|
raise TwythonError("getUserProfile() failed with a %s error code." % `e.code`, e.code)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encode_multipart_formdata(fields, files):
|
def encode_multipart_formdata(fields, files):
|
||||||
BOUNDARY = mimetools.choose_boundary()
|
BOUNDARY = mimetools.choose_boundary()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue