Search in sources :

Example 1 with TwitchUnfollowEvent

use of me.gloriouseggroll.quorrabot.event.twitch.follower.TwitchUnfollowEvent in project quorrabot by GloriousEggroll.

the class FollowersCache method quickUpdate.

public int quickUpdate(String channel) throws Exception {
    JSONObject j = TwitchAPIv3.instance().GetChannelFollows(channel, 100, 0, false);
    if (j.getBoolean("_success")) {
        if (j.getInt("_http") == 200) {
            int i = j.getInt("_total");
            Map<String, JSONObject> newCache = Maps.newHashMap();
            JSONArray followers = j.getJSONArray("follows");
            for (int b = 0; b < followers.length(); b++) {
                JSONObject follower = followers.getJSONObject(b);
                newCache.put(follower.getJSONObject("user").getString("name"), follower);
            }
            for (String key : newCache.keySet()) {
                if (cache == null || !cache.containsKey(key)) {
                    cache.put(key, newCache.get(key));
                    EventBus.instance().post(new TwitchFollowEvent(key, Quorrabot.getChannel(this.channel)));
                }
            }
            if (cache != null) {
                for (String key : cache.keySet()) {
                    if (!newCache.containsKey(key)) {
                        EventBus.instance().post(new TwitchUnfollowEvent(key, Quorrabot.getChannel(this.channel)));
                    }
                }
            }
            this.count = cache.size();
            return i;
        } else {
            throw new Exception("[HTTPErrorException] HTTP " + j.getInt("_http") + " " + j.getString("error") + ". req=" + j.getString("_type") + " " + j.getString("_url") + " " + j.getString("_post") + "   " + (j.has("message") && !j.isNull("message") ? "message=" + j.getString("message") : "content=" + j.getString("_content")));
        }
    } else {
        throw new Exception("[" + j.getString("_exception") + "] " + j.getString("_exceptionMessage"));
    }
}
Also used : TwitchFollowEvent(me.gloriouseggroll.quorrabot.event.twitch.follower.TwitchFollowEvent) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) TwitchUnfollowEvent(me.gloriouseggroll.quorrabot.event.twitch.follower.TwitchUnfollowEvent)

Aggregations

TwitchFollowEvent (me.gloriouseggroll.quorrabot.event.twitch.follower.TwitchFollowEvent)1 TwitchUnfollowEvent (me.gloriouseggroll.quorrabot.event.twitch.follower.TwitchUnfollowEvent)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1