Skip to content

Commit

Permalink
Merge pull request #465 from dart-neitro/master
Browse files Browse the repository at this point in the history
Fixed the bug # 450
  • Loading branch information
nficano committed Sep 10, 2019
2 parents 7e0cd4e + 9be85cb commit 66145e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pytube/streams.py
Expand Up @@ -165,6 +165,27 @@ def filesize(self):
self._filesize = int(headers['content-length'])
return self._filesize

@property
def title(self):
"""Get title of video
:rtype: str
:returns:
Youtube video title
"""
player_config_args = self.player_config_args or {}

if 'title' in player_config_args:
return player_config_args['title']

details = self.player_config_args.get(
'player_response', {}).get('videoDetails', {})

if 'title' in details:
return details['title']

return 'Unknown YouTube Video Title'

@property
def default_filename(self):
"""Generate filename based on the video title.
Expand All @@ -173,8 +194,8 @@ def default_filename(self):
:returns:
An os file system compatible filename.
"""
title = self.player_config_args['title']
filename = safe_filename(title)

filename = safe_filename(self.title)
return '{filename}.{s.subtype}'.format(filename=filename, s=self)

def download(self, output_path=None, filename=None, filename_prefix=None):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_streams.py
Expand Up @@ -19,6 +19,21 @@ def test_default_filename(cipher_signature):
assert stream.default_filename == expected


def test_title(cipher_signature):
expected = 'PSY - GANGNAM STYLE(강남스타일) M/V'
stream = cipher_signature.streams.first()
assert stream.title == expected

expected = 'PSY - GANGNAM STYLE(강남스타일)'
stream.player_config_args = {
'player_response': {'videoDetails': {'title': expected}}}
assert stream.title == expected

expected = 'Unknown YouTube Video Title'
stream.player_config_args = {}
assert stream.title == expected


def test_download(cipher_signature, mocker):
mocker.patch.object(request, 'get')
request.get.side_effect = [
Expand Down

0 comments on commit 66145e5

Please sign in to comment.