Skip to content

Commit

Permalink
fix: now pot applied to WebEmbedded client too (#47)
Browse files Browse the repository at this point in the history
* chore: apply embed to web embedded client (#1)

* chore: apply pot to WebEmbedded instead of Web

* fix: revert embed appliance

* fix: apply suggestion

* chore: revert change

* chore: apply suggestion

* chore: apply suggestion

* chore: non required

* chore: update pot & visitor data at rest

* chore: revert web client

* chore: revert

* chore: update client
  • Loading branch information
KagChi committed Sep 16, 2024
1 parent 5654235 commit 200ec45
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,64 @@
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;

public class WebEmbedded extends Web {
private static final Logger log = LoggerFactory.getLogger(WebEmbedded.class);

public static ClientConfig BASE_CONFIG = new ClientConfig()
.withApiKey("AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8")
.withClientName("WEB_EMBEDDED_PLAYER")
.withClientField("clientVersion", "1.20240806.01.00")
.withClientField("clientVersion", "1.20240902.00.00")
.withUserField("lockedSafetyMode", false);

public WebEmbedded() {
super(ClientOptions.DEFAULT);
}

public static void setPoTokenAndVisitorData(String poToken, String visitorData) {
WebEmbedded.poToken = poToken;

if (poToken == null || visitorData == null) {
BASE_CONFIG.getRoot().remove("serviceIntegrityDimensions");
BASE_CONFIG.withVisitorData(null);
return;
}

Map<String, Object> sid = BASE_CONFIG.putOnceAndJoin(BASE_CONFIG.getRoot(), "serviceIntegrityDimensions");
sid.put("poToken", poToken);
BASE_CONFIG.withVisitorData(visitorData);
}

@Override
public boolean isEmbedded() {
return true;
}

@Override
@NotNull
public URI transformPlaybackUri(@NotNull URI originalUri, @NotNull URI resolvedPlaybackUri) {
if (poToken == null) {
return resolvedPlaybackUri;
}

log.debug("Applying 'pot' parameter on playback URI: {}", resolvedPlaybackUri);
URIBuilder builder = new URIBuilder(resolvedPlaybackUri);
builder.addParameter("pot", poToken);

try {
return builder.build();
} catch (URISyntaxException e) {
log.debug("Failed to apply 'pot' parameter.", e);
return resolvedPlaybackUri;
}
}

public WebEmbedded(@NotNull ClientOptions options) {
super(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ default boolean supportsFormatLoading() {
return getOptions().getPlayback();
}


default boolean isEmbedded() {
return false;
}

void setPlaylistPageCount(int count);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ protected JsonBrowser loadTrackInfoFromInnertube(@NotNull YoutubeAudioSourceMana
// All other branches should've been caught by getPlayabilityStatus().
// An exception will be thrown if we can't handle it.
if (playabilityStatus == PlayabilityStatus.NON_EMBEDDABLE) {
if (isEmbedded()) {
throw new FriendlyException("Loading information for for video failed", Severity.COMMON,
new RuntimeException("Non-embeddable video cannot be loaded by embedded client"));
}

json = loadTrackInfoFromInnertube(source, httpInterface, videoId, status);
getPlayabilityStatus(json.get("playabilityStatus"), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import dev.lavalink.youtube.clients.ClientOptions;
import dev.lavalink.youtube.clients.Web;
import dev.lavalink.youtube.clients.WebEmbedded;
import dev.lavalink.youtube.clients.skeleton.Client;
import lavalink.server.config.RateLimitConfig;
import lavalink.server.config.ServerConfig;
Expand Down Expand Up @@ -169,7 +170,8 @@ public AudioPlayerManager configure(AudioPlayerManager audioPlayerManager) {
String visitorData = pot.getVisitorData();

if (token != null && visitorData != null) {
log.debug("Applying poToken and visitorData to WEB client (token: {}, vd: {})", token, visitorData);
log.debug("Applying poToken and visitorData to WEB & WEBEMBEDDED client (token: {}, vd: {})", token, visitorData);
WebEmbedded.setPoTokenAndVisitorData(token, visitorData);
Web.setPoTokenAndVisitorData(token, visitorData);
} else if (token != null || visitorData != null) {
log.warn("Both pot.token and pot.visitorData must be specified and valid for pot to apply.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
import dev.lavalink.youtube.YoutubeAudioSourceManager;
import dev.lavalink.youtube.clients.Web;
import dev.lavalink.youtube.clients.WebEmbedded;
import dev.lavalink.youtube.plugin.rest.MinimalConfigRequest;
import dev.lavalink.youtube.plugin.rest.MinimalConfigResponse;
import org.slf4j.Logger;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void updateYoutubeConfig(@RequestBody MinimalConfigRequest config) {
String visitorData = config.getVisitorData();

if (poToken == null || visitorData == null || (!poToken.isEmpty() && !visitorData.isEmpty())) {
WebEmbedded.setPoTokenAndVisitorData(poToken, visitorData);
Web.setPoTokenAndVisitorData(poToken, visitorData);
log.debug("Updated poToken to \"{}\" and visitorData to \"{}\"", poToken, visitorData);
}
Expand Down

0 comments on commit 200ec45

Please sign in to comment.