Skip to content

Commit

Permalink
Fix music list extraction
Browse files Browse the repository at this point in the history
Closes #160
  • Loading branch information
user234683 committed May 2, 2023
1 parent eca8650 commit 4032e0b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion youtube/templates/watch.html
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,11 @@ <h2 class="title">{{ title }}</h2>
{% for track in music_list %}
<tr>
{% for attribute in music_attributes %}
<td>{{ track.get(attribute.lower(), '') }}</td>
{% if attribute.lower() == 'title' and track['url'] is not none %}
<td><a href="{{ track['url'] }}">{{ track.get(attribute.lower(), '') }}</a></td>
{% else %}
<td>{{ track.get(attribute.lower(), '') }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
Expand Down
2 changes: 2 additions & 0 deletions youtube/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ def get_watch_page(video_id=None):
for item in info['related_videos']:
util.prefix_urls(item)
util.add_extra_html_info(item)
for song in info['music_list']:
song['url'] = util.prefix_url(song['url'])
if info['playlist']:
playlist_id = info['playlist']['id']
for item in info['playlist']['items']:
Expand Down
3 changes: 3 additions & 0 deletions youtube/yt_data_extract/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ def extract_item_info(item, additional_info={}):

if primary_type == 'video':
info['id'] = item.get('videoId')
if not info['id']:
info['id'] = deep_get(item,'navigationEndpoint', 'watchEndpoint',
'videoId')
info['view_count'] = extract_int(item.get('viewCountText'))

# dig into accessibility data to get view_count for videos marked as recommended, and to get time_published
Expand Down
29 changes: 29 additions & 0 deletions youtube/yt_data_extract/watch_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,34 @@ def _extract_metadata_row_info(renderer_content):

return info

def _extract_from_music_renderer(renderer_content):
# latest format for the music list
info = {
'music_list': [],
}

for carousel in renderer_content.get('carouselLockups', []):
song = {}
carousel = carousel.get('carouselLockupRenderer', {})
video_renderer = carousel.get('videoLockup', {})
video_renderer_info = extract_item_info(video_renderer)
video_id = video_renderer_info.get('id')
song['url'] = concat_or_none('https://www.youtube.com/watch?v=',
video_id)
song['title'] = video_renderer_info.get('title')
for row in carousel.get('infoRows', []):
row = row.get('infoRowRenderer', {})
title = extract_str(row.get('title'))
data = extract_str(row.get('defaultMetadata'))
if title == 'ARTIST':
song['artist'] = data
elif title == 'ALBUM':
song['album'] = data
elif title == 'WRITERS':
song['writers'] = data
info['music_list'].append(song)
return info

def _extract_from_video_metadata(renderer_content):
info = _extract_from_video_information_renderer(renderer_content)
liberal_dict_update(info, _extract_likes_dislikes(renderer_content))
Expand All @@ -254,6 +282,7 @@ def _extract_from_video_metadata(renderer_content):
'slimVideoActionBarRenderer': _extract_likes_dislikes,
'slimOwnerRenderer': _extract_from_owner_renderer,
'videoDescriptionHeaderRenderer': _extract_from_video_header_renderer,
'videoDescriptionMusicSectionRenderer': _extract_from_music_renderer,
'expandableVideoDescriptionRenderer': _extract_from_description_renderer,
'metadataRowContainerRenderer': _extract_metadata_row_info,
# OR just this one, which contains SOME of the above inside it
Expand Down

0 comments on commit 4032e0b

Please sign in to comment.