Search in sources :

Example 1 with JSONObject

use of lib.JSON.JSONObject in project Botnak by Gocnak.

the class FaceManager method handleEmoteSet.

public static void handleEmoteSet(String emotes) {
    if (checkedEmoteSets)
        return;
    ThreadEngine.submit(() -> {
        try {
            checkedEmoteSets = true;
            String line = APIRequests.Twitch.getEmoteSet(emotes);
            if (!line.isEmpty()) {
                User main = Settings.channelManager.getUser(Settings.accountManager.getUserAccount().getName(), true);
                JSONObject init = new JSONObject(line);
                String[] keys = emotes.split(",");
                JSONObject emote_sets = init.getJSONObject("emoticon_sets");
                for (String s : keys) {
                    JSONArray set = emote_sets.getJSONArray(s);
                    for (int i = 0; i < set.length(); i++) {
                        JSONObject emote = set.getJSONObject(i);
                        int ID = emote.getInt("id");
                        main.addEmote(ID);
                        if (doneWithTwitchFaces) {
                            if (twitchFaceMap.get(ID) == null) {
                                downloadEmote(ID);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            GUIMain.log("FaceManager: Failed to download EmoteSets!");
            checkedEmoteSets = false;
        }
    });
}
Also used : User(lib.pircbot.User) JSONObject(lib.JSON.JSONObject) JSONArray(lib.JSON.JSONArray)

Example 2 with JSONObject

use of lib.JSON.JSONObject in project Botnak by Gocnak.

the class FaceManager method buildMap.

/**
     * Builds the giant all-containing Twitch Face map.
     */
public static void buildMap() {
    try {
        // Load twitch faces
        String line = APIRequests.Twitch.getAllEmotes();
        if (!line.isEmpty()) {
            try {
                JSONObject init = new JSONObject(line);
                JSONArray emotes = init.getJSONArray("emoticons");
                for (int i = 0; i < emotes.length(); i++) {
                    JSONObject emote = emotes.getJSONObject(i);
                    int ID = emote.getInt("id");
                    if (twitchFaceMap.get(ID) != null)
                        continue;
                    String regex = emote.getString("code").replaceAll("\\\\&lt\\\\;", "\\<").replaceAll("\\\\&gt\\\\;", "\\>");
                    String URL = "http://static-cdn.jtvnw.net/emoticons/v1/" + ID + "/1.0";
                    onlineTwitchFaces.put(ID, new TwitchFace(regex, URL, true));
                }
            } catch (Exception e) {
                GUIMain.log("Failed to load online Twitch faces, is the API endpoint down?");
            }
        }
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : JSONObject(lib.JSON.JSONObject) JSONArray(lib.JSON.JSONArray)

Example 3 with JSONObject

use of lib.JSON.JSONObject in project Botnak by Gocnak.

the class UserManager method beat.

@Override
public void beat() {
    beating = true;
    String[] channels = Settings.channelManager.getChannelNames();
    URL url;
    for (String chan : channels) {
        String chanOut = chan.substring(1);
        GUIViewerList list = GUIMain.viewerLists.get(chanOut);
        if (list != null) {
            try {
                url = new URL("http://tmi.twitch.tv/group/user/" + chan.substring(1) + "/chatters");
                String line = Utils.createAndParseBufferedReader(url.openStream());
                if (!line.isEmpty()) {
                    final Enumeration<TreePath> beforePaths = list.beforePaths();
                    final int beforeScroll = list.beforeScroll();
                    JSONObject site = new JSONObject(line);
                    JSONObject chatters = site.getJSONObject("chatters");
                    JSONArray mods = chatters.getJSONArray("moderators");
                    readAndUpdate(mods, list, GUIViewerList.ViewerType.MOD);
                    JSONArray staff = chatters.getJSONArray("staff");
                    readAndUpdate(staff, list, GUIViewerList.ViewerType.STAFF);
                    JSONArray admins = chatters.getJSONArray("admins");
                    readAndUpdate(admins, list, GUIViewerList.ViewerType.ADMIN);
                    JSONArray global_mods = chatters.getJSONArray("global_mods");
                    readAndUpdate(global_mods, list, GUIViewerList.ViewerType.GLOBAL_MOD);
                    JSONArray viewers = chatters.getJSONArray("viewers");
                    readAndUpdate(viewers, list, GUIViewerList.ViewerType.VIEWER);
                    EventQueue.invokeLater(() -> list.updateRoot(beforePaths, beforeScroll));
                    Thread.sleep(2000);
                }
            } catch (Exception e) {
                GUIMain.log(e);
            }
        }
    }
}
Also used : TreePath(javax.swing.tree.TreePath) JSONObject(lib.JSON.JSONObject) JSONArray(lib.JSON.JSONArray) GUIViewerList(gui.forms.GUIViewerList) URL(java.net.URL)

Example 4 with JSONObject

use of lib.JSON.JSONObject in project Botnak by Gocnak.

the class DonationManager method scanInitialDonations.

public void scanInitialDonations(int passesCompleted) {
    String url = "https://streamtip.com/api/tips?client_id=" + getClientID() + "&access_token=" + getAccessCode() + "&limit=100&direction=asc";
    try {
        String offset = "&offset=" + String.valueOf(100 * passesCompleted);
        String line = Utils.createAndParseBufferedReader(new URL(url + offset).openStream());
        if (!line.isEmpty()) {
            JSONObject outerShell = new JSONObject(line);
            int status = outerShell.getInt("status");
            int count = outerShell.getInt("_count");
            if (count > 0) {
                if (status == 200) {
                    //ensure there's no problem with the site
                    JSONArray tipsArray = outerShell.getJSONArray("tips");
                    for (int i = 0; i < tipsArray.length(); i++) {
                        JSONObject tip = tipsArray.getJSONObject(i);
                        if (lastDonation != null) {
                            if (lastDonation.getDonationID().equals(tip.getString("_id"))) {
                                continue;
                            }
                        }
                        addDonation(tip, true);
                    //we're simulating a local donation here to not spam the chat with all the donations
                    }
                    scanInitialDonations(passesCompleted + 1);
                } else {
                    GUIMain.log("Failed to scan initial donations due to an error on Streamtip!");
                }
            } else {
                //finished!
                GUIMain.log("Successfully scanned initial donations!");
            }
        }
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : JSONObject(lib.JSON.JSONObject) JSONArray(lib.JSON.JSONArray) URL(java.net.URL)

Example 5 with JSONObject

use of lib.JSON.JSONObject in project Botnak by Gocnak.

the class DonationManager method checkDonations.

public void checkDonations(boolean single) {
    int limit = (single ? 5 : 100);
    String url = "https://streamtip.com/api/tips?client_id=" + getClientID() + "&access_token=" + getAccessCode() + "&limit=" + limit;
    try {
        String line = Utils.createAndParseBufferedReader(new URL(url).openStream());
        if (!line.isEmpty()) {
            JSONObject outerShell = new JSONObject(line);
            int status = outerShell.getInt("status");
            int count = outerShell.getInt("_count");
            if (status == 200) {
                //ensure there's no problem with the site
                JSONArray tipsArray = outerShell.getJSONArray("tips");
                for (int i = (single ? tipsArray.length() - 1 : count - 1); i > -1; i--) {
                    JSONObject tip = tipsArray.getJSONObject(i);
                    try {
                        Currency c = Currency.getInstance(tip.getString("currencyCode"));
                        getCurrencyFormat().setCurrency(c);
                    } catch (Exception e) {
                        GUIMain.log("Unknown currency code: " + tip.getString("currencyCode"));
                        getCurrencyFormat().setCurrency(Currency.getInstance("USD"));
                    }
                    if (lastDonation != null) {
                        if (lastDonation.getDonationID().equals(tip.getString("_id"))) {
                            continue;
                        }
                    }
                    addDonation(tip, false);
                }
            }
        }
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : JSONObject(lib.JSON.JSONObject) Currency(java.util.Currency) JSONArray(lib.JSON.JSONArray) URL(java.net.URL)

Aggregations

JSONArray (lib.JSON.JSONArray)6 JSONObject (lib.JSON.JSONObject)6 URL (java.net.URL)4 GUIViewerList (gui.forms.GUIViewerList)1 Subscriber (irc.Subscriber)1 LocalDateTime (java.time.LocalDateTime)1 Currency (java.util.Currency)1 TreePath (javax.swing.tree.TreePath)1 User (lib.pircbot.User)1