Skip to content

Commit

Permalink
add subs, add videos
Browse files Browse the repository at this point in the history
  • Loading branch information
dashaun committed Dec 11, 2023
1 parent 8aae625 commit e146db7
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export BW_SESSION=$(bw unlock --raw)
export TWITCH_CLIENT_ID=$(bw get username twitch-exporter)
export TWITCH_CLIENT_SECRET=$(bw get password twitch-exporter)
export TWITCH_CHANNEL_NAME=javagrunt
export TWITCH_CLIENT_REDIRECT_URL=http://localhost:8080
export TWITCH_CLIENT_REDIRECT_URL=http://localhost:8080
export TWITCH_ACCESS_TOKEN=$(bw get password twitch-exporter-token)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.Collections;

import static com.github.twitch4j.helix.domain.Video.*;

@Service
public class TwitchMetricsExporter {

Expand All @@ -33,12 +35,32 @@ public TwitchMetricsExporter(TwitchService twitchService, MeterRegistry meterReg
Gauge
.builder("twitch_inbound_followers", this, TwitchMetricsExporter::getInboundFollowersCount)
.register(meterRegistry);
Gauge
.builder("twitch_moderator_count", this, TwitchMetricsExporter::getModeratorCount)
.register(meterRegistry);
Gauge
.builder("twitch_subscription_count", this, TwitchMetricsExporter::getSubscriptionCount)
.register(meterRegistry);
Gauge
.builder("twitch_video_count",this, TwitchMetricsExporter::getVideoCount)
.register(meterRegistry);
}

@Scheduled(fixedDelay = 60000) // Run every minute
@Scheduled(fixedDelay = 600000)
public void exportMetrics() {
getLiveViewerCount();
getInboundFollowersCount();
getLiveViewerCount();
getModeratorCount();
getSubscriptionCount();
getVideoCount();
}

Integer getInboundFollowersCount() {
InboundFollowers inboundFollowers = getInboundFollowers();
if (inboundFollowers != null) {
return inboundFollowers.getTotal();
}
return 0;
}

Integer getLiveViewerCount() {
Expand All @@ -49,27 +71,31 @@ Integer getLiveViewerCount() {
return 0;
}

Stream getStream() {
Integer getModeratorCount() {
return getModeratorList().getModerators().isEmpty() ? 0 : getModeratorList().getModerators().size();
}

Integer getSubscriptionCount() {
return getSubscriptionList().getSubscriptions().size();
}

Integer getVideoCount() {
return getVideoList().getVideos().size();
}

private Stream getStream() {
try {
StreamList resultList = twitchClient
.getHelix()
.getStreams(authToken, null, null, null, null, null, null, Collections.singletonList(channel)).execute();
return resultList.getStreams().isEmpty() ? null : resultList.getStreams().get(0);
return resultList.getStreams().isEmpty() ? null : resultList.getStreams().getFirst();
} catch (Exception e) {
logger.error("TwitchMetricsExporter.getStream:", e);
}
return null;
}

Integer getInboundFollowersCount() {
InboundFollowers inboundFollowers = getInboundFollowers();
if (inboundFollowers != null) {
return inboundFollowers.getTotal();
}
return 0;
}

InboundFollowers getInboundFollowers() {
private InboundFollowers getInboundFollowers() {
try {
InboundFollowers inboundFollowers = twitchClient
.getHelix()
Expand All @@ -82,5 +108,37 @@ InboundFollowers getInboundFollowers() {
}
return null;
}


private ModeratorList getModeratorList() {
ModeratorList moderatorList = twitchClient
.getHelix()
.getModerators(authToken, broadcasterId, null, null, null)
.execute();

moderatorList.getModerators().forEach((moderator -> logger.info(moderator.toString())));

return moderatorList;
}

private SubscriptionList getSubscriptionList() {
SubscriptionList subscriptionList = twitchClient
.getHelix().getSubscriptions(authToken, broadcasterId, null, null, null)
.execute();

subscriptionList.getSubscriptions().forEach(subscription -> logger.info(subscription.getUserName()));

return subscriptionList;
}

private VideoList getVideoList() {
VideoList resultList = (VideoList) twitchClient
.getHelix()
.getVideos(authToken, null, broadcasterId, null, null, SearchPeriod.ALL, SearchOrder.TIME, Type.ALL, 100, null, null)
.execute();

resultList.getVideos().forEach(video -> logger.info(video.getTitle()));

return resultList;
}

}
9 changes: 7 additions & 2 deletions src/main/java/com/javagrunt/service/twitch/TwitchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ public class TwitchService {
Logger logger = LoggerFactory.getLogger(TwitchService.class);
private final TwitchClient client;
private final OAuth2Credential oAuth2Credential;
private final String accessToken;

private final String channel;

public TwitchService(@Value("${twitch.client-id}") String clientId,
@Value("${twitch.client-secret}") String clientSecret,
@Value("${twitch.channel}") String channel,
@Value("${twitch.client-redirect-url}") String clientRedirectUrl) {
@Value("${twitch.client-redirect-url}") String clientRedirectUrl,
@Value("${twitch.token}") String accessToken) {
this.accessToken = accessToken;
this.channel = channel;
CredentialManager credentialManager = CredentialManagerBuilder.builder().build();
TwitchIdentityProvider identityProvider = new TwitchIdentityProvider(clientId, clientSecret, clientRedirectUrl);
Expand All @@ -46,7 +49,7 @@ public TwitchClient getClient() {
}

public String getAccessToken() {
return oAuth2Credential.getAccessToken();
return accessToken;
}

public String getBroadcasterId() {
Expand All @@ -57,4 +60,6 @@ public String getBroadcasterId() {
return "";
}



}
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ management.endpoints.web.exposure.include=health,metrics,prometheus,loggers,info
twitch.client-id=${TWITCH_CLIENT_ID}
twitch.client-secret=${TWITCH_CLIENT_SECRET}
twitch.client-redirect-url=${TWITCH_CLIENT_REDIRECT_URL}
twitch.channel=${TWITCH_CHANNEL_NAME}
twitch.channel=${TWITCH_CHANNEL_NAME}

# twitch token -u -s "moderation:read channel:read:subscriptions"
twitch.token=${TWITCH_ACCESS_TOKEN}
10 changes: 5 additions & 5 deletions src/test/java/com/javagrunt/service/twitch/TestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
@TestConfiguration(proxyBeanMethods = false)
public class TestApplication {

// @Bean
// @ServiceConnection(name = "openzipkin/zipkin")
// GenericContainer<?> zipkinContainer() {
// return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin:latest")).withExposedPorts(9411);
// }
@Bean
@ServiceConnection(name = "openzipkin/zipkin")
GenericContainer<?> zipkinContainer() {
return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin:latest")).withExposedPorts(9411);
}

public static void main(String[] args) {
SpringApplication.from(Application::main).with(TestApplication.class).run(args);
Expand Down

0 comments on commit e146db7

Please sign in to comment.