Skip to content

Commit

Permalink
Catch Twitch source init errors, deprecate YouTube source.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Apr 23, 2024
1 parent 3a2ce69 commit 04c0022
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +41,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);

Expand All @@ -51,7 +54,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() {
Expand All @@ -69,7 +79,10 @@ public String getSourceName() {

@Override
public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) {
initRequestHeaders();

String streamName = getChannelIdentifierFromUrl(reference.identifier);

if (streamName == null) {
return null;
}
Expand Down Expand Up @@ -184,22 +197,32 @@ private JsonBrowser fetchStreamChannelInfo(String channelId) {
}

private void initRequestHeaders() {
try (HttpInterface httpInterface = getHttpInterface()) {
HttpGet get = new HttpGet("https://www.twitch.tv");
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("https://www.twitch.tv");
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/lavalink-devs/youtube-source">youtube-source</a>
*/
@Deprecated
public class YoutubeAudioSourceManager implements AudioSourceManager, HttpConfigurable {
private static final Logger log = LoggerFactory.getLogger(YoutubeAudioSourceManager.class);

Expand All @@ -56,18 +58,22 @@ public class YoutubeAudioSourceManager implements AudioSourceManager, HttpConfig

/**
* Create an instance with default settings.
* This source manager is now deprecated. Please use <a href="https://github.com/lavalink-devs/youtube-source">youtube-source</a>
*/
@Deprecated
public YoutubeAudioSourceManager() {
this(true, null, null);
}

/**
* Create an instance.
* This source manager is now deprecated. Please use <a href="https://github.com/lavalink-devs/youtube-source">youtube-source</a>
*
* @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,
Expand All @@ -83,6 +89,10 @@ public YoutubeAudioSourceManager(boolean allowSearch, String email, String passw
);
}

/**
* This source manager is now deprecated. Please use <a href="https://github.com/lavalink-devs/youtube-source">youtube-source</a>
*/
@Deprecated
public YoutubeAudioSourceManager(
boolean allowSearch,
String email,
Expand Down

0 comments on commit 04c0022

Please sign in to comment.