From ad13f99525fcbccf67605420810fa255c62144ed Mon Sep 17 00:00:00 2001 From: Aditya Kumar Das Date: Thu, 22 Feb 2024 13:44:16 +0530 Subject: [PATCH] Fixed the AZLyrics URL and trimmed the lyrics Now it will adapt to dynamic changes in the URL parameters. Additionally, it will remove extra spaces from the beginning of the lyrics. --- lib/utils/service_utils.dart | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/utils/service_utils.dart b/lib/utils/service_utils.dart index 2ddfe7fb..84f38f6a 100644 --- a/lib/utils/service_utils.dart +++ b/lib/utils/service_utils.dart @@ -121,16 +121,35 @@ abstract class ServiceUtils { static Future getAZLyrics( {required String title, required List artists}) async { - //Couldn't figure out a way to generate value for x. Also, it remains the same across different IP addresses. - final suggestionUrl = Uri.parse( - "https://search.azlyrics.com/suggest.php?q=$title ${artists[0]}&x=884911ec808d4712b839f06754f62ef23cddd06a36e86bf8d44fbd2bac3e6a56"); - const Map headers = { HttpHeaders.userAgentHeader: - "Mozilla/5.0 (Linux i656 ; en-US) AppleWebKit/601.49 (KHTML, like Gecko) Chrome/51.0.1145.334 Safari/600" + "Mozilla/5.0 (Linux i656 ; en-US) AppleWebKit/601.49 (KHTML, like Gecko) Chrome/51.0.1145.334 Safari/600", }; - final searchResponse = await http.get(suggestionUrl, headers: headers); + //Will throw error 400 when you request the script with the host header + const Map headersForScript = { + HttpHeaders.userAgentHeader: + "Mozilla/5.0 (Linux i656 ; en-US) AppleWebKit/601.49 (KHTML, like Gecko) Chrome/51.0.1145.334 Safari/600", + HttpHeaders.hostHeader: "www.azlyrics.com", + }; + + final azLyricsGeoScript = await http.get( + Uri.parse("https://www.azlyrics.com/geo.js"), + headers: headersForScript); + + RegExp scriptValueRegex = RegExp(r'ep\.setAttribute\("value", "(.*)"\);'); + RegExp scriptNameRegex = RegExp(r'ep\.setAttribute\("name", "(.*)"\);'); + final String? v = + scriptValueRegex.firstMatch(azLyricsGeoScript.body)?.group(1); + final String? x = + scriptNameRegex.firstMatch(azLyricsGeoScript.body)?.group(1); + + debugPrint("getAZLyrics -> Additional URL params: $x=$v"); + + final suggestionUrl = Uri.parse( + "https://search.azlyrics.com/suggest.php?q=$title ${artists[0]}&${x.toString()}=${v.toString()}"); + + final searchResponse = await http.get(suggestionUrl, headers: headers); if (searchResponse.statusCode != 200) { throw "searchResponse = ${searchResponse.statusCode}"; } @@ -161,7 +180,7 @@ abstract class ServiceUtils { final String lyrics = lyricsDiv[4].text; - return lyrics; + return lyrics.trim(); } @Deprecated("In favor spotify lyrics api, this isn't needed anymore")