From e146db7eea754f936eecfc58c4449423464066dc Mon Sep 17 00:00:00 2001 From: DaShaun Date: Mon, 11 Dec 2023 16:04:44 -0600 Subject: [PATCH] add subs, add videos --- .envrc | 3 +- .../service/twitch/TwitchMetricsExporter.java | 86 ++++++++++++++++--- .../service/twitch/TwitchService.java | 9 +- src/main/resources/application.properties | 5 +- .../service/twitch/TestApplication.java | 10 +-- 5 files changed, 90 insertions(+), 23 deletions(-) diff --git a/.envrc b/.envrc index 4fb7b96..baf5292 100644 --- a/.envrc +++ b/.envrc @@ -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 \ No newline at end of file +export TWITCH_CLIENT_REDIRECT_URL=http://localhost:8080 +export TWITCH_ACCESS_TOKEN=$(bw get password twitch-exporter-token) \ No newline at end of file diff --git a/src/main/java/com/javagrunt/service/twitch/TwitchMetricsExporter.java b/src/main/java/com/javagrunt/service/twitch/TwitchMetricsExporter.java index e3d3303..5291d45 100644 --- a/src/main/java/com/javagrunt/service/twitch/TwitchMetricsExporter.java +++ b/src/main/java/com/javagrunt/service/twitch/TwitchMetricsExporter.java @@ -12,6 +12,8 @@ import java.util.Collections; +import static com.github.twitch4j.helix.domain.Video.*; + @Service public class TwitchMetricsExporter { @@ -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() { @@ -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() @@ -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; + } + } \ No newline at end of file diff --git a/src/main/java/com/javagrunt/service/twitch/TwitchService.java b/src/main/java/com/javagrunt/service/twitch/TwitchService.java index e4f3249..df5bfc9 100644 --- a/src/main/java/com/javagrunt/service/twitch/TwitchService.java +++ b/src/main/java/com/javagrunt/service/twitch/TwitchService.java @@ -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); @@ -46,7 +49,7 @@ public TwitchClient getClient() { } public String getAccessToken() { - return oAuth2Credential.getAccessToken(); + return accessToken; } public String getBroadcasterId() { @@ -57,4 +60,6 @@ public String getBroadcasterId() { return ""; } + + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 458c97c..1042ff1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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} \ No newline at end of file +twitch.channel=${TWITCH_CHANNEL_NAME} + +# twitch token -u -s "moderation:read channel:read:subscriptions" +twitch.token=${TWITCH_ACCESS_TOKEN} \ No newline at end of file diff --git a/src/test/java/com/javagrunt/service/twitch/TestApplication.java b/src/test/java/com/javagrunt/service/twitch/TestApplication.java index 7e4223c..6e3f84c 100644 --- a/src/test/java/com/javagrunt/service/twitch/TestApplication.java +++ b/src/test/java/com/javagrunt/service/twitch/TestApplication.java @@ -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);