API wrapper
[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):
33                 tree = self._get_page_with_validation(
34                         action="lds.radio.radiochannels.programming.query",
35                         channelID=chanId,
36                 )
37                 programs = tree.find("programs")
38                 return self._process_list(programs, ["date", "time", "title", "shortdescription", "artist"])
39
40         def get_conferences(self, langId):
41                 tree = self._get_page_with_validation(
42                         action="lds.radio.conferences.query",
43                         languageID=langId,
44                 )
45                 conferences = tree.find("conferences")
46                 return self._process_list(conferences, ["title", "full_title", "month", "year"])
47
48         def get_conference_sessions(self, confId):
49                 tree = self._get_page_with_validation(
50                         action="lds.radio.conferences.sessions.query",
51                         conferenceID=confId,
52                 )
53                 items = tree.find("sessions")
54                 return self._process_list(items, ["title", "short_title", "order"])
55
56         def get_conference_talks(self, sessionId):
57                 tree = self._get_page_with_validation(
58                         action="lds.radio.conferences.sessions.talks.query",
59                         sessionID=sessionId,
60                 )
61                 items = tree.find("talks")
62                 return self._process_list(items, ["title", "order", "url", "speaker"])
63
64         def get_magazines(self, langId):
65                 tree = self._get_page_with_validation(
66                         action="lds.radio.magazines.query",
67                         languageID=langId,
68                 )
69                 magazines = tree.find("magazines")
70                 return self._process_list(magazines, ["title"])
71
72         def get_magazine_issues(self, magId):
73                 tree = self._get_page_with_validation(
74                         action="lds.radio.magazines.issues.query",
75                         magazineID=magId,
76                 )
77                 items = tree.find("issues")
78                 return self._process_list(items, ["title", "year", "month", "pictureURL"])
79
80         def get_magazine_articles(self, issueId):
81                 tree = self._get_page_with_validation(
82                         action="lds.radio.magazines.issues.articles.query",
83                         issueID=issueId,
84                 )
85                 items = tree.find("articles")
86                 return self._process_list(items, ["title", "author", "url"])
87
88         def get_scriptures(self, langId):
89                 tree = self._get_page_with_validation(
90                         action="lds.radio.scriptures.query",
91                         languageID=langId,
92                 )
93                 scriptures = tree.find("scriptures")
94                 return self._process_list(scriptures, ["title"])
95
96         def get_scripture_books(self, scriptId):
97                 tree = self._get_page_with_validation(
98                         action="lds.radio.scriptures.books.query",
99                         scriptureID=scriptId,
100                 )
101                 items = tree.find("books")
102                 return self._process_list(items, ["title"])
103
104         def get_scripture_chapters(self, bookId):
105                 tree = self._get_page_with_validation(
106                         action="lds.radio.scriptures.books.chapters.query",
107                         bookID=bookId,
108                 )
109                 items = tree.find("chapters")
110                 return self._process_list(items, ["title", "url"])
111
112         def _get_page_with_validation(self, **params):
113                 encodedParams = urllib.urlencode(params)
114                 page = self._browser.download("http://tech.lds.org/radio?%s" % encodedParams)
115                 if not page:
116                         raise RuntimeError("Blank page")
117                 tree = ElementTree.fromstring(page)
118
119                 if tree.tag == "apiresults":
120                         desc = tree.find("ErrorDescription")
121                         raise RuntimeError(desc.text)
122                 else:
123                         results = tree.find("apiresults")
124                         if not results.attrib["success"]:
125                                 raise RuntimeError("Could not determine radio languages")
126
127                 return tree
128
129         def _process_list(self, tree, elements):
130                 for item in tree.getchildren():
131                         data = {"id": item.attrib["ID"]}
132                         for element in elements:
133                                 data[element] = item.find(element).text
134                         yield data
135
136
137 if __name__ == "__main__":
138         b = Backend()
139
140         print list(b.get_languages())
141
142         if False:
143                 channels = list(b.get_radio_channels())
144                 print channels
145                 for chanData in channels:
146                         programs = list(b.get_radio_channel_programming(chanData["id"]))
147                         print programs
148
149         if False:
150                 confs = list(b.get_conferences(1))
151                 print confs
152                 for confData in confs:
153                         sessions = list(b.get_conference_sessions(confData["id"]))
154                         for sessionData in sessions:
155                                 talks = list(b.get_conference_talks(sessionData["id"]))
156                                 print talks
157
158         if False:
159                 mags = list(b.get_magazines(1))
160                 print mags
161                 for magData in mags:
162                         issues = list(b.get_magazine_issues(magData["id"]))
163                         issues
164                         for issueData in issues:
165                                 articles = list(b.get_magazine_articles(issueData["id"]))
166                                 print articles
167
168         if False:
169                 mags = list(b.get_scriptures(1))
170                 print mags
171                 for magData in mags:
172                         books = list(b.get_scripture_books(magData["id"]))
173                         print books
174                         for bookData in books:
175                                 chapters = list(b.get_scripture_chapters(bookData["id"]))
176                                 print chapters