summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2010-05-19 14:15:50 -0400
committerGuillaume Seguin <guillaume@segu.in>2010-05-19 14:15:50 -0400
commit3cdeefc9b12012f8b2968054515aead31e904f1a (patch)
tree3a0af861ada5487a50895e609859837920153d14
parentd093f63e619b4d000572707b4472b1d2a03f8bc3 (diff)
downloadsqwareapi-3cdeefc9b12012f8b2968054515aead31e904f1a.tar.gz
sqwareapi-3cdeefc9b12012f8b2968054515aead31e904f1a.tar.bz2
Cleanly use url openers per client (and not globally)
-rw-r--r--sqwarea/__init__.py31
1 files changed, 8 insertions, 23 deletions
diff --git a/sqwarea/__init__.py b/sqwarea/__init__.py
index a67ce26..6234d82 100644
--- a/sqwarea/__init__.py
+++ b/sqwarea/__init__.py
@@ -12,22 +12,6 @@ try:
from BeautifulSoup import BeautifulSoup
except ImportError:
print "HTML/json stuff not available, most features are now broken"
-"""
-class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
- def http_error_301(self, req, fp, code, msg, headers):
- result = urllib2.HTTPRedirectHandler.http_error_301(
- self, req, fp, code, msg, headers)
- result.status = code
- return result
-
- def http_error_302(self, req, fp, code, msg, headers):
- result = urllib2.HTTPRedirectHandler.http_error_302(
- self, req, fp, code, msg, headers)
- result.status = code
- return result
-
-opener = urllib2.build_opener (SmartRedirectHandler ())
-"""
#logging.basicConfig (level = logging.DEBUG)
@@ -71,6 +55,7 @@ class Client (object):
_openId = None
_cookiejar = None
_cookiejar_path = None
+ _opener = None
sessionId = None
def __init__ (self, openid):
@@ -82,14 +67,14 @@ class Client (object):
self._cookiejar.load (self._cookiejar_path)
cookie_processor = urllib2.HTTPCookieProcessor (self._cookiejar)
cookie_opener = urllib2.build_opener (cookie_processor)
- urllib2.install_opener (cookie_opener)
+ self._opener = cookie_opener
def request (self, path, data):
path = "http://" + DOMAIN + os.path.join (BASEPATH, path)
if self.sessionId:
data["sessionId"] = self.sessionId
data = urllib.urlencode (data)
- conn = urllib2.urlopen (path, data)
+ conn = self._opener.open (path, data)
return conn
def jsrequest (self, path, data):
@@ -155,7 +140,7 @@ def parseSessionId (data):
if line.startswith ("sessionId"):
return line.split (":")[1].strip ()
-def try_continue_form (reply):
+def try_continue_form (reply, client):
html = reply.read ()
soup = BeautifulSoup (html)
for form in soup.findAll ("form"):
@@ -172,14 +157,14 @@ def try_continue_form (reply):
if not form_path.startswith ("http"):
form_path = urlparse.urljoin (reply.url, form_path)
req = urllib2.Request (form_path, data, headers)
- return urllib2.urlopen (req)
+ return client._opener.open (req)
# No valid form in there, no redirection either, hm.. let's try the dumb way
for anchor in soup.findAll ("a"):
href = anchor["href"]
if urlparse.urlparse (href).netloc == DOMAIN:
headers = {"Referer": reply.url}
req = urllib2.Request (href, None, headers)
- return urllib2.urlopen (req)
+ return client._opener.open (req)
logging.log (logging.CRITICAL, "Couldn't find a form or link to continue")
class Point (object):
@@ -451,11 +436,11 @@ class API (object):
if not form_path.startswith ("http"):
form_path = urlparse.urljoin (reply.url, form_path)
login_req = urllib2.Request (form_path, login_data, login_headers)
- login_conn = urllib2.urlopen (login_req)
+ login_conn = self._client._opener.open (login_req)
conn = login_conn
while reply_host in conn.url: # Probably a continue form, let's try !
logging.log (logging.DEBUG, "Still at openid provider")
- conn = try_continue_form (conn)
+ conn = try_continue_form (conn, self._client)
if conn.url.endswith ("/Account/Register"):
raise RuntimeError, "Please register with %s first" % openid
elif conn.url.endswith ("/Game"):