summaryrefslogtreecommitdiff
path: root/wowarmopy/armory.py
blob: 49c5c04c7cb9bc13e2aacf1ab1cfaee91f095cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 ()