getLyrics method

Future<String?> getLyrics(
  1. String videoId
)

Retrieves the lyrics of a song given its video ID.

Implementation

Future<String?> getLyrics(String videoId) async {
  if (!RegExp(r"^[a-zA-Z0-9-_]{11}$").hasMatch(videoId)) {
    throw Exception("Invalid videoId");
  }

  final data = await constructRequest("next", body: {"videoId": videoId});
  final browseId =
      traverse(traverseList(data, ["tabs", "tabRenderer"])[1], ["browseId"]);

  final lyricsData =
      await constructRequest("browse", body: {"browseId": browseId});
  final lyrics =
      traverseString(lyricsData, ["description", "runs", "text"])?.trim();

  return lyrics
      ?.replaceAll("\r", "")
      .split("\n")
      .where((element) => element.isNotEmpty)
      .join("\n");
}