Skip to content

Commit

Permalink
[v1] Use WEB client and add 403 retry
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Mar 30, 2024
1 parent 3a2ce69 commit 0eaeee1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
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

0 comments on commit 0eaeee1

Please sign in to comment.