Skip to content

Commit

Permalink
quick changes to twitch api stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragovorn committed Oct 14, 2017
1 parent 94f8589 commit cfc7568
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.dragovorn</groupId>
<artifactId>dragon-bot</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<build>
<finalName>DragonBot</finalName>
<plugins>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/dragovorn/dragonbot/DragonBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.dragovorn.dragonbot.api.bot.scheduler.BotScheduler;
import com.dragovorn.dragonbot.api.bot.scheduler.Scheduler;
import com.dragovorn.dragonbot.api.github.GitHubAPI;
import com.dragovorn.dragonbot.api.twitch.TwitchAPI;
import com.dragovorn.dragonbot.bot.*;
import com.dragovorn.dragonbot.command.Github;
import com.dragovorn.dragonbot.command.VersionCmd;
Expand Down Expand Up @@ -95,6 +96,8 @@ public class DragonBot extends Bot {

private GitHubAPI gitHubAPI;

private TwitchAPI twitchAPI;

private final Logger logger;

public DragonBot() throws IOException {
Expand Down Expand Up @@ -152,7 +155,9 @@ public DragonBot() throws IOException {
this.commandManager = new CommandManager();
this.logger = new DragonLogger("Dragon Bot", FileManager.getLogs() + File.separator + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "-%g.log");
this.gitHubAPI = new GitHubAPI("dragovorn", "dragon-bot-twitch");
this.twitchAPI = new TwitchAPI();
this.scheduler = new BotScheduler();
this.twitchAPI.setClientId(this.config.getAPIKey());

System.setErr(new PrintStream(new LoggingOutputStream(this.logger, Level.SEVERE), true));
System.setOut(new PrintStream(new LoggingOutputStream(this.logger, Level.INFO), true));
Expand Down Expand Up @@ -675,6 +680,10 @@ public GitHubAPI getGitHubAPI() {
return this.gitHubAPI;
}

public TwitchAPI getTwitchAPI() {
return this.twitchAPI;
}

public Scheduler getScheduler() {
return this.scheduler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public GitHubAPI(String owner, String repository) {
}

public JSONObject getLatestRelease() throws IOException {
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + owner + "/" + repository + "/releases/latest"));
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + this.owner + "/" + this.repository + "/releases/latest"));

return new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
}

public JSONObject getRelease(String tag) throws IOException {
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + owner + "/" + repository + "/releases/tags/" + tag));
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + this.owner + "/" + this.repository + "/releases/tags/" + tag));

return new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
}

public int getNumCommitsBetween(String tag1, String tag2) throws IOException {
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + owner + "/" + repository + "/compare/" + tag1 + "..." + tag2));
HttpResponse response = client.execute(makeGetRequest(BASE_URL + REPO + this.owner + "/" + this.repository + "/compare/" + tag1 + "..." + tag2));

JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/dragovorn/dragonbot/api/twitch/TwitchAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ public class TwitchAPI {
private static final String BASE_URL = "https://api.twitch.tv/kraken/";
private static final String CHANNELS = "channels/";
private static final String STREAMS = "streams/";
private final String clientId;
private String clientId;

private final HttpClient client;

public TwitchAPI(String clientId) {
this.clientId = clientId;
public TwitchAPI() {
this.client = HttpClientBuilder.create().build();
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getClientId() {
return this.clientId;
}

public JSONObject getChannel(String channel) throws IOException {
HttpResponse response = client.execute(makeGetRequest(BASE_URL + CHANNELS + channel));

Expand Down
27 changes: 18 additions & 9 deletions src/main/java/com/dragovorn/dragonbot/bot/BotConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ protected void addDefaults() {
this.defaults.put("oauth", "");
this.defaults.put("channel", "");
this.defaults.put("console", true);
this.defaults.put("auto connect", false);
this.defaults.put("ask for update", true);
this.defaults.put("check for updates", true);
this.defaults.put("auto-connect", false);
this.defaults.put("ask-for-update", true);
this.defaults.put("check-for-updates", true);
this.defaults.put("api-key", "");
}

public String getName() {
Expand All @@ -53,20 +54,24 @@ public String getChannel() {
return getString("channel");
}

public String getAPIKey() {
return getString("api-key");
}

public boolean getConsole() {
return getBoolean("console");
}

public boolean getAutoConnect() {
return getBoolean("auto connect");
return getBoolean("auto-connect");
}

public boolean getAskForUpdates() {
return getBoolean("ask for update");
return getBoolean("ask-for-update");
}

public boolean getCheckForUpdates() {
return getBoolean("check for updates");
return getBoolean("check-for-updates");
}

public void setName(String name) {
Expand All @@ -81,19 +86,23 @@ public void setChannel(String channel) {
set("channel", channel);
}

public void setAPIKey(String apiKey) {
set("api-key", apiKey);
}

public void setConsole(boolean console) {
set("console", console);
}

public void setAutoConnect(boolean autoConnect) {
set("auto connect", autoConnect);
set("auto-connect", autoConnect);
}

public void setAskForUpdates(boolean askForUpdates) {
set("ask for update", askForUpdates);
set("ask-for-update", askForUpdates);
}

public void setCheckForUpdates(boolean checkForUpdates) {
set("check for updates", checkForUpdates);
set("check-for-updates", checkForUpdates);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void actionPerformed(ActionEvent event) {
ConsoleWindow.getInstance().getFrame().dispose();
}
}

DragonBot.getInstance().getTwitchAPI().setClientId(OptionsPanel.getInstance().getClientId());
}

if (OptionsPanel.getInstance().getAutoConnect().isSelected() != Bot.getInstance().getConfiguration().getAutoConnect()) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/dragovorn/dragonbot/gui/panel/OptionsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class OptionsPanel extends JPanel {
private JCheckBox autoConnect;

private JTextField username;
private JTextField clientId;

private final JLabel testStatus;

Expand Down Expand Up @@ -70,6 +71,11 @@ public OptionsPanel() {
this.autoConnect.setToolTipText("Allow the bot to automatically connect to \'" + Bot.getInstance().getConfiguration().getChannel() + "\' when it starts up.");
this.autoConnect.setSelected(Bot.getInstance().getConfiguration().getAutoConnect());

this.clientId = new JTextField(10);
this.clientId.setToolTipText("The client ID the bot uses to run twitch api queries.");
this.clientId.setMaximumSize(new Dimension(350, 20));
new TextPrompt("Client ID", this.clientId);

this.username = new JTextField(10);
this.username.setToolTipText("The username for the bot");
this.username.setMaximumSize(this.username.getPreferredSize());
Expand Down Expand Up @@ -150,6 +156,7 @@ public OptionsPanel() {
apply.addActionListener(new ApplyListener());

options.add(this.console);
options.add(this.clientId);

if (!Bot.getInstance().getConfiguration().getChannel().equals("")) {
options.add(this.autoConnect);
Expand Down Expand Up @@ -187,6 +194,10 @@ public boolean discrepancies() {
return true;
}

if (!this.clientId.getText().equals(DragonBot.getInstance().getTwitchAPI().getClientId())) {
return true;
}

return hasAccountInfoChanged();
}

Expand All @@ -207,6 +218,10 @@ public JLabel getTestStatus() {
return this.testStatus;
}

public String getClientId() {
return this.clientId.getText();
}

public static OptionsPanel getInstance() {
return instance;
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/META-INF/MANIFEST.MF

This file was deleted.

0 comments on commit cfc7568

Please sign in to comment.