Search in sources :

Example 6 with JSONArray

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

the class SubscriberManager method scanInitialSubscribers.

public void scanInitialSubscribers(String channel, Oauth key, int passesCompleted, HashSet<Subscriber> set) {
    String oauth = key.getKey().split(":")[1];
    String urlString = "https://api.twitch.tv/kraken/channels/" + channel + "/subscriptions?oauth_token=" + oauth + "&limit=100";
    String offset = "&offset=" + String.valueOf(100 * passesCompleted);
    urlString += offset;
    try {
        String line = Utils.createAndParseBufferedReader(new URL(urlString).openStream());
        if (!line.isEmpty()) {
            JSONObject entire = new JSONObject(line);
            if (entire.has("error")) {
                GUIMain.log("Error scanning for initial subs, does your OAuth key allow for this?");
            } else {
                int total = entire.getInt("_total");
                int passes = (total > 100 ? (int) Math.ceil((double) total / 100.0) : 1);
                if (passes == passesCompleted) {
                    fillSubscribers(set);
                    GUIMain.log("Successfully scanned " + set.size() + " subscriber(s)!");
                    Settings.scannedInitialSubscribers.setValue(true);
                } else {
                    JSONArray subs = entire.getJSONArray("subscriptions");
                    for (int subIndex = 0; subIndex < subs.length(); subIndex++) {
                        JSONObject outer = subs.getJSONObject(subIndex);
                        JSONObject user = outer.getJSONObject("user");
                        String name = user.getString("name");
                        //don't want to add yourself
                        if (name.equalsIgnoreCase(channel))
                            continue;
                        LocalDateTime started = LocalDateTime.parse(outer.getString("created_at"), DateTimeFormatter.ISO_DATE_TIME);
                        int streak = (int) started.until(LocalDateTime.now(), ChronoUnit.MONTHS);
                        Subscriber s = new Subscriber(name, started, true, streak);
                        set.add(s);
                    }
                    scanInitialSubscribers(channel, key, passesCompleted + 1, set);
                }
            }
        }
    } catch (Exception e) {
        if (e.getMessage().contains("422")) {
            //the user does not have a sub button
            key.setCanReadSubscribers(false);
            GUIMain.log("Failed to parse subscribers; your channel is not partnered!");
        } else
            GUIMain.log(e);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) JSONObject(lib.JSON.JSONObject) Subscriber(irc.Subscriber) 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