Skip to content

Commit

Permalink
Merge pull request #2 from SMANahian/add-source
Browse files Browse the repository at this point in the history
Added another bigger source of lyrics for different language songs
  • Loading branch information
LabyStudio committed Feb 3, 2024
2 parents b14fef2 + c4be67c commit cc47abe
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Expand Up @@ -4,6 +4,7 @@
import de.labystudio.desktopmodules.spotify.api.lyrics.source.LyricsSource;
import de.labystudio.desktopmodules.spotify.api.lyrics.source.crintsoft.CrintSoftSource;
import de.labystudio.desktopmodules.spotify.api.lyrics.source.music163.Music163Source;
import de.labystudio.desktopmodules.spotify.api.lyrics.source.lrclib.LrcLib;
import de.labystudio.spotifyapi.model.Track;

import java.util.ArrayList;
Expand All @@ -30,6 +31,7 @@ public class LyricsProvider {
* Adds all sources
*/
public LyricsProvider() {
this.sources.add(new LrcLib());
this.sources.add(new Music163Source());
this.sources.add(new CrintSoftSource());
}
Expand Down
@@ -0,0 +1,78 @@
package de.labystudio.desktopmodules.spotify.api.lyrics.source.lrclib;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

import de.labystudio.desktopmodules.spotify.api.lyrics.reader.Lyrics;
import de.labystudio.desktopmodules.spotify.api.lyrics.reader.LyricsReader;
import de.labystudio.desktopmodules.spotify.api.lyrics.source.LyricsSource;
import de.labystudio.desktopmodules.spotify.api.lyrics.source.lrclib.model.Song;
import de.labystudio.spotifyapi.model.Track;

/**
* Lrclib lyrics database
* https://lrclib.net/api/
*
* @author *Unknown*
*/
public class LrcLib extends LyricsSource {

private static final String API_ROOT = "https://lrclib.net/api/";
private static final String API_SEARCH = API_ROOT + "search";


@Override
public Lyrics get(Track track) throws Exception {

URL url = new URL(API_SEARCH + "?track_name=" + track.getName().replace(" ", "+") + "&artist_name=" + track.getArtist().replace(" ", "+") + "&duration=" + track.getLength()/1000);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();

//Getting the response code
int responsecode = conn.getResponseCode();

if (responsecode != 200) {
return null;
} else {
String inline = "";
// use utf-8 for the stream
Scanner scanner = new Scanner(url.openStream(), "UTF-8");

while (scanner.hasNext()) {
inline += scanner.nextLine();
}

//Close the scanner
scanner.close();



Song[] songs = GSON.fromJson(inline, Song[].class);


for(Song song : songs) {
if( song.name.equalsIgnoreCase(track.getName()) && song.artistName.equalsIgnoreCase(track.getArtist()) ) {
if( (song.duration - track.getLength()/1000) <= 1 ) {
return new LyricsReader(song.syncedLyrics).readLyrics();
}
}
}

return null;

}
}


protected String getReferer() {
return "https://lrclib.net/api/";
}

protected String getUserAgent() {
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36";
}

}
@@ -0,0 +1,12 @@
package de.labystudio.desktopmodules.spotify.api.lyrics.source.lrclib.model;

public class Song {

public int id;
public String name;
public String artistName;
public String albumName;
public int duration;
public String syncedLyrics;

}

0 comments on commit cc47abe

Please sign in to comment.