@@ -24,6 +24,9 @@ public class GeniusClient implements AutoCloseable {
2424 private static final ExecutorService executor = Executors .newSingleThreadExecutor ();
2525 private final HttpClientProvider httpInterfaceManager ;
2626 private final String apiKey ;
27+ private static final String BROWSER_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0" ;
28+ private static final String PRELOAD_START = "window.__PRELOADED_STATE__ = JSON.parse('" ;
29+ private static final String PRELOAD_END = "');" ;
2730
2831 public GeniusClient (String apiKey , HttpClientProvider httpProvider ) {
2932 this .apiKey = apiKey ;
@@ -112,17 +115,40 @@ private GeniusData findGeniusData(String query) throws IOException {
112115 private String loadLyrics (String geniusUrl ) throws IOException {
113116 final var request = new HttpGet (geniusUrl );
114117
118+ request .setHeader ("user-agent" , BROWSER_USER_AGENT );
119+
115120 try (final var response = getHttpInterface ().execute (request )) {
116121 final String html = IOUtils .toString (response .getEntity ().getContent (), StandardCharsets .UTF_8 );
117- final var doc = Jsoup .parse (html );
118122
119- final var lyricsContainer = doc .select ("div[data-lyrics-container]" ).first ();
123+ // fucking kill me
124+ final var idx1 = html .indexOf (PRELOAD_START );
125+ final var split1 = html .substring (idx1 + PRELOAD_START .length ());
126+ final var idx2 = split1 .indexOf (PRELOAD_END );
127+ final var json = split1 .substring (0 , idx2 )
128+ .replace ("\\ \" " , "\" " )
129+ .replace ("\\ '" , "'" )
130+ .replace ("\\ \\ " , "\\ " );
131+
132+ final var lyrics = JsonBrowser .parse (json ).get ("songPage" ).get ("lyricsData" ).get ("body" ).get ("html" ).text ();
133+
134+ if (lyrics == null || lyrics .isEmpty ()) {
135+ final var doc = Jsoup .parse (html );
136+
137+ final var lyricsContainer = doc .select ("[data-lyrics-container]" ).first ();
138+
139+ if (lyricsContainer == null ) {
140+ throw new RuntimeException ("Could not find lyrics container, please report this to the developer" );
141+ }
120142
121- if (lyricsContainer == null ) {
122- throw new RuntimeException ("Could not find lyrics container, please report this to the developer" );
143+ return lyricsContainer .wholeText ()
144+ .replace ("<br><br>" , "\n " )
145+ .replace ("<br>" , "\n " )
146+ .replace ("\n \n \n " , "\n " )
147+ .trim ();
123148 }
124149
125- return lyricsContainer .wholeText ()
150+ return Jsoup .parse (lyrics )
151+ .wholeText ()
126152 .replace ("<br><br>" , "\n " )
127153 .replace ("<br>" , "\n " )
128154 .replace ("\n \n \n " , "\n " )
0 commit comments