rework get_song to use player endpoint, remove get_streaming_data (#196)

This commit is contained in:
sigma67 2021-05-18 15:59:43 +02:00
commit e1af1c2653
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
3 changed files with 12 additions and 142 deletions

View file

@ -27,7 +27,6 @@ Browsing
.. automethod:: YTMusic.get_user_playlists
.. automethod:: YTMusic.get_album
.. automethod:: YTMusic.get_song
.. automethod:: YTMusic.get_streaming_data
.. automethod:: YTMusic.get_lyrics
Watch

View file

@ -121,12 +121,10 @@ class TestYTMusic(unittest.TestCase):
self.assertEqual(len(results['tracks']), 7)
def test_get_song(self):
song = self.yt_auth.get_song("AjXQiKP5kMs") # private upload
self.assertEqual(len(song), 4)
song = self.yt.get_song(sample_video)
self.assertGreaterEqual(len(song), 16)
def test_get_streaming_data(self):
streaming_data = self.yt_auth.get_streaming_data(sample_video)
self.assertGreaterEqual(len(streaming_data), 3)
self.assertGreaterEqual(len(song['streamingData']['adaptiveFormats']), 10)
def test_get_lyrics(self):
playlist = self.yt.get_watch_playlist(sample_video)
@ -163,7 +161,7 @@ class TestYTMusic(unittest.TestCase):
###############
def test_get_library_playlists(self):
playlists = self.yt_brand.get_library_playlists(50)
playlists = self.yt_auth.get_library_playlists(50)
self.assertGreater(len(playlists), 25)
def test_get_library_songs(self):

View file

@ -1,5 +1,3 @@
import codecs
from urllib.parse import parse_qs
from ytmusicapi.helpers import *
from ytmusicapi.parsers.browsing import *
from ytmusicapi.parsers.albums import *
@ -597,139 +595,14 @@ class BrowsingMixin:
}
"""
endpoint = "https://www.youtube.com/get_video_info"
params = {"video_id": videoId, "hl": self.language, "el": "detailpage"}
response = self._send_get_request(endpoint, params)
text = parse_qs(response)
if 'player_response' not in text:
return text
player_response = json.loads(text['player_response'][0])
song_meta = player_response['videoDetails']
song_meta['category'] = player_response['microformat']['playerMicroformatRenderer'][
'category']
if song_meta['shortDescription'].endswith("Auto-generated by YouTube."):
try:
description = song_meta['shortDescription'].split('\n\n')
for i, detail in enumerate(description):
description[i] = codecs.escape_decode(detail)[0].decode('utf-8')
song_meta['provider'] = description[0].replace('Provided to YouTube by ', '')
song_meta['artists'] = [artist for artist in description[1].split(' · ')[1:]]
song_meta['copyright'] = description[3]
song_meta['release'] = None if len(description) < 5 else description[4].replace(
'Released on: ', '')
song_meta['production'] = None if len(description) < 6 else [
pub for pub in description[5].split('\n')
]
except (KeyError, IndexError):
pass
return song_meta
def get_streaming_data(self, videoId: str) -> Dict:
"""
Returns the streaming data for a song or video.
:param videoId: Video id
:return: Dictionary with song streaming data.
Example::
{
"expiresInSeconds": "21540",
"formats": [
{
"itag": 18,
"mimeType": "video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"",
"bitrate": 306477,
"width": 360,
"height": 360,
"lastModified": "1574970034520502",
"contentLength": "9913027",
"quality": "medium",
"fps": 25,
"qualityLabel": "360p",
"projectionType": "RECTANGULAR",
"averageBitrate": 306419,
"audioQuality": "AUDIO_QUALITY_LOW",
"approxDurationMs": "258809",
"audioSampleRate": "44100",
"audioChannels": 2,
"signatureCipher": "s=..."
}
],
"adaptiveFormats": [
{
"itag": 137,
"mimeType": "video/mp4; codecs=\"avc1.640020\"",
"bitrate": 312234,
"width": 1078,
"height": 1080,
"initRange": {
"start": "0",
"end": "738"
},
"indexRange": {
"start": "739",
"end": "1382"
},
"lastModified": "1574970033536914",
"contentLength": "5674377",
"quality": "hd1080",
"fps": 25,
"qualityLabel": "1080p",
"projectionType": "RECTANGULAR",
"averageBitrate": 175432,
"approxDurationMs": "258760",
"signatureCipher": "s=..."
},
{...},
{
"itag": 140,
"mimeType": "audio/mp4; codecs=\"mp4a.40.2\"",
"bitrate": 131205,
"initRange": {
"start": "0",
"end": "667"
},
"indexRange": {
"start": "668",
"end": "1011"
},
"lastModified": "1574969975805792",
"contentLength": "4189579",
"quality": "tiny",
"projectionType": "RECTANGULAR",
"averageBitrate": 129521,
"highReplication": true,
"audioQuality": "AUDIO_QUALITY_MEDIUM",
"approxDurationMs": "258773",
"audioSampleRate": "44100",
"audioChannels": 2,
"loudnessDb": 1.1422243,
"signatureCipher": "s=..."
},
{...}
]
}
"""
endpoint = "https://www.youtube.com/get_video_info"
params = {
"video_id": videoId,
"hl": self.language,
"el": "detailpage",
"c": "WEB_REMIX",
"cver": "0.1"
}
response = self._send_get_request(endpoint, params)
text = parse_qs(response)
if 'player_response' not in text:
return text
player_response = json.loads(text['player_response'][0])
if 'streamingData' not in player_response:
raise Exception('This video is not playable.')
return player_response['streamingData']
endpoint = 'player'
params = {"video_id": videoId}
response = self._send_request(endpoint, params)
keys = ['videoDetails', 'playabilityStatus', 'streamingData', 'microformat']
for k in list(response.keys()):
if k not in keys:
del response[k]
return response
def get_lyrics(self, browseId: str) -> Dict:
"""