-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lyrics are now fetched using the JLyrics library
- Loading branch information
Showing
5 changed files
with
53 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 0 additions & 86 deletions
86
src/main/java/spotify/playback/data/lyrics/GeniusLyrics.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/main/java/spotify/playback/data/lyrics/GeniusLyricsScraper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package spotify.playback.data.lyrics; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.jagrosh.jlyrics.Lyrics; | ||
import com.jagrosh.jlyrics.LyricsClient; | ||
|
||
@Service | ||
public class GeniusLyricsScraper { | ||
private final LyricsClient lyricsClient; | ||
|
||
GeniusLyricsScraper() { | ||
this.lyricsClient = new LyricsClient("Genius"); | ||
} | ||
|
||
public String getSongLyrics(String artistName, String songName) { | ||
try { | ||
Lyrics lyrics = lyricsClient.getLyrics(artistName + " " + songName).join(); | ||
if (lyrics != null && lyrics.getContent() != null && !lyrics.getContent().isBlank()) { | ||
return insertLineBreaksEveryFourLines(lyrics.getContent()); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
public static String insertLineBreaksEveryFourLines(String input) { | ||
StringBuilder result = new StringBuilder(); | ||
List<String> lines = Arrays.stream(input.split("\n")) | ||
.map(String::strip) | ||
.map(line -> line.replace(" ", "")) | ||
.filter(line -> !line.isBlank() && !line.startsWith("[") && !line.startsWith("]")) | ||
.collect(Collectors.toList()); | ||
for (int i = 0; i < lines.size(); i++) { | ||
String line = lines.get(i); | ||
result.append(line).append("\n"); | ||
if ((i + 1) % 4 == 0 && (i + 1) < lines.size()) { | ||
result.append("\n"); | ||
} | ||
} | ||
return result.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters