Removing use of small icons to be more finger friendly on Maemo
[watersofshiloah] / src / backend.py
1 #!/usr/bin/env python
2
3 import urllib
4 from xml.etree import ElementTree
5 import logging
6
7 import browser_emu
8
9
10 _moduleLogger = logging.getLogger(__name__)
11
12
13 class Backend(object):
14
15         def __init__(self):
16                 self._browser = browser_emu.MozillaEmulator()
17
18         def get_languages(self):
19                 tree = self._get_page_with_validation(
20                         action="lds.radio.languages.query",
21                 )
22                 languages = tree.find("languages")
23                 return self._process_list(languages, ["name"])
24
25         def get_radio_channels(self):
26                 tree = self._get_page_with_validation(
27                         action="lds.radio.radiochannels.query",
28                 )
29                 channels = tree.find("channels")
30                 return self._process_list(channels, ["description", "url", "port"])
31
32         def get_radio_channel_programming(self, chanId, date=None):
33                 if date is not None:
34                         tree = self._get_page_with_validation(
35                                 action="lds.radio.radiochannels.programming.query",
36                                 channelID=chanId,
37                                 date=date,
38                         )
39                 else:
40                         tree = self._get_page_with_validation(
41                                 action="lds.radio.radiochannels.programming.query",
42                                 channelID=chanId,
43                         )
44                 programs = tree.find("programs")
45                 return self._process_list(programs, ["date", "time", "title", "shortdescription", "artist"])
46
47         def get_conferences(self, langId):
48                 tree = self._get_page_with_validation(
49                         action="lds.radio.conferences.query",
50                         languageID=langId,
51                 )
52                 conferences = tree.find("conferences")
53                 return self._process_list(conferences, ["title", "full_title", "month", "year"])
54
55         def get_conference_sessions(self, confId):
56                 tree = self._get_page_with_validation(
57                         action="lds.radio.conferences.sessions.query",
58                         conferenceID=confId,
59                 )
60                 items = tree.find("sessions")
61                 return self._process_list(items, ["title", "short_title", "order"])
62
63         def get_conference_talks(self, sessionId):
64                 tree = self._get_page_with_validation(
65                         action="lds.radio.conferences.sessions.talks.query",
66                         sessionID=sessionId,
67                 )
68                 items = tree.find("talks")
69                 return self._process_list(items, ["title", "order", "url", "speaker"])
70
71         def get_magazines(self, langId):
72                 tree = self._get_page_with_validation(
73                         action="lds.radio.magazines.query",
74                         languageID=langId,
75                 )
76                 magazines = tree.find("magazines")
77                 return self._process_list(magazines, ["title"])
78
79         def get_magazine_issues(self, magId):
80                 tree = self._get_page_with_validation(
81                         action="lds.radio.magazines.issues.query",
82                         magazineID=magId,
83                 )
84                 items = tree.find("issues")
85                 return self._process_list(items, ["title", "year", "month", "pictureURL"])
86
87         def get_magazine_articles(self, issueId):
88                 tree = self._get_page_with_validation(
89                         action="lds.radio.magazines.issues.articles.query",
90                         issueID=issueId,
91                 )
92                 items = tree.find("articles")
93                 return self._process_list(items, ["title", "author", "url"])
94
95         def get_scriptures(self, langId):
96                 tree = self._get_page_with_validation(
97                         action="lds.radio.scriptures.query",
98                         languageID=langId,
99                 )
100                 scriptures = tree.find("scriptures")
101                 return self._process_list(scriptures, ["title"])
102
103         def get_scripture_books(self, scriptId):
104                 tree = self._get_page_with_validation(
105                         action="lds.radio.scriptures.books.query",
106                         scriptureID=scriptId,
107                 )
108                 items = tree.find("books")
109                 return self._process_list(items, ["title"])
110
111         def get_scripture_chapters(self, bookId):
112                 tree = self._get_page_with_validation(
113                         action="lds.radio.scriptures.books.chapters.query",
114                         bookID=bookId,
115                 )
116                 items = tree.find("chapters")
117                 return self._process_list(items, ["title", "url"])
118
119         def _get_page_with_validation(self, **params):
120                 encodedParams = urllib.urlencode(params)
121                 page = self._browser.download("http://tech.lds.org/radio?%s" % encodedParams)
122                 if not page:
123                         raise RuntimeError("Blank page")
124                 tree = ElementTree.fromstring(page)
125
126                 if tree.tag == "apiresults":
127                         desc = tree.find("ErrorDescription")
128                         raise RuntimeError(desc.text)
129                 else:
130                         results = tree.find("apiresults")
131                         if not results.attrib["success"]:
132                                 raise RuntimeError("Could not determine radio languages")
133
134                 return tree
135
136         def _process_list(self, tree, elements):
137                 for item in tree.getchildren():
138                         data = {"id": item.attrib["ID"]}
139                         for element in elements:
140                                 data[element] = item.find(element).text
141                         yield data
142
143
144 if __name__ == "__main__":
145         b = Backend()
146
147         print list(b.get_languages())
148
149         if False:
150                 channels = list(b.get_radio_channels())
151                 print channels
152                 for chanData in channels:
153                         programs = list(b.get_radio_channel_programming(chanData["id"]))
154                         print programs
155
156         if False:
157                 confs = list(b.get_conferences(1))
158                 print confs
159                 for confData in confs:
160                         sessions = list(b.get_conference_sessions(confData["id"]))
161                         for sessionData in sessions:
162                                 print sessionData
163                                 talks = list(b.get_conference_talks(sessionData["id"]))
164                                 print talks
165
166         if False:
167                 mags = list(b.get_magazines(1))
168                 print mags
169                 for magData in mags:
170                         issues = list(b.get_magazine_issues(magData["id"]))
171                         for issueData in issues:
172                                 print issueData
173                                 articles = list(b.get_magazine_articles(issueData["id"]))
174                                 print articles
175
176         if False:
177                 mags = list(b.get_scriptures(1))
178                 print mags
179                 for magData in mags:
180                         books = list(b.get_scripture_books(magData["id"]))
181                         for bookData in books:
182                                 print bookData
183                                 chapters = list(b.get_scripture_chapters(bookData["id"]))
184                                 print chapters