<commit_before>'use strict';

const POLL_INTERVAL = 30*1000;

function Followers(nodecg) {
  this.nodecg = nodecg;
  this.twitch = nodecg.extensions['lfg-twitchapi'];
  this.latestFollower = nodecg.Replicant("latestFollower", {defaultValue: null, persistent: false});

  this._scheduleFollowers();
}

Followers.prototype._scheduleFollowers = function() {
  this.nodecg.log.debug("Polling for TwitchTV Followers.");
  this.twitch.get('/channels/{{username}}/follows', { limit: 1, direction: 'desc' }, (err, code, body) => {
    if (err) {
      this.nodecg.log.error(err);
      setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
      return;
    }

    if (code != 200) {
      this.nodecg.log.error("Unknown response code: "+code);
      setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
      return;
    }

    if (body.follows.length > 0) {
      this.nodecg.log.debug("Discovered " + body.follows.length + " followers.");
      this.latestFollower.value = body.follows[0].user.display_name;
    }

    setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
  });
}

module.exports = function(api) {
  return new Followers(api);
};
<commit_msg>Emit all follower events<commit_after>'use strict';

const POLL_INTERVAL = 30*1000;

function Followers(nodecg) {
  this.nodecg = nodecg;
  this.twitch = nodecg.extensions['lfg-twitchapi'];
  this.latestFollower = nodecg.Replicant("latestFollower", { defaultValue: null, persistent: true });

  this._scheduleFollowers();
}

Followers.prototype._scheduleFollowers = function() {
  this.nodecg.log.debug("Polling for TwitchTV Followers.");
  this.twitch.get('/channels/{{username}}/follows', { limit: 50, direction: 'desc' }, (err, code, body) => {
    if (err) {
      this.nodecg.log.error(err);
      setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
      return;
    }

    if (code != 200) {
      this.nodecg.log.error("Unknown response code: "+code);
      setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
      return;
    }

    var lastFollowerTs = 0;
    if (this.latestFollower.value) {
      lastFollowerTs = Date.parse(this.latestFollower.value.created_at);
    }

    if (body.follows.length > 0) {
      this.nodecg.log.debug("Discovered " + body.follows.length + " followers.");
      this.latestFollower.value = body.follows[0];

      for (var follower of body.follows.reverse()) {
        var parsedTs = Date.parse(follower.created_at);
        if (parsedTs > lastFollowerTs) {
          this.nodecg.sendMessage('follower', follower);
        }
      }
    }

    setTimeout(() => { this._scheduleFollowers() }, POLL_INTERVAL);
  });
};

module.exports = function(api) {
  return new Followers(api);
};

{'fraction_non_alphanumeric': 0.1600292290829375, 'fraction_numerical': 0.009499451954694921, 'mean_word_length': 3.533112582781457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2165945', 'n_tokens_mistral': 949, 'n_tokens_neox': 888, 'n_words': 155}