🚸 Added cli args

This commit is contained in:
2021-04-10 21:21:15 +03:00
parent 5542467fbb
commit ac2707b506

70
main.py
View File

@@ -3,45 +3,65 @@ from rich.console import Console
from pathlib import Path from pathlib import Path
import os import os
import json import json
import argparse
from getch import getch from getch import getch
import keyboard
import youtube_dl import youtube_dl
from get_channels import retrieve_youtube_subscriptions from get_channels import retrieve_youtube_subscriptions
output_dir = 'output' parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', action='store', type=str,
help='Path to json file generated by previously running this program.')
parser.add_argument('-o', '--output', action='store', type=str,
help='Output folder.')
parser.add_argument('-a', '--all', action='store_true',
help='Download all subscriptions.')
args = parser.parse_args()
json_input = args.input
output_dir = args.output or 'output'
download_all = args.all
c = Console() c = Console()
ydl_opts = { ydl_opts = {
'format': 'best', 'format': 'best'
'outtmpl': '%(id)s.%(ext)s'
} }
all_channels = retrieve_youtube_subscriptions() if json_input:
curr_channel = 0 f = open(json_input, "r", encoding='utf-8')
c.print(f'You will be prompted if you want to download \ all_channels = json.loads(f.read())
a channel for each of your subscriptions. (total {len(all_channels)})') f.close()
for ch in all_channels: else:
curr_channel += 1 all_channels = retrieve_youtube_subscriptions()
c.print(f'[dim][{curr_channel}/{len(all_channels)}]:[/dim] {ch["title"]} [cyan]\[y/n]') curr_channel = 0
while True: c.print(f'You will be prompted if you want to download \
key = getch() a channel for each of your subscriptions. (total {len(all_channels)})')
if key == "y": for ch in all_channels:
if download_all:
ch['download'] = True ch['download'] = True
break
elif key == "n":
ch['download'] = False
break
else: else:
c.print('Press "y" or "n"', style='orange') curr_channel += 1
c.print(f'[dim][{curr_channel}/{len(all_channels)}]:[/dim] {ch["title"]} [cyan]\[y/n]')
while True:
key = getch()
if key == "y":
ch['download'] = True
break
elif key == "n":
ch['download'] = False
break
else:
c.print('Press "y" or "n"', style='orange')
c.print('All done! 🎉') c.print('All done! 🎉')
c.print('Saving to output.json...', style='italic') c.print('Saving to download_list.json...', style='italic')
f = open("output.json", "a", encoding='utf-8') f = open("download_list.json", "w", encoding='utf-8')
f.write(json.dumps(all_channels)) f.write(json.dumps(all_channels))
f.close() f.close()
for ch in all_channels: for ch in all_channels:
if ch['download']: if ch['download']:
ydl_opts['outtmpl'] = '{}/{}/%(id)s.%(ext)s'.format(output_dir, ch['title']) ydl_opts['outtmpl'] = '{}/{}/%(title)s.%(ext)s'.format(output_dir, ch['title'])
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
Path(os.path.join(output_dir, ch['title'])).mkdir(parents=True, exist_ok=True) Path(os.path.join(output_dir, ch['title'])).mkdir(parents=True, exist_ok=True)
ydl.download(['https://www.youtube.com/channel/{}'.format(ch["id"])]) ydl.download(['https://www.youtube.com/channel/{}'.format(ch["id"])])