Skip to content

Commit

Permalink
Move twitch URLs to appropriate class, fix #51 (#57)
Browse files Browse the repository at this point in the history
* Moved URLs to TwitchConstants

* Use lowercase streamName everywhere, fixes #51

* Prevent a NullPointerException

* Also move the comment
  • Loading branch information
GravelCZ authored Dec 29, 2023
1 parent cbc0225 commit eb28477
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class TwitchConstants {
static final String TWITCH_GRAPHQL_BASE_URL = "https://gql.twitch.tv/gql";
static final String TWITCH_URL = "https://www.twitch.tv";
static final String TWITCH_IMAGE_PREVIEW_URL = "https://static-cdn.jtvnw.net/previews-ttv/live_user_%s-440x248.jpg";
static final String METADATA_PAYLOAD = "{\"operationName\":\"StreamMetadata\",\"variables\":{\"channelLogin\":\"%s\"},\"extensions\":{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e\"}}}";
static final String ACCESS_TOKEN_PAYLOAD = "{\"operationName\":\"PlaybackAccessToken_Template\",\"query\":\"query PlaybackAccessToken_Template($login: String!,$isLive:Boolean!,$vodID:ID!,$isVod:Boolean!,$playerType:String!){streamPlaybackAccessToken(channelName:$login,params:{platform:\\\"web\\\",playerBackend:\\\"mediaplayer\\\",playerType:$playerType})@include(if:$isLive){value signature __typename}videoPlaybackAccessToken(id:$vodID,params:{platform:\\\"web\\\",playerBackend:\\\"mediaplayer\\\",playerType:$playerType})@include(if:$isVod){value signature __typename}}\",\"variables\":{\"isLive\":true,\"login\":\"%s\",\"isVod\":false,\"vodID\":\"\",\"playerType\":\"site\"}}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,24 @@ public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference)

if (channelInfo == null || channelInfo.get("stream").get("type").isNull()) {
return AudioReference.NO_TRACK;
} else {
String title = channelInfo.get("lastBroadcast").get("title").text();

final String thumbnail = String.format(
"https://static-cdn.jtvnw.net/previews-ttv/live_user_%s-440x248.jpg",
// Using root because the turkish lowercase "i" does not have the little dot above the letter when defaulted
streamName.toLowerCase(Locale.ROOT)
);

return new TwitchStreamAudioTrack(new AudioTrackInfo(
title,
streamName,
Units.DURATION_MS_UNKNOWN,
reference.identifier,
true,
reference.identifier,
thumbnail,
null
), this);
}
String title = channelInfo.get("lastBroadcast").get("title").text();

final String thumbnail = String.format(
TwitchConstants.TWITCH_IMAGE_PREVIEW_URL,
streamName
);

return new TwitchStreamAudioTrack(new AudioTrackInfo(
title,
streamName,
Units.DURATION_MS_UNKNOWN,
reference.identifier,
true,
reference.identifier,
thumbnail,
null
), this);
}

@Override
Expand Down Expand Up @@ -128,7 +126,8 @@ public static String getChannelIdentifierFromUrl(String url) {
return null;
}

return matcher.group(1);
// Using root because the turkish lowercase "i" does not have the little dot above the letter when defaulted
return matcher.group(1).toLowerCase(Locale.ROOT);
}

/**
Expand Down Expand Up @@ -194,7 +193,7 @@ private JsonBrowser fetchStreamChannelInfo(String channelId) {

private void initRequestHeaders() {
try (HttpInterface httpInterface = getHttpInterface()) {
HttpGet get = new HttpGet("https://www.twitch.tv");
HttpGet get = new HttpGet(TwitchConstants.TWITCH_URL);
get.setHeader("Accept", "text/html");
CloseableHttpResponse response = httpInterface.execute(get);
HttpClientTools.assertSuccessWithContent(response, "twitch main page");
Expand Down

0 comments on commit eb28477

Please sign in to comment.