Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JohnChernoff/f4ca74ebf02fe976fce425c032ea9422 to your computer and use it in GitHub Desktop.
Save JohnChernoff/f4ca74ebf02fe976fce425c032ea9422 to your computer and use it in GitHub Desktop.
Credential Attempt #9027
public class BingoChessServ {
Client lichessClient;
boolean polling = false;
ITwitchClient twitchClient;
String currentChannel;
final TwitchIdentityProvider twitchIdentityProvider;
Config config;
public static void main(String[] args) {
new BingoChessServ().startServ();
}
public BingoChessServ() {
loadConfiguration();
if (loadConfiguration().isEmpty()) {
log("No configuration found."); System.exit(1);
}
config = loadConfiguration().get();
twitchIdentityProvider = new TwitchIdentityProvider(
config.getApi().get("twitch_client_id"),
config.getApi().get("twitch_client_secret"),
null);
currentChannel = config.getChannels().getFirst(); log(currentChannel);
lichessClient = Client.auth(config.getCredentials().get("lichess_token"));
OAuth2Credential initialCredential = new OAuth2Credential("twitch",
config.getCredentials().get("access_token"));
initialCredential.setRefreshToken(config.getCredentials().get("refresh_token"));
OAuth2Credential credential = twitchIdentityProvider.getAdditionalCredentialInformation(initialCredential)
.orElseGet(() -> twitchIdentityProvider.refreshCredential(initialCredential)
.flatMap(twitchIdentityProvider::getAdditionalCredentialInformation)
.orElse(null));
if (credential == null) {
throw new RuntimeException("Invalid token!");
} else if (credential != initialCredential) {
updateCredentials(credential);
}
ScheduledThreadPoolExecutor exec = ThreadUtils.getDefaultScheduledThreadPoolExecutor("twitch4j",
Runtime.getRuntime().availableProcessors());
if (credential.getExpiresIn() > 0) {
exec.scheduleAtFixedRate(() -> twitchIdentityProvider.refreshCredential(credential).ifPresent(cred -> {
updateCredentials(credential);
}), credential.getExpiresIn() / 2, Duration.ofHours(1L).getSeconds(), TimeUnit.SECONDS);
}
CredentialManager credentialManager = CredentialManagerBuilder.builder().build();
credentialManager.registerIdentityProvider(twitchIdentityProvider);
log("Cred: " + credential);
twitchClient = TwitchClientPoolBuilder.builder()
.withClientId(config.getApi().get("twitch_client_id"))
.withClientSecret(config.getApi().get("twitch_client_secret"))
.withRedirectUrl("http://localhost:8888")
.withCredentialManager(credentialManager)
.withChatAccount(credential)
.withDefaultAuthToken(credential)
.withEnableChat(true)
.withEnableHelix(true)
.withEnableChatPool(true)
.withScheduledThreadPoolExecutor(exec)
.build();
}
private void updateCredentials(OAuth2Credential credential) {
config.getCredentials().put("access_token", credential.getAccessToken());
saveConfiguration();
}
private Optional<Config> loadConfiguration() {
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("config.yaml");
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return Optional.of(mapper.readValue(is, Config.class));
} catch (Exception ex) { ex.printStackTrace(); }
return Optional.empty();
}
private void saveConfiguration() {
Yaml yaml = new Yaml();
URL resource = BingoChessServ.class.getResource("/config.yaml");
if (resource != null) try {
FileWriter writer = new FileWriter(Paths.get(resource.toURI()).toFile());
yaml.dump(config, writer);
} catch (IOException | URISyntaxException e) {
log("Could not save configuration file: " + e.getMessage());
} else {
log("Cannot find resource: config.yaml");
}
}
//etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment