diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubePlaylistLoader.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubePlaylistLoader.java index eb85108d1..60fa4b94f 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubePlaylistLoader.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubePlaylistLoader.java @@ -22,11 +22,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER; import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.COMMON; public class DefaultYoutubePlaylistLoader implements YoutubePlaylistLoader { - private static final Logger log = LoggerFactory.getLogger(DefaultYoutubePlaylistLoader.class); - private static final String REQUEST_URL = "https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"; private static final String REQUEST_PAYLOAD = "{\"context\":{\"client\":{\"clientName\":\"WEB\",\"clientVersion\":\"2.20210302.07.01\"}},\"continuation\":\"%s\"}"; private volatile int playlistPageCount = 6; @@ -40,7 +39,7 @@ public void setPlaylistPageCount(int playlistPageCount) { public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId, Function trackFactory) { - HttpGet request = new HttpGet(getPlaylistUrl(playlistId) + "&pbj=1&hl=en"); + HttpGet request = new HttpGet(getPlaylistUrl(playlistId) + PBJ_PARAMETER + "&hl=en"); try (CloseableHttpResponse response = httpInterface.execute(request)) { HttpClientTools.assertSuccessWithContent(response, "playlist response"); diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubeTrackDetailsLoader.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubeTrackDetailsLoader.java index 8a81bd4b2..75f859618 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubeTrackDetailsLoader.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/DefaultYoutubeTrackDetailsLoader.java @@ -18,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER; import static com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeTrackJsonData.fromEmbedParts; import static com.sedmelluq.discord.lavaplayer.tools.ExceptionTools.throwWithDebugInfo; import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.COMMON; @@ -173,7 +174,7 @@ protected String getUnplayableReason(JsonBrowser statusBlock) { } protected JsonBrowser loadTrackInfoFromMainPage(HttpInterface httpInterface, String videoId) throws IOException { - String url = "https://www.youtube.com/watch?v=" + videoId + "&pbj=1&hl=en"; + String url = "https://www.youtube.com/watch?v=" + videoId + PBJ_PARAMETER + "&hl=en"; try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) { HttpClientTools.assertSuccessWithContent(response, "video page response"); @@ -214,7 +215,7 @@ protected JsonBrowser loadTrackArgsFromVideoInfoPage(HttpInterface httpInterface String videoApiUrl = "https://youtube.googleapis.com/v/" + videoId; String encodedApiUrl = URLEncoder.encode(videoApiUrl, UTF_8.name()); String url = "https://www.youtube.com/get_video_info?video_id=" + videoId + "&eurl=" + encodedApiUrl + - "hl=en_GB&html5=1"; + "&hl=en_GB&html5=1&c=ANDROID&cver=16.24"; if (sts != null) { url += "&sts=" + sts; diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeHttpContextFilter.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeHttpContextFilter.java index 00f78d178..3322e0c1c 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeHttpContextFilter.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeHttpContextFilter.java @@ -12,6 +12,8 @@ import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.COMMON; public class YoutubeHttpContextFilter extends BaseYoutubeHttpContextFilter { + public static final String PBJ_PARAMETER = "&pbj=1"; + private static final String ATTRIBUTE_RESET_RETRY = "isResetRetry"; @Override @@ -38,15 +40,9 @@ public void onRequest(HttpClientContext context, HttpUriRequest request, boolean context.removeAttribute(ATTRIBUTE_RESET_RETRY); } - request.setHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/76.0.3809.100 Safari/537.36"); - request.setHeader("x-youtube-client-name", "1"); - request.setHeader("x-youtube-client-version", "2.20191008.04.01"); - request.setHeader("x-youtube-page-cl", "276511266"); - request.setHeader("x-youtube-page-label", "youtube.ytfe.desktop_20191024_3_RC0"); - request.setHeader("x-youtube-utc-offset", "0"); - request.setHeader("x-youtube-variants-checksum", "7a1198276cf2b23fc8321fac72aa876b"); - request.setHeader("accept-language", "en"); + if (isPbjRequest(request)) { + addPbjHeaders(request); + } super.onRequest(context, request, isRepetition); } @@ -72,4 +68,21 @@ public boolean onRequestException(HttpClientContext context, HttpUriRequest requ return false; } + + protected boolean isPbjRequest(HttpUriRequest request) { + String rawQuery = request.getURI().getRawQuery(); + return rawQuery != null && rawQuery.contains(PBJ_PARAMETER); + } + + protected void addPbjHeaders(HttpUriRequest request) { + request.setHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " + + "(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"); + request.setHeader("x-youtube-client-name", "1"); + request.setHeader("x-youtube-client-version", "2.20191008.04.01"); + request.setHeader("x-youtube-page-cl", "276511266"); + request.setHeader("x-youtube-page-label", "youtube.ytfe.desktop_20191024_3_RC0"); + request.setHeader("x-youtube-utc-offset", "0"); + request.setHeader("x-youtube-variants-checksum", "7a1198276cf2b23fc8321fac72aa876b"); + request.setHeader("accept-language", "en"); + } } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeMixProvider.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeMixProvider.java index 5d4baf3f0..6678b5ddb 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeMixProvider.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeMixProvider.java @@ -16,6 +16,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import static com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter.PBJ_PARAMETER; import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.SUSPICIOUS; /** @@ -38,7 +39,7 @@ public AudioPlaylist load( String playlistTitle = "YouTube mix"; List tracks = new ArrayList<>(); - String mixUrl = "https://www.youtube.com/watch?v=" + selectedVideoId + "&list=" + mixId + "&pbj=1"; + String mixUrl = "https://www.youtube.com/watch?v=" + selectedVideoId + "&list=" + mixId + PBJ_PARAMETER; try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(mixUrl))) { HttpClientTools.assertSuccessWithContent(response, "mix response");