Udio: Fix track loading when lyrics are present in the JSON

This commit is contained in:
anthorob committed 2024-05-10 06:53:15 -04:00
1 parent 20d362978c
commit 8742ecc096
3 files changed
+30 -32

No files matched your search

@@ -31,7 +31,6 @@ public class SourceFinder {
private static final Pattern udioPattern = Pattern.compile("^https://www.udio.com/songs/[0-9A-Za-z]+$", Pattern.CASE_INSENSITIVE); private static final Pattern udioPattern = Pattern.compile("^https://www.udio.com/songs/[0-9A-Za-z]+$", Pattern.CASE_INSENSITIVE);
private static final Pattern mp3Pattern = Pattern.compile("^https?://(?:.*)(?:.mp3)$", Pattern.CASE_INSENSITIVE); private static final Pattern mp3Pattern = Pattern.compile("^https?://(?:.*)(?:.mp3)$", Pattern.CASE_INSENSITIVE);
public static AudioSourceType getSource(String url) { public static AudioSourceType getSource(String url) {
Matcher youtubeMatcher = youtubePattern.matcher(url); Matcher youtubeMatcher = youtubePattern.matcher(url);
Matcher youtubeMusicMatcher = youtubeMusicPattern.matcher(url); Matcher youtubeMusicMatcher = youtubeMusicPattern.matcher(url);
@@ -5,6 +5,8 @@ package netneutrality.music.source;/*
* https://discord.gg/CdFcyQ * https://discord.gg/CdFcyQ
*/ */
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.io.JsonEOFException;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager; import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools; import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
@@ -25,6 +27,8 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.DataInput; import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
@@ -38,6 +42,9 @@ import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.
public class UdioAudioSourceManager implements AudioSourceManager, HttpConfigurable { public class UdioAudioSourceManager implements AudioSourceManager, HttpConfigurable {
private static final Logger log = LoggerFactory.getLogger(UdioAudioSourceManager.class);
private static final String TRACK_URL_REGEX = "^https://www.udio.com/songs/[0-9A-Za-z]+$"; private static final String TRACK_URL_REGEX = "^https://www.udio.com/songs/[0-9A-Za-z]+$";
private static final Pattern trackUrlPattern = Pattern.compile(TRACK_URL_REGEX); private static final Pattern trackUrlPattern = Pattern.compile(TRACK_URL_REGEX);
@@ -58,9 +65,6 @@ public class UdioAudioSourceManager implements AudioSourceManager, HttpConfigura
@Override @Override
public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) { public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) {
// start = "og:audio:url\",\"content\":\"
// end = \"}],
if (!trackUrlPattern.matcher(reference.identifier).matches()) { if (!trackUrlPattern.matcher(reference.identifier).matches()) {
return null; return null;
} }
@@ -90,17 +94,10 @@ public class UdioAudioSourceManager implements AudioSourceManager, HttpConfigura
} }
private AudioTrack loadTrackFromPageContent(String trackUrl, String content) throws IOException { private AudioTrack loadTrackFromPageContent(String trackUrl, String content) throws IOException {
JsonBrowser config = loadConfigJsonFromPageContent(content); JsonBrowser trackInfo = loadTrackInfoFromPageContent(content);
if (config == null) { if (trackInfo == null) {
throw new FriendlyException("Track information not found on the page.", SUSPICIOUS, null); throw new FriendlyException("Udio: Failed to extract track information from web page.", SUSPICIOUS, null);
}
JsonBrowser trackInfo;
try {
trackInfo = config.index(3).get("children").index(3).get("children").index(3).get("track");
} catch (NullPointerException e) {
throw new FriendlyException("Track information not found on the page.", SUSPICIOUS, null);
} }
return new UdioAudioTrack(new AudioTrackInfo( return new UdioAudioTrack(new AudioTrackInfo(
@@ -113,15 +110,24 @@ public class UdioAudioSourceManager implements AudioSourceManager, HttpConfigura
), this); ), this);
} }
JsonBrowser loadConfigJsonFromPageContent(String content) throws IOException { JsonBrowser loadTrackInfoFromPageContent(String content) throws IOException {
String configText = DataFormatTools.extractBetween(content, "self.__next_f.push([1,\"8:", "\\n"); // start = self.__next_f.push([1,"8:
System.out.println(configText); // end = \n"])
if (configText != null) { String json = DataFormatTools.extractBetween(content, "self.__next_f.push([1,\"8:", "\\n\"])");
configText = configText.replace("\\", "");
System.out.println(configText); if (json != null) {
json = json.replace("\\", "");
try {
JsonBrowser parsedJson = JsonBrowser.parse(json);
return parsedJson.index(3).get("children").index(3).get("children").index(3).get("track");
} catch (JsonParseException e) {
log.debug("Udio HTML: {}", content);
return null;
}
return JsonBrowser.parse(configText);
} }
return null; return null;
@@ -59,17 +59,10 @@ public class UdioAudioTrack extends DelegatedAudioTrack {
} }
private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException { private String loadPlaybackUrl(HttpInterface httpInterface) throws IOException {
JsonBrowser config = loadPlayerConfig(httpInterface); JsonBrowser trackInfo = loadPlayerConfig(httpInterface);
if (config == null) { if (trackInfo == null) {
throw new FriendlyException("Track information not present on the page.", SUSPICIOUS, null); throw new FriendlyException("Udio: Failed to extract track information from web page.", SUSPICIOUS, null);
}
JsonBrowser trackInfo;
try {
trackInfo = config.index(3).get("children").index(3).get("children").index(3).get("track");
} catch (NullPointerException e) {
throw new FriendlyException("Track information not found on the page.", SUSPICIOUS, null);
} }
return trackInfo.get("song_path").text(); return trackInfo.get("song_path").text();
@@ -84,7 +77,7 @@ public class UdioAudioTrack extends DelegatedAudioTrack {
new IllegalStateException("Response code for player config is " + statusCode)); new IllegalStateException("Response code for player config is " + statusCode));
} }
return sourceManager.loadConfigJsonFromPageContent(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); return sourceManager.loadTrackInfoFromPageContent(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
} }
} }
} }