parsePlaylistHeaderMeta function
- JsonMap header
Parses header meta from playlist.
Implementation
JsonMap parsePlaylistHeaderMeta(JsonMap header) {
final playlistMeta = <String, dynamic>{
'views': null,
'duration': null,
'trackCount': null,
'title':
((header['title'] as JsonMap?)?['runs'] as List? ?? [])
.map((run) => (run as JsonMap)['text'])
.join(),
'thumbnails': nav(header, THUMBNAILS),
};
if (header.containsKey('facepile')) {
final avatarRenderer = nav(header, [
'facepile',
'avatarStackViewModel',
'rendererContext',
]);
final avatarCommand = nav(avatarRenderer, [
'commandContext',
'onTap',
'innertubeCommand',
], nullIfAbsent: true);
if (nav(avatarCommand, [
'showEngagementPanelEndpoint',
'identifier',
'tag',
], nullIfAbsent: true) ==
'PAplaylist_collaborate') {
final avatars = List<JsonMap>.from(
nav(header, ['facepile', 'avatarStackViewModel', 'avatars']) as List,
);
playlistMeta['collaborators'] = {
'text': nav(avatarRenderer, ['accessibilityContext', 'label']),
'avatars': [
for (final avatar in avatars)
(((avatar['avatarViewModel'] as JsonMap)['image']
as JsonMap)['sources']
as List)[0],
],
};
} else {
playlistMeta['author'] = {
'name': nav(header, [
'facepile',
'avatarStackViewModel',
'text',
'content',
]),
'id': nav(avatarCommand, [
'browseEndpoint',
'browseId',
], nullIfAbsent: true),
};
}
}
final secondSubtitleRuns =
nav(header, ['secondSubtitle', 'runs'], nullIfAbsent: true) as List?;
if (secondSubtitleRuns != null) {
final hasViews = secondSubtitleRuns.length > 3 ? 2 : 0;
playlistMeta['views'] =
hasViews == 0
? null
: toInt((secondSubtitleRuns[0] as JsonMap)['text'] as String);
final hasDuration = secondSubtitleRuns.length > 1 ? 2 : 0;
playlistMeta['duration'] =
hasDuration == 0
? null
: (secondSubtitleRuns[hasViews + hasDuration] as JsonMap)['text'];
final songCountText =
(secondSubtitleRuns[hasViews] as JsonMap)['text'] as String;
final songCountSearch =
RegExp(
r'\d+',
).allMatches(songCountText).map((match) => match.group(0)!).toList();
playlistMeta['trackCount'] =
songCountSearch.isNotEmpty ? toInt(songCountSearch.join()) : null;
}
return playlistMeta;
}