Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1-legacy] Use WEB client, add 403 retry (Fix "Video returned by Youtube is not what was requested") #87

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ protected JsonBrowser loadTrackInfoFromInnertube(
clientConfig = YoutubeClientConfig.WEB.copy();
} else if (infoStatus == InfoStatus.NON_EMBEDDABLE) {
// Used when age restriction bypass failed, if we have valid auth then most likely this request will be successful
clientConfig = YoutubeClientConfig.ANDROID.copy()
clientConfig = YoutubeClientConfig.WEB.copy()
.withRootField("params", PLAYER_PARAMS);
} else if (infoStatus == InfoStatus.REQUIRES_LOGIN) {
// Age restriction bypass
clientConfig = YoutubeClientConfig.TV_EMBEDDED.copy();
} else {
// Default payload from what we start trying to get required data
clientConfig = YoutubeClientConfig.ANDROID.copy()
clientConfig = YoutubeClientConfig.WEB.copy()
.withClientField("clientScreen", CLIENT_SCREEN_EMBED)
.withThirdPartyEmbedUrl(CLIENT_THIRD_PARTY_EMBED)
.withRootField("params", PLAYER_PARAMS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private String fetchVisitorId() throws IOException {
try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
httpInterface.getContext().setAttribute(TOKEN_FETCH_CONTEXT_ATTRIBUTE, true);

YoutubeClientConfig clientConfig = YoutubeClientConfig.ANDROID.copy().setAttribute(httpInterface);
YoutubeClientConfig clientConfig = YoutubeClientConfig.WEB.copy().setAttribute(httpInterface);
HttpPost visitorIdPost = new HttpPost(VISITOR_ID_URL);
StringEntity visitorIdPayload = new StringEntity(clientConfig.toJsonString(), "UTF-8");
visitorIdPost.setEntity(visitorIdPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
public class YoutubeAudioTrack extends DelegatedAudioTrack {
private static final Logger log = LoggerFactory.getLogger(YoutubeAudioTrack.class);
private static final int MAX_RETRIES = 3;

private final YoutubeAudioSourceManager sourceManager;

Expand All @@ -48,7 +49,30 @@ public void process(LocalAudioTrackExecutor localExecutor) throws Exception {
if (trackInfo.isStream || format.details.getContentLength() == CONTENT_LENGTH_UNKNOWN) {
processStream(localExecutor, format);
} else {
processStaticWithRetry(localExecutor, httpInterface, format);
}
}
}

@SuppressWarnings("ConstantValue")
private void processStaticWithRetry(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface, FormatWithUrl initialFormat) throws Exception {
FormatWithUrl format = initialFormat;

for (int i = 1; i <= MAX_RETRIES; i++) {
log.debug("Opening YouTube stream URL attempt {}/{}", i, MAX_RETRIES);

try {
processStatic(localExecutor, httpInterface, format);
return;
} catch (RuntimeException e) {
String message = e.getMessage();

// Throw if it's not a message we're expecting, or this is the last retry.
if (!"Not success status code: 403".equals(message) && !"Invalid status code for video page response: 400".equals(message) || i == MAX_RETRIES) {
throw e;
}

format = loadBestFormatWithUrl(httpInterface);
}
}
}
Expand Down
Loading