diff options
Diffstat (limited to 'youtube_podcaster/podcastupdater.py')
| -rw-r--r-- | youtube_podcaster/podcastupdater.py | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/youtube_podcaster/podcastupdater.py b/youtube_podcaster/podcastupdater.py index 4a0a017..75b8b10 100644 --- a/youtube_podcaster/podcastupdater.py +++ b/youtube_podcaster/podcastupdater.py | |||
| @@ -2,6 +2,7 @@ import pickle | |||
| 2 | import os | 2 | import os |
| 3 | import time | 3 | import time |
| 4 | import hashlib | 4 | import hashlib |
| 5 | import sys | ||
| 5 | 6 | ||
| 6 | from feedgen.feed import FeedGenerator | 7 | from feedgen.feed import FeedGenerator |
| 7 | 8 | ||
| @@ -11,11 +12,19 @@ from . import ( | |||
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | class PodcastUpdater: | 14 | class PodcastUpdater: |
| 14 | def __init__(self, youtube_config, podcast_config): | 15 | def __init__(self, config): |
| 15 | self.podcasts = podcast_config | 16 | self.podcasts = config.podcasts |
| 16 | self.youtube = youtube.Youtube(youtube_config["api-key"]) | 17 | self.youtube = youtube.Youtube(config.youtube["api-key"]) |
| 18 | |||
| 19 | if sys.platform == "linux" and not hasattr(sys, "real_prefix"): | ||
| 20 | self.data_dir = "/var/lib/youtube-podcaster" | ||
| 21 | else: | ||
| 22 | self.data_dir = "%s/var/lib/youtube-podcaster" % (sys.prefix) | ||
| 23 | |||
| 24 | os.makedirs(self.data_dir, 0o755, True) | ||
| 25 | |||
| 26 | self.feeds_file = "%s/feeds.dump" % (self.data_dir) | ||
| 17 | 27 | ||
| 18 | self.feeds_file = "feeds.save" | ||
| 19 | if os.path.isfile(self.feeds_file): | 28 | if os.path.isfile(self.feeds_file): |
| 20 | with open(self.feeds_file, "rb") as feeds: | 29 | with open(self.feeds_file, "rb") as feeds: |
| 21 | self.feeds = pickle.load(feeds) | 30 | self.feeds = pickle.load(feeds) |
| @@ -42,6 +51,8 @@ class PodcastUpdater: | |||
| 42 | 51 | ||
| 43 | def update_podcast(self, channel, playlist): | 52 | def update_podcast(self, channel, playlist): |
| 44 | feed_id = hashlib.sha1(bytes("%s %s" % (channel, playlist), "UTF-8")).hexdigest() | 53 | feed_id = hashlib.sha1(bytes("%s %s" % (channel, playlist), "UTF-8")).hexdigest() |
| 54 | feed_file = "%s/%s.xml" % (self.data_dir, feed_id) | ||
| 55 | |||
| 45 | yt_channel = self.youtube.get_channel(channel)[0] | 56 | yt_channel = self.youtube.get_channel(channel)[0] |
| 46 | yt_playlists = self.youtube.get_playlists(yt_channel, 50) | 57 | yt_playlists = self.youtube.get_playlists(yt_channel, 50) |
| 47 | 58 | ||
| @@ -58,17 +69,16 @@ class PodcastUpdater: | |||
| 58 | 69 | ||
| 59 | if feed.last_updated < time.time() - 600: | 70 | if feed.last_updated < time.time() - 600: |
| 60 | self.populate_feed(feed, feed_id, yt_playlist) | 71 | self.populate_feed(feed, feed_id, yt_playlist) |
| 72 | feed.rss_file(feed_file) | ||
| 61 | 73 | ||
| 62 | feed_file = "%s.xml" % (feed_id) | 74 | with open(self.feeds_file, "wb") as feeds: |
| 63 | self.feeds[feed_id].rss_file(feed_file) | 75 | pickle.dump(self.feeds, feeds) |
| 64 | |||
| 65 | with open(self.feeds_file, "wb") as feed: | ||
| 66 | pickle.dump(self.feeds, feed) | ||
| 67 | 76 | ||
| 68 | return "%s.xml" % (feed_id) | 77 | return feed_file |
| 69 | 78 | ||
| 70 | def add_feed(self, feed_id, yt_playlist): | 79 | def add_feed(self, feed_id, yt_playlist): |
| 71 | feed = FeedGenerator() | 80 | feed = FeedGenerator() |
| 81 | |||
| 72 | feed.load_extension("podcast") | 82 | feed.load_extension("podcast") |
| 73 | feed.id(feed_id) | 83 | feed.id(feed_id) |
| 74 | feed.title(yt_playlist["snippet"]["title"]) | 84 | feed.title(yt_playlist["snippet"]["title"]) |
| @@ -78,7 +88,9 @@ class PodcastUpdater: | |||
| 78 | feed.link(href="https://www.youtube.com/playlist?list=%s" % (yt_playlist["id"])) | 88 | feed.link(href="https://www.youtube.com/playlist?list=%s" % (yt_playlist["id"])) |
| 79 | feed.rss_str(pretty=True) | 89 | feed.rss_str(pretty=True) |
| 80 | feed.last_updated = 0 | 90 | feed.last_updated = 0 |
| 91 | |||
| 81 | self.feeds[feed_id] = feed | 92 | self.feeds[feed_id] = feed |
| 93 | |||
| 82 | return feed | 94 | return feed |
| 83 | 95 | ||
| 84 | def populate_feed(self, feed, feed_id, yt_playlist, max_results=5): | 96 | def populate_feed(self, feed, feed_id, yt_playlist, max_results=5): |
| @@ -93,7 +105,9 @@ class PodcastUpdater: | |||
| 93 | break | 105 | break |
| 94 | else: | 106 | else: |
| 95 | url, size, mime = downloader.download(video, video_id, feed_id) | 107 | url, size, mime = downloader.download(video, video_id, feed_id) |
| 108 | |||
| 96 | feed_entry = feed.add_entry() | 109 | feed_entry = feed.add_entry() |
| 110 | |||
| 97 | feed_entry.id(video_id) | 111 | feed_entry.id(video_id) |
| 98 | feed_entry.guid(video_id) | 112 | feed_entry.guid(video_id) |
| 99 | feed_entry.title(video["snippet"]["title"]) | 113 | feed_entry.title(video["snippet"]["title"]) |
