Class creation

This commit is contained in:
Firstname Lastname 2019-12-12 22:23:43 +01:00
parent b87310f7ca
commit 6e22039cfe
4 changed files with 154 additions and 110 deletions

34
core.py Normal file
View File

@ -0,0 +1,34 @@
from __future__ import unicode_literals
import youtube_dl
from feedgen.feed import FeedGenerator
import os
import sys
import argparse
import ydl
#TODO:
# offer download video option
# create good README.md
# .gitignore
# LICENSE
parser = argparse.ArgumentParser()
parser.add_argument("-i","--interface", help="Launch GUI (beta)",action="store_true")
parser.add_argument("-s","--simulate", help="Do not download videos, just do as if",action="store_true")
parser.add_argument("-q","--quiet", help="Very quiet option for youtube-dl",action="store_true")
parser.add_argument("-f","--feed", help="Create Podcast feed",action="store_true")
parser.add_argument("-video", help="Download videos instead of creating audio",action="store_true")
parser.add_argument("-d","--dir", help="Define download directory for files, default value:'~/Vidéos'", default="~/Vidéos")
parser.add_argument("-url", help="Define base url for podcasts, default value:'http://podcasts.lutix.org'", default="http://podcasts.lutix.org")
parser.add_argument("-yturl","--youtube_url", help="Define youtube url to fetch", default ="https://www.youtube.com/watch?v=xJO5GstqTSY&list=PLxzM9a5lhAumFRpcigmGY1QLDYxb4-P2B")
args = parser.parse_args()
if __name__ == "__main__":
if not args.interface:
ydlo = ydl.ydl_object(args)
ydlo.print_infos()
ydlo.process_args()
else:
print('Waiting for interface to be loaded...')

BIN
dist/ydl → dist/core vendored

Binary file not shown.

25
interface.py Normal file
View File

@ -0,0 +1,25 @@
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
Frame1 = tk.Frame(self, borderwidth=2, relief="groove")
Frame1.pack(side="left", padx=330, pady=330)
nom = tk.Label(self, text = 'Votre nom :')
reponse = tk.Entry(self)
valeur = tk.Button(self, text =' Valider', command=self.repondre)
reponse.pack()
valeur.pack()
def repondre():
print("reponse")
if __name__ == "__main__":
root = tk.Tk()
app = Application(master=root)
app.mainloop()

93
ydl.py
View File

@ -3,35 +3,20 @@ import youtube_dl
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
import os import os
import sys import sys
import argparse
import main
#TODO: class ydl_object:
# offer download video option def __init__(self,args):
# create good README.md self.args = args
# .gitignore self.infos = self.fetch_info()
# LICENSE # print(self.args)
parser = argparse.ArgumentParser() def fetch_info(self):
parser.add_argument("-s","--simulate", help="Do not download videos, just do as if",action="store_true")
parser.add_argument("-q","--quiet", help="Very quiet option for youtube-dl",action="store_true")
parser.add_argument("-f","--feed", help="Create Podcast feed",action="store_true")
parser.add_argument("-video", help="Download videos instead of creating audio",action="store_true")
parser.add_argument("-d","--dir", help="Define download directory for files, default value:'~/Vidéos'", default="~/Vidéos")
parser.add_argument("-url", help="Define base url for podcasts, default value:'http://podcasts.lutix.org'", default="http://podcasts.lutix.org")
parser.add_argument("-yturl","--youtube_url", help="Define youtube url to fetch", default ="https://www.youtube.com/watch?v=xJO5GstqTSY&list=PLxzM9a5lhAumFRpcigmGY1QLDYxb4-P2B")
args = parser.parse_args()
ydlo = main.ydl_object(args)
def fetch_info(url_yt):
print('Fetching Youtube Link informations...') print('Fetching Youtube Link informations...')
infos = youtube_dl.YoutubeDL({'quiet':True,'ignoreerrors':True}).extract_info(url_yt, False) return youtube_dl.YoutubeDL({'quiet':True,'ignoreerrors':True}).extract_info(self.args.youtube_url, False)
print('...Done')
return infos
def create_feed(results): def create_feed(self):
""" """
replace results by info
#results keys #results keys
_type entries id title uploader uploader_id uploader_url extractor webpage_url webpage_url_basename extractor_key _type entries id title uploader uploader_id uploader_url extractor webpage_url webpage_url_basename extractor_key
""" """
@ -39,44 +24,44 @@ def create_feed(results):
fg.load_extension('podcast') fg.load_extension('podcast')
fg.podcast.itunes_category('Podcasting') fg.podcast.itunes_category('Podcasting')
fg.title(results['title']) fg.title(self.infos['title'])
fg.description('none') fg.description('none')
fg.link(href=args.url,rel='self') fg.link(href=self.args.url,rel='self')
for item in results['entries']: for item in self.infos['entries']:
""" """
#results['entries'] keys #infos['entries'] keys
id uploader uploader_id uploader_url channel_id channel_url upload_date license creator title alt_title thumbnail description categories tags subtitles automatic_captions duration id uploader uploader_id uploader_url channel_id channel_url upload_date license creator title alt_title thumbnail description categories tags subtitles automatic_captions duration
""" """
fe = fg.add_entry() fe = fg.add_entry()
fe.id(item['id']) fe.id(item['id'])
fe.title(item['title']) fe.title(item['title'])
fe.description(item['description']) fe.description(item['description'])
item_full_path = args.url +'/'+results['title']+'/'+item['title']+'.mp3' item_full_path = self.args.url +'/'+self.infos['title']+'/'+item['title']+'.mp3'
fe.enclosure(item_full_path,str(item['duration']),'audio/mpeg') fe.enclosure(item_full_path,str(item['duration']),'audio/mpeg')
fg.rss_str(pretty=True) fg.rss_str(pretty=True)
# create folder of feed if it doesn't exists # create folder of feed if it doesn't exists
os.makedirs(args.dir+'/'+results['title'], exist_ok=True) os.makedirs(self.args.dir+'/'+self.infos['title'], exist_ok=True)
fg.rss_file(args.dir+'/'+results['title']+'/podcast.xml') fg.rss_file(self.args.dir+'/'+self.infos['title']+'/podcast.xml')
return True return True
def my_hook(d): def my_hook(self,d):
if d['status'] == 'finished': if d['status'] == 'finished':
print('Done downloading, now converting ...') print('Done downloading, now converting ...')
def yt_download(ydl_opts): def yt_download(self,ydl_opts):
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print('Downloading Videos...') print('Downloading Videos...')
print('Depending on number of files, it can take a while...') print('Depending on number of files, it can take a while...')
results = ydl.extract_info(args.youtube_url, not args.simulate) results = ydl.extract_info(self.args.youtube_url, not self.args.simulate)
print('Done') print('Done')
def create_opts(infos,type): def create_opts(self,type):
if type=='audio': if type=='audio':
ydl_opts = { ydl_opts = {
'quiet':args.quiet, 'quiet':self.args.quiet,
'format': 'bestaudio/best', 'format': 'bestaudio/best',
'ignoreerrors': True, 'ignoreerrors': True,
'postprocessors': [{ 'postprocessors': [{
@ -84,7 +69,7 @@ def create_opts(infos,type):
'preferredcodec': 'mp3', 'preferredcodec': 'mp3',
'preferredquality': '192', 'preferredquality': '192',
}], }],
'progress_hooks': [my_hook], 'progress_hooks': [self.my_hook],
} }
elif type=='video': elif type=='video':
ydl_opts= {} ydl_opts= {}
@ -92,42 +77,42 @@ def create_opts(infos,type):
# if youtube url is a list, then download archive # if youtube url is a list, then download archive
print('Fetching Url properties...') print('Fetching Url properties...')
if 'entries' in infos: if 'entries' in self.infos:
ydl_opts['download_archive']= args.dir+'/archive.txt' ydl_opts['download_archive']= self.args.dir+'/archive.txt'
ydl_opts['outtmpl']= args.dir+'/%(playlist_title)s/%(title)s.%(ext)s' ydl_opts['outtmpl']= self.args.dir+'/%(playlist_title)s/%(title)s.%(ext)s'
else: else:
ydl_opts['download_archive']= args.dir+'/archive.txt' ydl_opts['download_archive']= self.args.dir+'/archive.txt'
ydl_opts['outtmpl']= args.dir+'/%(title)s.%(ext)s' ydl_opts['outtmpl']= self.args.dir+'/%(title)s.%(ext)s'
print('Done') print('Done')
return ydl_opts return ydl_opts
if __name__ == "__main__": def print_infos(self):
print('Downloads folder name: {}'.format(args.dir)) print('Downloads folder name: {}'.format(self.args.dir))
infos = ydlo.infos if 'entries' in self.infos:
# if youtube url linked to a list
if 'entries' in infos:
print('') print('')
print('List of episodes online:') print('List of episodes online:')
for k,i in enumerate(infos['entries']): for k,i in enumerate(self.infos['entries']):
print('Episode {}: {}, Durée:{}'.format(k+1,i['title'],i['duration'])) print('Episode {}: {}, Durée:{}'.format(k+1,i['title'],i['duration']))
if args.feed: def process_args(self):
if 'entries' in self.infos:
if self.args.feed:
print('') print('')
print('Feed creation...{}'.format("Done" if create_feed(infos) else "Error")) print('Feed creation...{}'.format("Done" if self.create_feed() else "Error"))
else: else:
print('') print('')
print('Warning: no podcast feed has been created, please add --feed argument.') print('Warning: no podcast feed has been created, please add --feed argument.')
else: else:
if args.feed: if self.args.feed:
print('') print('')
print('In spite of --feed argument, as you specified contradictory options, no feed can be created!') print('In spite of --feed argument, as you specified contradictory options, no feed can be created!')
if args.simulate: if self.args.simulate:
print('Downloads...Not requested!') print('Downloads...Not requested!')
else: else:
yt_download(create_opts(infos, type='audio' if not args.video else 'video')) self.yt_download(self.create_opts(type='audio' if not self.args.video else 'video'))