Merge pull request #465 from dart-neitro/master
Browse files- pytube/streams.py +23 -2
- tests/test_streams.py +15 -0
pytube/streams.py
CHANGED
@@ -165,6 +165,27 @@ class Stream(object):
|
|
165 |
self._filesize = int(headers['content-length'])
|
166 |
return self._filesize
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
@property
|
169 |
def default_filename(self):
|
170 |
"""Generate filename based on the video title.
|
@@ -173,8 +194,8 @@ class Stream(object):
|
|
173 |
:returns:
|
174 |
An os file system compatible filename.
|
175 |
"""
|
176 |
-
|
177 |
-
filename = safe_filename(title)
|
178 |
return '{filename}.{s.subtype}'.format(filename=filename, s=self)
|
179 |
|
180 |
def download(self, output_path=None, filename=None, filename_prefix=None):
|
|
|
165 |
self._filesize = int(headers['content-length'])
|
166 |
return self._filesize
|
167 |
|
168 |
+
@property
|
169 |
+
def title(self):
|
170 |
+
"""Get title of video
|
171 |
+
|
172 |
+
:rtype: str
|
173 |
+
:returns:
|
174 |
+
Youtube video title
|
175 |
+
"""
|
176 |
+
player_config_args = self.player_config_args or {}
|
177 |
+
|
178 |
+
if 'title' in player_config_args:
|
179 |
+
return player_config_args['title']
|
180 |
+
|
181 |
+
details = self.player_config_args.get(
|
182 |
+
'player_response', {}).get('videoDetails', {})
|
183 |
+
|
184 |
+
if 'title' in details:
|
185 |
+
return details['title']
|
186 |
+
|
187 |
+
return 'Unknown YouTube Video Title'
|
188 |
+
|
189 |
@property
|
190 |
def default_filename(self):
|
191 |
"""Generate filename based on the video title.
|
|
|
194 |
:returns:
|
195 |
An os file system compatible filename.
|
196 |
"""
|
197 |
+
|
198 |
+
filename = safe_filename(self.title)
|
199 |
return '{filename}.{s.subtype}'.format(filename=filename, s=self)
|
200 |
|
201 |
def download(self, output_path=None, filename=None, filename_prefix=None):
|
tests/test_streams.py
CHANGED
@@ -19,6 +19,21 @@ def test_default_filename(cipher_signature):
|
|
19 |
assert stream.default_filename == expected
|
20 |
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def test_download(cipher_signature, mocker):
|
23 |
mocker.patch.object(request, 'get')
|
24 |
request.get.side_effect = [
|
|
|
19 |
assert stream.default_filename == expected
|
20 |
|
21 |
|
22 |
+
def test_title(cipher_signature):
|
23 |
+
expected = 'PSY - GANGNAM STYLE(강남스타일) M/V'
|
24 |
+
stream = cipher_signature.streams.first()
|
25 |
+
assert stream.title == expected
|
26 |
+
|
27 |
+
expected = 'PSY - GANGNAM STYLE(강남스타일)'
|
28 |
+
stream.player_config_args = {
|
29 |
+
'player_response': {'videoDetails': {'title': expected}}}
|
30 |
+
assert stream.title == expected
|
31 |
+
|
32 |
+
expected = 'Unknown YouTube Video Title'
|
33 |
+
stream.player_config_args = {}
|
34 |
+
assert stream.title == expected
|
35 |
+
|
36 |
+
|
37 |
def test_download(cipher_signature, mocker):
|
38 |
mocker.patch.object(request, 'get')
|
39 |
request.get.side_effect = [
|