getLyrics method

Future<JsonMap?> getLyrics(
  1. String browseId, {
  2. bool timestamps = false,
})

Returns lyrics of a song or video. When timestamps is set, lyrics are returned with timestamps, if available.

  • browseId Lyrics browseId obtained from WatchMixin.getWatchPlaylist (starts with MPLYt...).
  • timestamps Optional. Whether to return bare lyrics or lyrics with timestamps, if available. (Default: false).

Returns Map with song lyrics or null, if no lyrics were found.

The hasTimestamps-key determines the format of the data.

Example when timestamps = false, or no timestamps are available:

{
  'lyrics': "Today is gonna be the day\\nThat they're gonna throw it back to you\\n",
  'source': 'Source: LyricFind',
  'hasTimestamps': false
}

Example when timestamps = true and timestamps are available:

  'lyrics': [
    LyricLine('I was a liar', 9200, 10630, 1),
    LyricLine('I gave in to the fire', 10680, 12540, 2),
  ],
  'source': 'Source: LyricFind',
  'hasTimestamps': true,
}

Implementation

// {
///   'lyrics': [
///     LyricLine('I was a liar', 9200, 10630, 1),
///     LyricLine('I gave in to the fire', 10680, 12540, 2),
///   ],
///   'source': 'Source: LyricFind',
///   'hasTimestamps': true,
/// }
/// ```
Future<JsonMap?> getLyrics(String browseId, {bool timestamps = false}) async {
  if (browseId.isEmpty) {
    throw Exception(
      'Invalid browseId provided. This song might not have lyrics.',
    );
  }

  final response =
      timestamps
          ? await asMobile(
            () => sendRequest('browse', {'browseId': browseId}),
          )
          : await sendRequest('browse', {'browseId': browseId});

  if (timestamps) {
    final data =
        nav(response, TIMESTAMPED_LYRICS, nullIfAbsent: true) as JsonMap?;
    if (data == null || !data.containsKey('timedLyricsData')) return null;

    return TimedLyrics(
      (data['timedLyricsData'] as List)
          .map((line) => LyricLine.fromRaw(line as JsonMap))
          .toList(),
      data['sourceMessage'] as String?,
      true,
    ).toJson();
  } else {
    final lyricsStr = nav(response, [
      'contents',
      ...SECTION_LIST_ITEM,
      ...DESCRIPTION_SHELF,
      ...DESCRIPTION,
    ], nullIfAbsent: true);
    if (lyricsStr == null) return null;

    return Lyrics(
      lyricsStr as String,
      nav(response, [
            'contents',
            ...SECTION_LIST_ITEM,
            ...DESCRIPTION_SHELF,
            ...['description'],
            ...RUN_TEXT,
          ], nullIfAbsent: timestamps)
          as String?,
      false,
    ).toJson();
  }
}