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] Fix Twitch sometimes throwing on initialization #100

Merged
merged 2 commits into from
Apr 26, 2024
Merged
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 @@ -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 @@ -70,10 +80,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()) {
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
Loading