From e3e7c22e620d1fdb445af3646dcaad940ca78f67 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Das Date: Sun, 25 Feb 2024 01:50:09 +0530 Subject: [PATCH] Added method to fetch genius lyrics url This way, users don't require genius API access tokens. --- lib/utils/service_utils.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/utils/service_utils.dart b/lib/utils/service_utils.dart index 04a58f9e..43c2a7f9 100644 --- a/lib/utils/service_utils.dart +++ b/lib/utils/service_utils.dart @@ -120,6 +120,34 @@ abstract class ServiceUtils { return results; } + static Future getGeniusLyrics( + {required String title, required List artists}) async { + //Requires a non-blacklisted, valid User Agent. Or else, cloudflare might throw a 403. + Map headers = { + HttpHeaders.userAgentHeader: + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.4", + }; + + final searchResultResponse = await http.get( + Uri.parse( + "https://genius.com/api/search/multi?q=${title.replaceAll(RegExp(r"(\(.*\))"), "")} ${artists[0]}"), + headers: headers); + final searchResultObj = jsonDecode(searchResultResponse.body); + String topResultPath; + try { + topResultPath = searchResultObj["response"]["sections"][0]["hits"][0] + ["result"]["path"] as String; + logger.t("topResultPath: $topResultPath"); + } catch (e) { + logger.e(e); + throw "topResultPath not found!"; + } + final lyrics = + await extractLyrics(Uri.parse("https://genius.com$topResultPath")); + + return lyrics?.trim(); + } + static Future getAZLyrics( {required String title, required List artists}) async { const Map headers = {