diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamAudioSourceManager.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamAudioSourceManager.java index 9d23e7e5..b2066f4b 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamAudioSourceManager.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamAudioSourceManager.java @@ -20,6 +20,8 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.DataInput; import java.io.DataOutput; @@ -40,6 +42,7 @@ * Audio source manager which detects Twitch tracks by URL. */ public class TwitchStreamAudioSourceManager implements AudioSourceManager, HttpConfigurable { + private static final Logger log = LoggerFactory.getLogger(TwitchStreamAudioSourceManager.class); private static final String STREAM_NAME_REGEX = "^https://(?:www\\.|go\\.|m\\.)?twitch.tv/([^/]+)$"; private static final Pattern streamNameRegex = Pattern.compile(STREAM_NAME_REGEX); @@ -52,7 +55,14 @@ public class TwitchStreamAudioSourceManager implements AudioSourceManager, HttpC */ public TwitchStreamAudioSourceManager() { httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager(); - initRequestHeaders(); + + try { + initRequestHeaders(); + } catch (FriendlyException e) { + log.warn("Failed to fetch Twitch source keys. " + + "Key fetching will be retried upon loading a Twitch track. " + + "This warning can be ignored unless the issue persists.", e); + } } public String getClientId() { @@ -71,10 +81,13 @@ public String getSourceName() { @Override public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) { String streamName = getChannelIdentifierFromUrl(reference.identifier); + if (streamName == null) { return null; } + initRequestHeaders(); + JsonBrowser channelInfo = fetchStreamChannelInfo(streamName).get("data").get("user"); if (channelInfo == null || channelInfo.get("stream").get("type").isNull()) { @@ -192,22 +205,32 @@ private JsonBrowser fetchStreamChannelInfo(String channelId) { } private void initRequestHeaders() { - try (HttpInterface httpInterface = getHttpInterface()) { - HttpGet get = new HttpGet(TwitchConstants.TWITCH_URL); - get.setHeader("Accept", "text/html"); - CloseableHttpResponse response = httpInterface.execute(get); - HttpClientTools.assertSuccessWithContent(response, "twitch main page"); + if (twitchClientId != null) { + return; + } + + synchronized (this) { + if (twitchClientId != null) { + return; + } + + try (HttpInterface httpInterface = getHttpInterface()) { + HttpGet get = new HttpGet(TwitchConstants.TWITCH_URL); + get.setHeader("Accept", "text/html"); + CloseableHttpResponse response = httpInterface.execute(get); + HttpClientTools.assertSuccessWithContent(response, "twitch main page"); - String responseText = EntityUtils.toString(response.getEntity()); - twitchClientId = DataFormatTools.extractBetween(responseText, "clientId=\"", "\""); + String responseText = EntityUtils.toString(response.getEntity()); + twitchClientId = DataFormatTools.extractBetween(responseText, "clientId=\"", "\""); - for (Header header : response.getAllHeaders()) { - if (header.getName().contains("Set-Cookie") && header.getValue().contains("unique_id=")) { - twitchDeviceId = DataFormatTools.extractBetween(header.toString(), "unique_id=", ";"); + for (Header header : response.getAllHeaders()) { + if (header.getName().contains("Set-Cookie") && header.getValue().contains("unique_id=")) { + twitchDeviceId = DataFormatTools.extractBetween(header.toString(), "unique_id=", ";"); + } } + } catch (IOException e) { + throw new FriendlyException("Loading Twitch main page failed.", SUSPICIOUS, e); } - } catch (IOException e) { - throw new FriendlyException("Loading Twitch main page failed.", SUSPICIOUS, e); } } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioSourceManager.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioSourceManager.java index bd55a680..d0ce9316 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioSourceManager.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/youtube/YoutubeAudioSourceManager.java @@ -37,7 +37,9 @@ /** * Audio source manager that implements finding Youtube videos or playlists based on an URL or ID. + * This source manager is now deprecated. Please use youtube-source */ +@Deprecated public class YoutubeAudioSourceManager implements AudioSourceManager, HttpConfigurable { private static final Logger log = LoggerFactory.getLogger(YoutubeAudioSourceManager.class); @@ -56,7 +58,9 @@ public class YoutubeAudioSourceManager implements AudioSourceManager, HttpConfig /** * Create an instance with default settings. + * This source manager is now deprecated. Please use youtube-source */ + @Deprecated public YoutubeAudioSourceManager() { this(true, null, null); } @@ -64,10 +68,13 @@ public YoutubeAudioSourceManager() { /** * Create an instance. * + * This source manager is now deprecated. Please use youtube-source + * * @param allowSearch Whether to allow search queries as identifiers * @param email Email of Google account to auth in, required for playing age restricted tracks * @param password Password of Google account to auth in, required for playing age restricted tracks */ + @Deprecated public YoutubeAudioSourceManager(boolean allowSearch, String email, String password) { this( allowSearch, @@ -83,6 +90,10 @@ public YoutubeAudioSourceManager(boolean allowSearch, String email, String passw ); } + /** + * This source manager is now deprecated. Please use youtube-source + */ + @Deprecated public YoutubeAudioSourceManager( boolean allowSearch, String email,