Java tutorial
/* * -------------------------------------------------------------------------------- * -------------------- Copyright (C) 2016 <SoWhoYou, Inc.> ----------------------- * -------------------------------------------------------------------------------- * Project Name : TwitchBot * Github Repo : https://www.github.com/SoWhoYou * -------------------------------------------------------------------------------- * Author : SoWhoYou * Website : www.SoWhoYou.com * E-Mail : SoWhoYou@Gmail.com * -------------------------------------------------------------------------------- * Created on : <Jun 2, 2016> at <4:01:07 AM> * -------------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * -------------------------------------------------------------------------------- */ package com.sowhoyou.twitchbot.chatbot; import java.net.URL; import org.apache.commons.io.IOUtils; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.sowhoyou.twitchbot.user.User; public class BotHelper { private Bot bot; public BotHelper(Bot bot) { this.setBot(bot); } public String getTotalViewsToString() { try { URL url = new URL("https://api.twitch.tv/kraken/channels/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); return ("Views: [" + jObject.get("views").toString() + "]"); } catch (Exception e) { } return null; } public String getTotalViewersToString() { try { URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); return ("Viewers: [" + jObject.get("chatter_count").toString() + "]"); } catch (Exception e) { } return null; } public void getModList() { try { URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String moderators = jObject.get("moderators").toString(); jArray.addAll(jPhraser.parse(moderators).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { User u = this.getBot().getUserList() .getUser(s.toString().toLowerCase().replace('"', ' ').replace(" ", "")); u.setMod(true); sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { this.getBot().sendDebug("Moderators: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { this.getBot().sendDebug("There are No Visable Moderators!"); } } catch (Exception e) { } } public String getModListToString() { try { URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String moderators = jObject.get("moderators").toString(); jArray.addAll(jPhraser.parse(moderators).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { return ("Moderators: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { return ("There are No Visable Moderators!"); } } catch (Exception e) { } return null; } public void getViewerList() { try { URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String viewers = jObject.get("viewers").toString(); jArray.addAll(jPhraser.parse(viewers).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { User u = this.getBot().getUserList() .getUser(s.toString().toLowerCase().replace('"', ' ').replace(" ", "")); u.setMod(false); sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { this.getBot().sendDebug("Viewers: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { this.getBot().sendDebug("There are No Visable Viewers!"); } } catch (Exception e) { } } public String getViewerListToString() { try { URL url = new URL(("http://tmi.twitch.tv/group/user/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/chatters")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonArray jArray = new JsonArray(); JsonObject jObject = jPhraser.parse(jText).getAsJsonObject(); String streamObject = jObject.get("chatters").toString(); jObject = jPhraser.parse(streamObject).getAsJsonObject(); String viewers = jObject.get("viewers").toString(); jArray.addAll(jPhraser.parse(viewers).getAsJsonArray()); StringBuilder sb = new StringBuilder(); int viewerCount = 0; for (JsonElement s : jArray.getAsJsonArray()) { sb.append(", " + s.toString().replace('"', ' ').replace(" ", "")); viewerCount++; } if (viewerCount > 0) { return ("Viewers: [" + sb.toString().replaceFirst(", ", "") + "]"); } else { return ("There are No Visable Viewers!"); } } catch (Exception e) { } return null; } public String getTotalFollowersToString() { try { URL url = new URL("https://api.twitch.tv/kraken/channels/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "")); JsonParser jPhraser = new JsonParser(); JsonObject jObject = jPhraser.parse(IOUtils.toString(url)).getAsJsonObject(); return ("Followers: [" + jObject.get("followers").toString() + "]"); } catch (Exception e) { } return null; } public String getLatestFollowerToString() { try { URL url = new URL(("https://api.twitch.tv/kraken/channels/" + this.getBot().getBotConfig().getBotChannel().toLowerCase().replace("#", "") + "/follows?limit=1")); String jText = IOUtils.toString(url); JsonParser jPhraser = new JsonParser(); JsonObject jo = jPhraser.parse(jText).getAsJsonObject(); JsonArray ja1 = jo.getAsJsonArray("follows"); String s1 = ja1.get(0).toString(); JsonObject jo2 = jPhraser.parse(s1).getAsJsonObject(); JsonObject jo3 = jo2.get("user").getAsJsonObject(); String s2 = jo3.get("name").toString(); return ("Latest Follower: [" + s2.replace('"', ' ').replace(" ", "") + "]"); } catch (Exception e) { } return null; } public Bot getBot() { return bot; } public void setBot(Bot bot) { this.bot = bot; } }