Nick Ficano
commited on
Commit
·
ac64032
1
Parent(s):
978cd9c
commented, todos, removed kwargs from video
Browse files- pytube/models.py +39 -27
pytube/models.py
CHANGED
@@ -13,21 +13,38 @@ except ImportError:
|
|
13 |
class Video(object):
|
14 |
"""Class representation of a single instance of a YouTube video.
|
15 |
"""
|
16 |
-
def __init__(self, url, filename,
|
|
|
17 |
"""Sets-up the video object.
|
18 |
|
19 |
:param str url:
|
20 |
The url of the video. (e.g.: https://youtube.com/watch?v=...)
|
21 |
:param str filename:
|
22 |
The filename (minus the extention) to save the video.
|
23 |
-
:param
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
"""
|
27 |
self.url = url
|
28 |
self.filename = filename
|
29 |
-
|
30 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def download(self, path='', chunk_size=8 * 1024, on_progress=None,
|
33 |
on_finish=None, force_overwrite=False):
|
@@ -48,34 +65,35 @@ class Video(object):
|
|
48 |
:param bool force_overwrite:
|
49 |
Force a file overwrite if conflicting one exists.
|
50 |
"""
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
# TODO: Move this into cli, this kind of logic probably shouldn't be
|
58 |
# handled by the library.
|
59 |
-
if os.path.isfile(
|
60 |
raise OSError("Conflicting filename:'{}'".format(self.filename))
|
61 |
-
|
62 |
response = urlopen(self.url)
|
63 |
meta_data = dict(response.info().items())
|
64 |
file_size = int(meta_data.get("Content-Length") or
|
65 |
meta_data.get("content-length"))
|
66 |
self._bytes_received = 0
|
67 |
start = clock()
|
|
|
|
|
68 |
try:
|
69 |
-
with open(
|
70 |
while True:
|
71 |
self._buffer = response.read(chunk_size)
|
72 |
-
#
|
73 |
if not self._buffer:
|
74 |
if on_finish:
|
75 |
# TODO: We possibly want to flush the
|
76 |
# `_bytes_recieved`` buffer before we call
|
77 |
# ``on_finish()``.
|
78 |
-
on_finish(
|
79 |
break
|
80 |
|
81 |
self._bytes_received += len(self._buffer)
|
@@ -83,17 +101,11 @@ class Video(object):
|
|
83 |
if on_progress:
|
84 |
on_progress(self._bytes_received, file_size, start)
|
85 |
|
86 |
-
# Catch possible exceptions occurring during download.
|
87 |
-
except IOError:
|
88 |
-
raise IOError("Failed to open file.")
|
89 |
-
|
90 |
-
except BufferError:
|
91 |
-
raise BufferError("Failed to write video to file.")
|
92 |
-
|
93 |
except KeyboardInterrupt:
|
94 |
# TODO: Move this into the cli, ``KeyboardInterrupt`` handling
|
95 |
-
# should be taken care of by the client.
|
96 |
-
|
|
|
97 |
raise KeyboardInterrupt("Interrupt signal given. Deleting "
|
98 |
"incomplete video.")
|
99 |
|
@@ -106,7 +118,7 @@ class Video(object):
|
|
106 |
"""The "less than" (lt) method is used for comparing video object to
|
107 |
one another. This useful when sorting.
|
108 |
|
109 |
-
:param
|
110 |
The instance of the other video instance for comparison.
|
111 |
"""
|
112 |
if isinstance(other, Video):
|
|
|
13 |
class Video(object):
|
14 |
"""Class representation of a single instance of a YouTube video.
|
15 |
"""
|
16 |
+
def __init__(self, url, filename, extension, resolution, video_codec,
|
17 |
+
profile, video_bitrate, audio_codec, audio_bitrate):
|
18 |
"""Sets-up the video object.
|
19 |
|
20 |
:param str url:
|
21 |
The url of the video. (e.g.: https://youtube.com/watch?v=...)
|
22 |
:param str filename:
|
23 |
The filename (minus the extention) to save the video.
|
24 |
+
:param str extention:
|
25 |
+
The desired file extention (e.g.: mp4, flv, webm).
|
26 |
+
:param str resolution:
|
27 |
+
The broadcasting standard (e.g.: 720p, 1080p).
|
28 |
+
:param str video_codec:
|
29 |
+
The codec used to encode the video.
|
30 |
+
:param str profile:
|
31 |
+
The arbitrary quality profile.
|
32 |
+
:param str video_bitrate:
|
33 |
+
The bitrate of the video over sampling interval.
|
34 |
+
:param str audio_codec:
|
35 |
+
The codec used to encode the audio.
|
36 |
+
:param str audio_bitrate:
|
37 |
+
The bitrate of the video's audio over sampling interval.
|
38 |
"""
|
39 |
self.url = url
|
40 |
self.filename = filename
|
41 |
+
self.extension = extension
|
42 |
+
self.resolution = resolution
|
43 |
+
self.video_codec = video_codec
|
44 |
+
self.profile = profile
|
45 |
+
self.video_bitrate = video_bitrate
|
46 |
+
self.audio_codec = audio_codec
|
47 |
+
self.audio_bitrate = audio_bitrate
|
48 |
|
49 |
def download(self, path='', chunk_size=8 * 1024, on_progress=None,
|
50 |
on_finish=None, force_overwrite=False):
|
|
|
65 |
:param bool force_overwrite:
|
66 |
Force a file overwrite if conflicting one exists.
|
67 |
"""
|
68 |
+
path = os.path.normpath(path)
|
69 |
+
if os.path.isdir(path):
|
70 |
+
filename = "{}.{}".format(self.filename, self.extension)
|
71 |
+
path = os.path.join(path, filename)
|
72 |
+
# TODO: If it's not a path, this should raise an ``OSError``.
|
|
|
73 |
# TODO: Move this into cli, this kind of logic probably shouldn't be
|
74 |
# handled by the library.
|
75 |
+
if os.path.isfile(path) and not force_overwrite:
|
76 |
raise OSError("Conflicting filename:'{}'".format(self.filename))
|
77 |
+
# TODO: Split up the downloading and OS jazz into separate functions.
|
78 |
response = urlopen(self.url)
|
79 |
meta_data = dict(response.info().items())
|
80 |
file_size = int(meta_data.get("Content-Length") or
|
81 |
meta_data.get("content-length"))
|
82 |
self._bytes_received = 0
|
83 |
start = clock()
|
84 |
+
# TODO: Let's get rid of this whole try/except block, let ``OSErrors``
|
85 |
+
# fail loudly.
|
86 |
try:
|
87 |
+
with open(path, 'wb') as dst_file:
|
88 |
while True:
|
89 |
self._buffer = response.read(chunk_size)
|
90 |
+
# Check if the buffer is empty (aka no bytes remaining).
|
91 |
if not self._buffer:
|
92 |
if on_finish:
|
93 |
# TODO: We possibly want to flush the
|
94 |
# `_bytes_recieved`` buffer before we call
|
95 |
# ``on_finish()``.
|
96 |
+
on_finish(path)
|
97 |
break
|
98 |
|
99 |
self._bytes_received += len(self._buffer)
|
|
|
101 |
if on_progress:
|
102 |
on_progress(self._bytes_received, file_size, start)
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
except KeyboardInterrupt:
|
105 |
# TODO: Move this into the cli, ``KeyboardInterrupt`` handling
|
106 |
+
# should be taken care of by the client. Also you should be allowed
|
107 |
+
# to disable this.
|
108 |
+
os.remove(path)
|
109 |
raise KeyboardInterrupt("Interrupt signal given. Deleting "
|
110 |
"incomplete video.")
|
111 |
|
|
|
118 |
"""The "less than" (lt) method is used for comparing video object to
|
119 |
one another. This useful when sorting.
|
120 |
|
121 |
+
:param other:
|
122 |
The instance of the other video instance for comparison.
|
123 |
"""
|
124 |
if isinstance(other, Video):
|