-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.py
executable file
·137 lines (105 loc) · 3.83 KB
/
fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/local/bin/python3 -u
import pprint
import datetime
import os
import sys
import socket
import getopt
import logging
import subprocess
from io import StringIO
import string
import ssl
import urllib
import certifi
from xml.dom import minidom
from html.parser import HTMLParser
from ytdragon.utils import print_pretty
from ytdragon.ytpage import get_uid_from_ref
from ytdragon.ytmeta import create_default_vid_meta
from ytdragon.ytlist import load_list
from ytdragon.download import download_with_retry, download_list
### User Config Variable ----------------------------
quite = False
enable_line_log = True
line_log_path = "./ytdragon.log"
enable_vid_log = True
vidlog_path = "./logs"
deep_debug = True
default_host = "youtube.com"
default_hurl = "https://"+default_host
#### -- Logging Related Functions -------------------
def setup_main_logger():
logm = logging.getLogger() # Basic level is DEBUG only to allow other handlers to work
logm.setLevel(logging.DEBUG)
cfh = logging.StreamHandler()
if(quite):
cfh.setLevel(logging.WARN)
else:
cfh.setLevel(logging.INFO)
logm.addHandler(cfh)
if(enable_line_log):
ifh = logging.FileHandler(line_log_path,mode='a')
ifh.setLevel(logging.INFO)
iff = logging.Formatter('%(asctime)s:[%(levelname)s]:%(message)s')
ifh.setFormatter(iff)
logm.addHandler(ifh)
return logm
#---------------------------------------------------------------
# Support functions for Main
def parse_arguments(argv):
logm = logging.getLogger()
usage_str = "Usage: %s -f|--folder='destination' <download_reference> \n"
usage_str += " Download Reference can be defined as below \n"
usage_str += " It can be direct URL such as http://www.youtube.com/watch?v=<vid> \n"
usage_str += " or just v=<vid> or vid=<vid> or video=<vid> \n"
usage_str += " Youtube Playlists and YT Lists can be directly downloaded from here with following download_refs \n"
usage_str += " YT List downlad ref: l=<ytlist_file.yl> or yl=<ytlist_file.yl> or ytlist=<ytlist_file.yl>\n"
usage_str += " Playlist download ref: playlist_url p=<plid> or pl=<plid> or playlist=<plid>\n"
usage_str += " Remember no spaces before or after '=' \n"
usage_str += " Currently channel and playlist are not supported to be fetched directly so use 'extract' for the same"
folder = ""
if (len(argv) == 0):
logm.critical(usage_str,sys.argv[0])
sys.exit(2)
try:
opts, args = getopt.getopt(argv,"f:",["folder="])
except getopt.GetoptError as err:
logm.error("Error in Options: %s",str(err))
logm.critical(usage_str,sys.argv[0])
sys.exit(2)
for opt, arg in opts:
if opt in ("-f", "--folder"):
folder = arg
if ( folder == ""):
logm.warning("Missing destination folder. Assuming './' (current working directory) ")
folder = "./"
logm.debug("Destination folder for streams: %s",folder)
if ( len(args) > 0):
uid_ref = args[0]
else:
logm.critical("Missing source. supply at least one reference to download")
logm.critical(usage_str,sys.argv[0])
sys.exit(2)
return folder, uid_ref
#---------------------------------------------------------------
# Main function
logm = setup_main_logger()
(folder, uidref) = parse_arguments(sys.argv[1:])
status, uid_type, uid = get_uid_from_ref(uidref)
success = 0
if(status != "OK"):
logm.info("Skipping uid %s: Status %s",uid,status)
exit();
if (uid_type == "video"):
vid = uid
vid_item = create_default_vid_meta(uid)
success = download_with_retry(vid_item,folder)
elif( (uid_type == "ytlist") or (uid_type == "playlist") or (uid_type == "channel")):
uid_list = load_list(uid,uid_type)
success = download_list(uid_list,folder)
else:
logm.info("Type currently not supported. or Error in id_reference")
logm.info("Status {} id_type {} uid {}".format(status, uid_type, uid))
logm.info("Downloaded {} videos".format(success))
logm.info("Good bye... {}".format("Enjoy the video!" if (success>0) else "Bad luck"))