summaryrefslogtreecommitdiff
path: root/wowarmopy/armory.py
diff options
context:
space:
mode:
authorGuillaume Seguin <ixce@ed3n-n.aulo.in>2009-07-24 03:55:36 +0200
committerGuillaume Seguin <ixce@ed3n-n.aulo.in>2009-07-24 03:55:36 +0200
commit2644e3d7246e74b8048f5b5d7024a6b4d9510f42 (patch)
tree85e62363d2e734d1e999ce36ae535e330de16dc2 /wowarmopy/armory.py
downloadwowarmopy-2644e3d7246e74b8048f5b5d7024a6b4d9510f42.tar.gz
wowarmopy-2644e3d7246e74b8048f5b5d7024a6b4d9510f42.tar.bz2
Initial import, only provides basic armory calendar support for nowHEADmaster
Diffstat (limited to 'wowarmopy/armory.py')
-rw-r--r--wowarmopy/armory.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/wowarmopy/armory.py b/wowarmopy/armory.py
new file mode 100644
index 0000000..49c5c04
--- /dev/null
+++ b/wowarmopy/armory.py
@@ -0,0 +1,100 @@
+import re
+import httplib, urllib, urlparse
+
+import simplejson
+
+import auth
+import constants
+import calendar
+
+class Armory (object):
+
+ auth = None
+ realm = None
+ character_name = None
+ guild_name = None
+ locale = None
+
+ def __init__ (self, realm, character_name = None, guild_name = None, locale = "us"):
+ """Initializes Armory object"""
+ self.realm = realm
+ self.character_name = character_name
+ self.guild_name = guild_name
+ self.locale = locale
+
+ def login (self, username, password):
+ self.auth = auth.ArmoryAuth (self)
+ self.auth.login (username, password)
+
+ def refresh_login (self):
+ assert self.auth != None
+ self.auth.refresh_login ()
+
+ def get_base_url (self, login = False, secure = False):
+ if secure:
+ url = "https://"
+ else:
+ url = "http://"
+ if self.locale == "us":
+ url += "www."
+ else:
+ url += self.locale + "."
+ if login:
+ url += constants.login_base_url
+ else:
+ url+= constants.armory_base_url
+ return url
+
+ def get_calendar (self, **kwargs):
+ url = self.get_base_url (secure = True) + constants.calendar_user_url
+ data = self.get_json (url, **kwargs)
+ return calendar.Calendar (self, data)
+
+ def get_calendar_event_details (self, event_id):
+ url = self.get_base_url (secure = True) + constants.calendar_detail_url
+ data = self.get_json (url, event = event_id)
+ return data
+
+ def get_json (self, url, **kwargs):
+ raw = self.get_file (url, **kwargs)
+ match = re.search ("^\w+\((.+)\);$", raw);
+ if not match:
+ print "DEBUG (raw data) :", raw
+ raise RuntimeError, "Failed to fetch json data"
+ json = simplejson.loads (match.groups ()[0])
+ return json
+
+ def get_params (self, **kwargs):
+ keys = {
+ "character_name": 'n',
+ "realm": 'r',
+ "guild_name": 'gn',
+ "calendar_type": 'type',
+ "month": 'month',
+ "year": 'year',
+ "event": 'e',
+ }
+ params = {}
+ for key in keys:
+ if key in kwargs:
+ params[keys[key]] = kwargs[key]
+ elif hasattr (self, key):
+ params[keys[key]] = getattr (self, key)
+ return params
+
+ def get_file (self, url, **kwargs):
+ parsed_url = urlparse.urlsplit (url)
+ scheme, domain, path, query, fragment = parsed_url
+ query = query + "&" if query != '' else "?"
+ query += urllib.urlencode (self.get_params (**kwargs))
+ if scheme == "https":
+ h = httplib.HTTPSConnection (domain)
+ else:
+ h = httplib.HTTPConnection (domain)
+ headers = {"User-Agent": "Mozilla/5.0 Gecko/20070219 Firefox/2.0.0.2", # ensure returns XML
+ "Cookie": "cookieMenu=all; cookies=true; "}
+ if self.auth:
+ headers["Cookie"] += self.auth.get_cookie ()
+ h.request ("GET", "%s?%s#%s" % (path, query, fragment), None, headers)
+ r = h.getresponse ()
+ return r.read ()