Tango now supports adding a unique User Agent, in keeping with Twitter's Search API requirements. See the Wiki for information on usage.

This commit is contained in:
Ryan McGrath 2009-07-28 02:52:35 -04:00
parent 8bf063fdf9
commit 4fed1e241f
2 changed files with 13 additions and 12 deletions

View file

@ -15,7 +15,7 @@ import httplib, urllib, urllib2, mimetypes, mimetools
from urllib2 import HTTPError from urllib2 import HTTPError
__author__ = "Ryan McGrath <ryan@venodesigns.net>" __author__ = "Ryan McGrath <ryan@venodesigns.net>"
__version__ = "0.5" __version__ = "0.6"
try: try:
import simplejson import simplejson
@ -45,7 +45,7 @@ class APILimit(TangoError):
return repr(self.msg) return repr(self.msg)
class setup: class setup:
def __init__(self, authtype = "OAuth", username = None, password = None, oauth_keys = None): def __init__(self, authtype = "OAuth", username = None, password = None, oauth_keys = None, headers = None):
self.authtype = authtype self.authtype = authtype
self.authenticated = False self.authenticated = False
self.username = username self.username = username
@ -63,6 +63,8 @@ class setup:
self.auth_manager.add_password(None, "http://twitter.com", self.username, self.password) self.auth_manager.add_password(None, "http://twitter.com", self.username, self.password)
self.handler = urllib2.HTTPBasicAuthHandler(self.auth_manager) self.handler = urllib2.HTTPBasicAuthHandler(self.auth_manager)
self.opener = urllib2.build_opener(self.handler) self.opener = urllib2.build_opener(self.handler)
if self.headers is not None:
self.opener.addheaders = [('User-agent', self.headers)]
try: try:
test_verify = simplejson.load(self.opener.open("http://twitter.com/account/verify_credentials.json")) test_verify = simplejson.load(self.opener.open("http://twitter.com/account/verify_credentials.json"))
self.authenticated = True self.authenticated = True

View file

@ -15,14 +15,7 @@ import http.client, urllib, urllib.request, urllib.error, urllib.parse, mimetype
from urllib.error import HTTPError from urllib.error import HTTPError
__author__ = "Ryan McGrath <ryan@venodesigns.net>" __author__ = "Ryan McGrath <ryan@venodesigns.net>"
__version__ = "0.5" __version__ = "0.6"
"""
REQUEST_TOKEN_URL = 'https://twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
SIGNIN_URL = 'http://twitter.com/oauth/authenticate'
"""
try: try:
import simplejson import simplejson
@ -52,7 +45,7 @@ class APILimit(TangoError):
return repr(self.msg) return repr(self.msg)
class setup: class setup:
def __init__(self, authtype = "OAuth", username = None, password = None, oauth_keys = None): def __init__(self, authtype = "OAuth", username = None, password = None, oauth_keys = None, headers = None):
self.authtype = authtype self.authtype = authtype
self.authenticated = False self.authenticated = False
self.username = username self.username = username
@ -60,12 +53,18 @@ class setup:
self.oauth_keys = oauth_keys self.oauth_keys = oauth_keys
if self.username is not None and self.password is not None: if self.username is not None and self.password is not None:
if self.authtype == "OAuth": if self.authtype == "OAuth":
pass self.request_token_url = 'https://twitter.com/oauth/request_token'
self.access_token_url = 'https://twitter.com/oauth/access_token'
self.authorization_url = 'http://twitter.com/oauth/authorize'
self.signin_url = 'http://twitter.com/oauth/authenticate'
# Do OAuth type stuff here - how should this be handled? Seems like a framework question...
elif self.authtype == "Basic": elif self.authtype == "Basic":
self.auth_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm() self.auth_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
self.auth_manager.add_password(None, "http://twitter.com", self.username, self.password) self.auth_manager.add_password(None, "http://twitter.com", self.username, self.password)
self.handler = urllib.request.HTTPBasicAuthHandler(self.auth_manager) self.handler = urllib.request.HTTPBasicAuthHandler(self.auth_manager)
self.opener = urllib.request.build_opener(self.handler) self.opener = urllib.request.build_opener(self.handler)
if self.headers is not None:
self.opener.addheaders = [('User-agent', self.headers)]
try: try:
test_verify = simplejson.load(self.opener.open("http://twitter.com/account/verify_credentials.json")) test_verify = simplejson.load(self.opener.open("http://twitter.com/account/verify_credentials.json"))
self.authenticated = True self.authenticated = True