Skip to content

Commit

Permalink
Manually implement ip rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Mar 5, 2024
1 parent c8051cf commit f5c8527
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 19 deletions.
2 changes: 2 additions & 0 deletions application/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ tasks {
dependencies {
// LL holds all our versions
compileOnly(libs.lavalink.api)
compileOnly(libs.lavalink.server)
compileOnly(libs.lavaplayer)
compileOnly(libs.lavaplayer.rotator)
compileOnly(libs.http)
compileOnly(libs.jackson.annotations)
compileOnly(libs.jsoup)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
package me.duncte123.lyrics;

import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.ExceptionTools;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterfaceManager;
import com.sedmelluq.lava.extensions.youtuberotator.YoutubeIpRotatorSetup;
import com.sedmelluq.lava.extensions.youtuberotator.planner.*;
import lavalink.server.config.RateLimitConfig;
import lavalink.server.config.ServerConfig;

import java.util.Optional;

public class HttpClientProvider implements AutoCloseable {
private final HttpInterfaceManager httpInterfaceManager;

public HttpClientProvider(AudioPlayerManager playerManager) {
final YoutubeAudioSourceManager sourceManager = playerManager.source(YoutubeAudioSourceManager.class);
public HttpClientProvider(AbstractRoutePlanner routePlanner, ServerConfig serverConfig) {
this.httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();

if (routePlanner != null) {
final int retryLimit = Optional.ofNullable(serverConfig.getRatelimit())
.map(RateLimitConfig::getRetryLimit)
.orElse(-1);

final YoutubeIpRotatorSetup rotator;

if (retryLimit < 0) {
rotator = new YoutubeIpRotatorSetup(routePlanner);
} else if (retryLimit == 0) {
rotator = new YoutubeIpRotatorSetup(routePlanner).withRetryLimit(Integer.MAX_VALUE);
} else {
rotator = new YoutubeIpRotatorSetup(routePlanner).withRetryLimit(retryLimit);
}

if (sourceManager != null) {
this.httpInterfaceManager = (HttpInterfaceManager) sourceManager.getMainHttpConfiguration();
} else {
this.httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();
rotator.forConfiguration(this.httpInterfaceManager, false).setup();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public Future<List<SearchTrack>> search(String query, String region) {

final var result = request(SEARCH_URL, new SearchRequest(
Context.DEFAULT_MOBILE_REQUEST_WITH_REGION.apply(region),
query
query,
ONLY_TRACKS_SEARCH_PARAMS
));

// /contents/tabbedSearchResultsRenderer/tabs/0/tabRenderer/content/sectionListRenderer/contents/1/musicCardShelfRenderer/title/runs/0/navigationEndpoint/watchEndpoint/videoId
Expand All @@ -132,7 +133,8 @@ public Future<List<SearchTrack>> search(String query, String region) {
.get("sectionListRenderer")
.get("contents");

contents.values()
// Top result, seems to be empty with the new param
/*contents.values()
.stream()
.filter((it) -> !it.get("musicCardShelfRenderer").isNull())
.findFirst()
Expand All @@ -151,7 +153,7 @@ public Future<List<SearchTrack>> search(String query, String region) {
if (title != null && videoId != null) {
resList.add(new SearchTrack(videoId, title));
}
});
});*/

contents.values()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static me.duncte123.lyrics.utils.JsonUtils.getRunningText;

public final class YouTubeUtils {
public static final String ONLY_TRACKS_SEARCH_PARAMS = "EgWKAQIIAWoQEAMQBBAJEAoQBRAREBAQFQ%3D%3D";

@Nullable
public static String getBrowseEndpoint(JsonBrowser browser) {
return browser.get("contents")
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ dependencies {
compileOnly(libs.jackson.annotations)
compileOnly(libs.lavalink.server)
compileOnly(libs.lavaplayer)
compileOnly(libs.lavaplayer.rotator)

implementation(projects.protocol)
implementation(projects.application)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
lavalink-api = "4.0.0"
lavalink-server = "4.0.0"
lavalyrics = "0932f77"
lavaplayer = "2.1.0"

[libraries]
# No versions? That is correct, lavalink server has the version for lavaplayer.
Expand All @@ -10,6 +11,7 @@ http = { group = "org.apache.httpcomponents", name = "httpclient" }
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind" }
jackson-annotations = { group = "com.fasterxml.jackson.core", name = "jackson-annotations" }
lavaplayer = { group = "dev.arbjerg", name = "lavaplayer" }
lavaplayer-rotator = { group = "dev.arbjerg", name = "lavaplayer-ext-youtube-rotator", version.ref = "lavaplayer" }
commons-io = { group = "commons-io", name = "commons-io", version = "2.13.0" }
jsoup = { group = "org.jsoup", name = "jsoup", version = "1.16.1" }
lavalink-server = { group = "dev.arbjerg.lavalink", name = "Lavalink-Server", version.ref = "lavalink-server" }
Expand Down
3 changes: 3 additions & 0 deletions lavalyrics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tasks {
dependencies {
// LL holds all our versions
compileOnly(libs.lavalink.api)
compileOnly(libs.lavalink.server)
compileOnly(libs.lavaplayer)
compileOnly(libs.lavaplayer.rotator)

compileOnly(libs.lavalyrics)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.lava.extensions.youtuberotator.planner.AbstractRoutePlanner;
import lavalink.server.config.ServerConfig;
import me.duncte123.lyrics.GeniusClient;
import me.duncte123.lyrics.HttpClientProvider;
import me.duncte123.lyrics.LyricsClient;
Expand All @@ -28,8 +30,8 @@ public class JavaAudioLyricsManager implements AudioLyricsManager {
private final GeniusClient geniusClient;
private final HttpClientProvider httpProvider;

public JavaAudioLyricsManager(Config config, AudioPlayerManager audioPlayerManager) {
this.httpProvider = new HttpClientProvider(audioPlayerManager);
public JavaAudioLyricsManager(Config config, AbstractRoutePlanner routePlanner, ServerConfig serverConfig) {
this.httpProvider = new HttpClientProvider(routePlanner, serverConfig);
final String geniusApiKey = config.getGeniusApiKey();

this.youtubeClient = new LyricsClient(this.httpProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.github.topi314.lavalyrics.LyricsManager;
import com.github.topi314.lavalyrics.api.LyricsManagerConfiguration;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.lava.extensions.youtuberotator.planner.AbstractRoutePlanner;
import lavalink.server.config.ServerConfig;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

@Service
public class JavaLyricsManagerConfiguration implements LyricsManagerConfiguration {
private final JavaAudioLyricsManager javaAudioLyricsManager;

public JavaLyricsManagerConfiguration(Config config, AudioPlayerManager audioPlayerManager) {
this.javaAudioLyricsManager = new JavaAudioLyricsManager(config, audioPlayerManager);
public JavaLyricsManagerConfiguration(Config config, AbstractRoutePlanner routePlanner, ServerConfig serverConfig) {
this.javaAudioLyricsManager = new JavaAudioLyricsManager(config, routePlanner, serverConfig);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import me.duncte123.lyrics.model.Context;

public record SearchRequest(Context context, String query) {
public record SearchRequest(Context context, String query, String params) {
}
6 changes: 4 additions & 2 deletions src/main/java/me/duncte123/lyrics/lavalink/RestHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.duncte123.lyrics.lavalink;

import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.lava.extensions.youtuberotator.planner.AbstractRoutePlanner;
import lavalink.server.config.ServerConfig;
import lavalink.server.io.SocketServer;
import me.duncte123.lyrics.GeniusClient;
import me.duncte123.lyrics.HttpClientProvider;
Expand All @@ -26,11 +28,11 @@ public class RestHandler {
private final SocketServer socketServer;
private final Config config;

public RestHandler(SocketServer socketServer, Config config, AudioPlayerManager audioPlayerManager) {
public RestHandler(SocketServer socketServer, Config config, AbstractRoutePlanner routePlanner, ServerConfig serverConfig) {
this.socketServer = socketServer;
this.config = config;

final HttpClientProvider httpProvider = new HttpClientProvider(audioPlayerManager);
final HttpClientProvider httpProvider = new HttpClientProvider(routePlanner, serverConfig);

this.ytClient = new LyricsClient(httpProvider);

Expand Down

0 comments on commit f5c8527

Please sign in to comment.