grab the real char info from the eve api
[mevemon] / mevemon.py
1 import hildon
2 import gtk
3 from eveapi import eveapi
4 import fetchimg
5 import apicache
6
7 # we will store our preferences in gconf
8 import gnome.gconf
9
10 #ugly hack to check maemo version. any better way?
11 if hasattr(hildon, "StackableWindow"):
12     from ui.fremantle import ui
13 else:
14     from ui.diablo import ui
15
16 class mEveMon():
17     def __init__(self):
18         self.program = hildon.Program()
19         self.program.__init__()
20         self.gconf = gnome.gconf.client_get_default()
21         self.set_auth()
22         self.ui = ui.mEveMonUI(self)
23
24     def run(self):
25         gtk.main()
26     
27     def quit(self, *args):
28         gtk.main_quit()
29
30     def get_api_key(self):
31         return self.gconf.get_string("/apps/maemo/mevemon/eve_api_key") or ''
32
33     def get_uid(self):
34         return self.gconf.get_string("/apps/maemo/mevemon/eve_uid") or ''
35
36     def set_api_key(self, key):
37         self.gconf.set_string("/apps/maemo/mevemon/eve_api_key", key)
38
39     def set_uid(self, uid):
40         self.gconf.set_string("/apps/maemo/mevemon/eve_uid", uid)
41
42
43     def set_auth(self):
44         """
45         set self.auth to None if there was a problem. somehow later on we'll
46         have to pass the error to the UI, but for now I just want the program
47         to not be broken. --danny
48         """
49         uid = self.get_uid()
50         api_key = self.get_api_key()
51         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
52                 apicache.cache_handler( debug = False ) )
53         try:
54             self.auth = self.cached_api.auth( userID = uid, apiKey = api_key )
55         except eveapi.Error, e:
56             # we need to deal with this, so raise --danny
57             raise
58         except ValueError, e:
59             self.auth = None
60             #raise
61
62     def get_auth(self):
63         return self.auth
64
65     def get_char_sheet(self, charID):
66         if not self.auth: return None
67         try:
68             sheet = self.auth.character(charID).CharacterSheet()
69         except eveapi.Error, e:
70             # we should really have a logger that logs this error somewhere
71             return None
72
73         return sheet
74
75     def char_id2name(self, charID):
76         # the api can take a comma-seperated list of ids, but we'll just take
77         # a single id for now
78         try:
79             chars = self.cached_api.eve.CharacterName(ids=charID).characters
80             name = chars[0].characterName
81         except eveapi.Error, e:
82             return None
83
84         return name
85
86     def char_name2id(self, name):
87         # the api can take a comma-seperated list of names, but we'll just take
88         # a single name for now
89         try:
90             chars = self.cached_api.eve.CharacterID(names=name).characters
91             char_id = chars[0].characterID
92         except eveapi.Error, e:
93             return None
94
95         return char_id
96
97     
98     def get_characters( self ):
99         """
100         returns a list containing a single character with an error message for a
101         name, if there's a problem. FIXME --danny
102         """
103         ui_char_list = []
104         placeholder_chars = [("Please check your API settings.", "imgs/error.jpg")]
105         if not self.auth: return placeholder_chars
106         try:
107             api_char_list = self.auth.account.Characters()
108             # append each char we get to the list we'll return to the
109             # UI --danny
110             for character in api_char_list.characters:
111                 ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ) ) )
112         except eveapi.Error, e:
113             # again, we need to handle this... --danny
114             raise
115
116         return ui_char_list
117
118     def get_portrait(self, char_name, size):
119         """
120         returns the relative path of the retrieved portrait
121         """
122         charID = self.char_name2id(char_name)
123         return fetchimg.portrait_filename(charID, size)
124
125 if __name__ == "__main__":
126     app = mEveMon()
127     app.run()