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 cf4d70db..033eadac 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 @@ -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); diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAccessTokenTracker.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAccessTokenTracker.java index d29b26c0..22d0178e 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAccessTokenTracker.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAccessTokenTracker.java @@ -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); diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioTrack.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioTrack.java index 1f225fe7..c6177ac7 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioTrack.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioTrack.java @@ -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; @@ -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); } } }