diff options
Diffstat (limited to 'youtube_podcaster/podcastfeeder.py')
| -rw-r--r-- | youtube_podcaster/podcastfeeder.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/youtube_podcaster/podcastfeeder.py b/youtube_podcaster/podcastfeeder.py new file mode 100644 index 0000000..3f752d2 --- /dev/null +++ b/youtube_podcaster/podcastfeeder.py | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | from http.server import BaseHTTPRequestHandler | ||
| 2 | |||
| 3 | from .podcastupdater import ( | ||
| 4 | PodcastUpdater | ||
| 5 | ) | ||
| 6 | |||
| 7 | |||
| 8 | def create_feeder(youtube_config, podcast_config): | ||
| 9 | class PodcastFeeder(BaseHTTPRequestHandler): | ||
| 10 | def __init__(self, request, client_address, server): | ||
| 11 | self.updater = PodcastUpdater(youtube_config, podcast_config) | ||
| 12 | super(PodcastFeeder, self).__init__(request, client_address, server) | ||
| 13 | |||
| 14 | def do_GET(self): | ||
| 15 | path = self.path.split('/') | ||
| 16 | |||
| 17 | if len(path) == 3: | ||
| 18 | channel = path[1] | ||
| 19 | playlist = path[2] | ||
| 20 | else: | ||
| 21 | return self.return_error(404) | ||
| 22 | |||
| 23 | xml = self.updater.get_xml(channel, playlist) | ||
| 24 | |||
| 25 | if not xml: | ||
| 26 | return self.return_error(404) | ||
| 27 | else: | ||
| 28 | self.send_response(200) | ||
| 29 | self.send_header("Content-type", "text/xml") | ||
| 30 | self.end_headers() | ||
| 31 | self.wfile.write(bytes(xml, 'UTF-8')) | ||
| 32 | |||
| 33 | def return_error(self, code): | ||
| 34 | self.send_response(code) | ||
| 35 | self.send_header("Content-type", "text/html") | ||
| 36 | self.end_headers() | ||
| 37 | |||
| 38 | reponse = "<body>Error: %s</body>" % (code) | ||
| 39 | self.wfile.write(bytes(reponse, 'UTF-8')) | ||
| 40 | |||
| 41 | return PodcastFeeder | ||
| 42 | |||
| 43 | # vim: set ts=8 sw=4 tw=0 et : | ||
