aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_podcaster/youtube/downloader.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2015-10-19 21:38:00 +0200
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2015-10-19 21:38:00 +0200
commit9cc311ca5376bfcbcacf7ac492f5958acfac0682 (patch)
tree451b076d21bbaa477c901b83d325d7360be2cd67 /youtube_podcaster/youtube/downloader.py
parenta7fe15b99d64904855ba2bd3649b1a3642824a75 (diff)
downloadyoutube-podcaster-9cc311ca5376bfcbcacf7ac492f5958acfac0682.tar.gz
youtube-podcaster-9cc311ca5376bfcbcacf7ac492f5958acfac0682.tar.bz2
youtube-podcaster-9cc311ca5376bfcbcacf7ac492f5958acfac0682.zip
Added commandline options and config file
Diffstat (limited to 'youtube_podcaster/youtube/downloader.py')
-rw-r--r--youtube_podcaster/youtube/downloader.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/youtube_podcaster/youtube/downloader.py b/youtube_podcaster/youtube/downloader.py
index ca1327b..529902a 100644
--- a/youtube_podcaster/youtube/downloader.py
+++ b/youtube_podcaster/youtube/downloader.py
@@ -1,41 +1,44 @@
1#!/usr/bin/env python3 1#!/usr/bin/env python3
2 2
3import youtube_dl
4import os 3import os
4import mimetypes
5
6import youtube_dl
5 7
6 8
7class Downloader: 9class Downloader:
8 instance = None 10 instance = None
9 11
10 def get_instance(file_format, location, base_url): 12 def get_instance(file_format, location, base_url):
11 if Downloader.instance: 13 if not Downloader.instance:
12 return Downloader.instance
13 else:
14 Downloader.instance = Downloader(file_format, location, base_url) 14 Downloader.instance = Downloader(file_format, location, base_url)
15 return Downloader.instance 15 return Downloader.instance
16 16
17 def __init__(self, file_format, location, base_url): 17 def __init__(self, file_format, location, base_url):
18 self.file_format = file_format 18 self.file_format = file_format
19 self.location = location 19 self.location = location
20 self.base_url = base_url 20 self.base_url = base_url
21 21
22 if file_format == "vorbis":
23 self.extension = "ogg"
24
22 def download(self, video, video_id, feed_id): 25 def download(self, video, video_id, feed_id):
23 output = "%s/%s/%s.ogg" % (self.location, feed_id, video_id) 26 output = "%s/%s/%s.%s" % (self.location, feed_id, video_id, self.extension)
24 options = {"format": "bestaudio/best", 27 options = {"format": "bestaudio/best",
25 "outtmpl": output, 28 "outtmpl": output,
26 "postprocessors": [{ 29 "postprocessors": [{
27 "key": "FFmpegExtractAudio", 30 "key": "FFmpegExtractAudio",
28 "preferredcodec": self.file_format 31 "preferredcodec": self.file_format
29 }], 32 }],
30 "nooverwrites": True 33 "nooverwrites": True}
31 } 34
32 35
33 video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"]) 36 video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"])
34 youtube_dl.YoutubeDL(options).download([video_url]) 37 youtube_dl.YoutubeDL(options).download([video_url])
35 38
36 url = "%s/%s/%s.ogg" % (self.base_url, feed_id, video_id) 39 url = "%s/%s/%s.%s" % (self.base_url, feed_id, video_id, self.extension)
37 size = str(os.path.getsize(output)) 40 size = str(os.path.getsize(output))
38 mime = "audio/ogg" 41 mime = mimetypes.guess_type(output)[0]
39 42
40 return (url, size, mime) 43 return (url, size, mime)
41 44