Skip to content

Commit

Permalink
Fixed YT age-restricted videos
Browse files Browse the repository at this point in the history
  • Loading branch information
sedmelluq committed Jun 28, 2021
1 parent e8bf67a commit 888024a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +39,7 @@ public void setPlaylistPageCount(int playlistPageCount) {
public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId,
Function<AudioTrackInfo, AudioTrack> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -38,7 +39,7 @@ public AudioPlaylist load(
String playlistTitle = "YouTube mix";
List<AudioTrack> 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");
Expand Down

0 comments on commit 888024a

Please sign in to comment.