From 29c193381133199ec914e834401033f085e813c9 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Wed, 21 Oct 2015 18:04:42 +0200 Subject: Opus, fixes en tmp's Added: - Opus fileformat - Downloads first go to a tmp dir Fixed: - Download config not being used --- youtube_podcaster/config.py | 1 + youtube_podcaster/podcastupdater.py | 40 ++++++++++++++++++++++++--------- youtube_podcaster/youtube/downloader.py | 40 +++++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/youtube_podcaster/config.py b/youtube_podcaster/config.py index bfce902..e535dcf 100644 --- a/youtube_podcaster/config.py +++ b/youtube_podcaster/config.py @@ -41,6 +41,7 @@ class Config: self.server = config["server"] self.youtube = config["youtube"] self.podcasts = config["podcasts"] + self.downloads = config["downloads"] except KeyError as e: raise ConfigException("Missing %s-section in %s" % ( str(e), config)) diff --git a/youtube_podcaster/podcastupdater.py b/youtube_podcaster/podcastupdater.py index 75b8b10..8afebfa 100644 --- a/youtube_podcaster/podcastupdater.py +++ b/youtube_podcaster/podcastupdater.py @@ -10,11 +10,14 @@ from . import ( youtube, ) +from threading import Thread + class PodcastUpdater: def __init__(self, config): self.podcasts = config.podcasts self.youtube = youtube.Youtube(config.youtube["api-key"]) + self.downloads = config.downloads if sys.platform == "linux" and not hasattr(sys, "real_prefix"): self.data_dir = "/var/lib/youtube-podcaster" @@ -95,7 +98,14 @@ class PodcastUpdater: def populate_feed(self, feed, feed_id, yt_playlist, max_results=5): videos = self.youtube.get_playlist_items(yt_playlist, max_results) - downloader = youtube.Downloader.get_instance("vorbis", "downloads", "192.168.178.100") + + file_format = self.downloads["format"] + download_path = self.downloads["path"] + download_url = self.downloads["url"] + + downloader = youtube.Downloader.get_instance(file_format, download_path, download_url) + + threads = [] entries = feed.entry() for video in videos: @@ -104,17 +114,27 @@ class PodcastUpdater: if entry.id() == video_id: break else: - url, size, mime = downloader.download(video, video_id, feed_id) - - feed_entry = feed.add_entry() + t = Thread(target=self.process_video, args=(downloader, video, video_id, feed_id)) + threads.append(t) + t.start() - feed_entry.id(video_id) - feed_entry.guid(video_id) - feed_entry.title(video["snippet"]["title"]) - feed_entry.description(video["snippet"]["description"]) - feed_entry.published(video["snippet"]["publishedAt"]) - feed_entry.enclosure(url, size, mime) + for t in threads: + t.join() feed.last_updated = time.time() + def process_video(self, downloader, video, video_id, feed_id): + url, size, mime = downloader.download(video, video_id, feed_id) + + feed = self.feeds[feed_id] + feed_entry = feed.add_entry() + + feed_entry.id(video_id) + feed_entry.guid(video_id) + feed_entry.title(video["snippet"]["title"]) + feed_entry.description(video["snippet"]["description"]) + feed_entry.published(video["snippet"]["publishedAt"]) + feed_entry.enclosure(url, size, mime) + + # vim: set ts=8 sw=4 tw=0 et : diff --git a/youtube_podcaster/youtube/downloader.py b/youtube_podcaster/youtube/downloader.py index 529902a..24fa1b0 100644 --- a/youtube_podcaster/youtube/downloader.py +++ b/youtube_podcaster/youtube/downloader.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import os +import sys import mimetypes import youtube_dl @@ -19,26 +20,51 @@ class Downloader: self.location = location self.base_url = base_url + self.downloaded = [] + if file_format == "vorbis": self.extension = "ogg" + elif file_format == "opus": + self.extension = "opus" + + if sys.platform == "linux" and not hasattr(sys, "real_prefix"): + self.tmp_dir = "/tmp/youtube-podcaster" + else: + self.tmp_dir= "%s/tmp/youtube-podcaster" % (sys.prefix) + + os.makedirs(self.tmp_dir, 0o755, True) def download(self, video, video_id, feed_id): - output = "%s/%s/%s.%s" % (self.location, feed_id, video_id, self.extension) + + # Real output + filename = "%s.%s" % (video_id, self.extension) + output_dir = "%s/%s" % (self.location, feed_id) + output = "%s/%s" % (output_dir, filename) + + # Tmp output + tmp_filename = "%s.webm" % (video_id) + tmp_output = "%s/%s" % (self.tmp_dir, tmp_filename) + options = {"format": "bestaudio/best", - "outtmpl": output, + "outtmpl": tmp_output, "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": self.file_format - }], - "nooverwrites": True} - + }]} video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"]) youtube_dl.YoutubeDL(options).download([video_url]) + tmp_output = "%s/%s" % (self.tmp_dir, filename) + url = "%s/%s/%s.%s" % (self.base_url, feed_id, video_id, self.extension) - size = str(os.path.getsize(output)) - mime = mimetypes.guess_type(output)[0] + size = str(os.path.getsize(tmp_output)) + mime = mimetypes.guess_type(tmp_output)[0] + + os.makedirs(output_dir, 0o755, True) + os.rename(tmp_output, output) + + self.downloaded.append(output) return (url, size, mime) -- cgit v1.2.3