san
commited on
Commit
·
c411b95
1
Parent(s):
a491b92
covert relative path to absolute path at the time being passed from commad line, updated Downlaoding message to print where the file is saved to, check to make sure user passes both extension and resolution and print to shell if not sure what resolution to choose, and fix for progress bar dispay on windows.
Browse files- pytube/models.py +3 -2
- pytube/utils.py +10 -2
- scripts/pytubectl +13 -2
pytube/models.py
CHANGED
@@ -7,6 +7,7 @@ try:
|
|
7 |
except ImportError:
|
8 |
from urllib.request import urlopen
|
9 |
from sys import exit
|
|
|
10 |
|
11 |
|
12 |
class Video(object):
|
@@ -65,8 +66,8 @@ class Video(object):
|
|
65 |
try:
|
66 |
with open(fullpath, 'wb') as dst_file:
|
67 |
# Print downloading message
|
68 |
-
print("\nDownloading: '{0}.{1}' (Bytes: {2})\n\n".format(
|
69 |
-
self.filename, self.extension, file_size))
|
70 |
|
71 |
while True:
|
72 |
self._buffer = response.read(chunk_size)
|
|
|
7 |
except ImportError:
|
8 |
from urllib.request import urlopen
|
9 |
from sys import exit
|
10 |
+
from pytube.utils import sizeof
|
11 |
|
12 |
|
13 |
class Video(object):
|
|
|
66 |
try:
|
67 |
with open(fullpath, 'wb') as dst_file:
|
68 |
# Print downloading message
|
69 |
+
print("\nDownloading: '{0}.{1}' (Bytes: {2}) \nto path: {3}\n\n".format(
|
70 |
+
self.filename, self.extension, sizeof(file_size), path))
|
71 |
|
72 |
while True:
|
73 |
self._buffer = response.read(chunk_size)
|
pytube/utils.py
CHANGED
@@ -1,7 +1,16 @@
|
|
|
|
1 |
import re
|
|
|
|
|
2 |
from sys import stdout, platform
|
3 |
from time import clock
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def safe_filename(text, max_length=200):
|
6 |
"""
|
7 |
Sanitizes filenames for many operating systems.
|
@@ -72,7 +81,6 @@ def print_status(progress, file_size, start):
|
|
72 |
|
73 |
percentDone = int(progress) * 100. / file_size
|
74 |
done = int(50 * progress / int(file_size))
|
75 |
-
|
76 |
-
stdout.write("%s[%s%s][%3.2f%%] %s at %s/s " % (rawtxt, '=' * done, ' ' * (50-done), percentDone,
|
77 |
sizeof(file_size), sizeof(progress//(clock() - start))))
|
78 |
stdout.flush()
|
|
|
1 |
+
import argparse
|
2 |
import re
|
3 |
+
|
4 |
+
from os import path
|
5 |
from sys import stdout, platform
|
6 |
from time import clock
|
7 |
|
8 |
+
|
9 |
+
class FullPaths(argparse.Action):
|
10 |
+
"""Expand user- and relative-paths"""
|
11 |
+
def __call__(self, parser, namespace, values, option_string=None):
|
12 |
+
setattr(namespace, self.dest, path.abspath(path.expanduser(values)))
|
13 |
+
|
14 |
def safe_filename(text, max_length=200):
|
15 |
"""
|
16 |
Sanitizes filenames for many operating systems.
|
|
|
81 |
|
82 |
percentDone = int(progress) * 100. / file_size
|
83 |
done = int(50 * progress / int(file_size))
|
84 |
+
stdout.write("\r [%s%s][%3.2f%%] %s at %s/s\r " % ('=' * done, ' ' * (50-done), percentDone,
|
|
|
85 |
sizeof(file_size), sizeof(progress//(clock() - start))))
|
86 |
stdout.flush()
|
scripts/pytubectl
CHANGED
@@ -2,7 +2,9 @@
|
|
2 |
|
3 |
from pytube import YouTube
|
4 |
from pytube.utils import print_status
|
|
|
5 |
from pytube.exceptions import YouTubeError
|
|
|
6 |
|
7 |
import sys
|
8 |
import argparse
|
@@ -15,7 +17,7 @@ def _main():
|
|
15 |
help="The requested format of the video", dest="ext")
|
16 |
parser.add_argument("--resolution", "-r",
|
17 |
help="The requested resolution", dest="res")
|
18 |
-
parser.add_argument("--path", "-p",
|
19 |
help="The path to save the video to.", dest="path")
|
20 |
parser.add_argument("--filename", "-f",
|
21 |
dest="filename",
|
@@ -27,6 +29,8 @@ def _main():
|
|
27 |
yt = YouTube()
|
28 |
try:
|
29 |
yt.url = args.url
|
|
|
|
|
30 |
except YouTubeError:
|
31 |
print "Incorrect video URL."
|
32 |
sys.exit(1)
|
@@ -34,12 +38,19 @@ def _main():
|
|
34 |
if args.filename:
|
35 |
yt.filename = args.filename
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if args.ext and args.res:
|
38 |
# There's only ope video that matches both so get it
|
39 |
vid = yt.get(args.ext, args.res)
|
40 |
# Check if there's a video returned
|
41 |
if not vid:
|
42 |
-
print "
|
|
|
43 |
sys.exit(1)
|
44 |
|
45 |
elif args.ext:
|
|
|
2 |
|
3 |
from pytube import YouTube
|
4 |
from pytube.utils import print_status
|
5 |
+
from pytube.utils import FullPaths
|
6 |
from pytube.exceptions import YouTubeError
|
7 |
+
from pprint import pprint
|
8 |
|
9 |
import sys
|
10 |
import argparse
|
|
|
17 |
help="The requested format of the video", dest="ext")
|
18 |
parser.add_argument("--resolution", "-r",
|
19 |
help="The requested resolution", dest="res")
|
20 |
+
parser.add_argument("--path", "-p", action=FullPaths,
|
21 |
help="The path to save the video to.", dest="path")
|
22 |
parser.add_argument("--filename", "-f",
|
23 |
dest="filename",
|
|
|
29 |
yt = YouTube()
|
30 |
try:
|
31 |
yt.url = args.url
|
32 |
+
res_formts = yt.videos
|
33 |
+
res_formts = ["%s %s"% (str(res_formts[index].extension), str(res_formts[index].resolution)) for index, video in enumerate(res_formts)]
|
34 |
except YouTubeError:
|
35 |
print "Incorrect video URL."
|
36 |
sys.exit(1)
|
|
|
38 |
if args.filename:
|
39 |
yt.filename = args.filename
|
40 |
|
41 |
+
if args.ext or args.res:
|
42 |
+
if not all([args.ext, args.res]):
|
43 |
+
print "\nMake sure you give either of the below specified format/resolution combination.\n"
|
44 |
+
pprint(res_formts)
|
45 |
+
return
|
46 |
+
|
47 |
if args.ext and args.res:
|
48 |
# There's only ope video that matches both so get it
|
49 |
vid = yt.get(args.ext, args.res)
|
50 |
# Check if there's a video returned
|
51 |
if not vid:
|
52 |
+
print "\nThere's no video with the specified format/resolution combination.\n"
|
53 |
+
pprint(res_formts)
|
54 |
sys.exit(1)
|
55 |
|
56 |
elif args.ext:
|