<commit_before>var nazgul = require('./nazgul');
var irc = require('irc');
var async = nazgul.async,
    await = nazgul.await,
    Config = nazgul.Config,
    Log = nazgul.Log,
    sprintf = nazgul.sprintf,
    MongoClient = nazgul.MongoClient,
    Twitch = nazgul.Twitch;

var monitor = async(function(channels) {
  Log.info('Chat monitoring starting.');

  var db = await(MongoClient.connectAsync(Config.mongo));
  var messages = db.collection('chat:messages');
  var joins = db.collection('chat:joins');
  var parts = db.collection('chat:parts');

  var twitchIds = {};
  for (var i = 0; i < channels.length; ++i) {
    var channel = channels[i].toLowerCase();
    var url = sprintf('https://api.twitch.tv/kraken/users/%s', channel);

    var response = await(Twitch.request(url));
    var id = response._id;

    if (!id) {
      throw new Error(sprintf('Could not find Twitch channel ID for %s.', channel));
    }

    twitchIds[channel] = id;
  }

  var bot = new irc.Client('irc.twitch.tv', Config.twitch.username, {
    port: 6667,
    showErrors: true,
    password: 'oauth:' + Config.twitch.oauth,
    userName: Config.twitch.username,
    realName: Config.twitch.username,
    autoConnect: false,
    showErrors: true,
    stripColors: true,
    secure: false
  });

  bot.on('error', function(message) {
    Log.error('IRC error: ' + message);
  });

  Log.info('Connecting to Twitch IRC servers.');
  bot.connect(5, function() {
    Log.info('Connected to Twitch IRC servers.');

    for (var i = 0; i < channels.length; ++i) {<commit_msg>Search for Twitch user ID in Mongo before making external request.<commit_after>var nazgul = require('./nazgul');
var irc = require('irc');
var async = nazgul.async,
    await = nazgul.await,
    Config = nazgul.Config,
    Log = nazgul.Log,
    sprintf = nazgul.sprintf,
    MongoClient = nazgul.MongoClient,
    Twitch = nazgul.Twitch;

var monitor = async(function(channels) {
  Log.info('Chat monitoring starting.');

  var db = await(MongoClient.connectAsync(Config.mongo));
  var messages = db.collection('chat:messages');
  var joins = db.collection('chat:joins');
  var parts = db.collection('chat:parts');
  var snapshots = db.collection('stream:snapshots');

  var twitchIds = {};
  for (var i = 0; i < channels.length; ++i) {
    var channel = channels[i].toLowerCase();

    var id = null;

    var stream = await(snapshots.findOneAsync({ ch: channel }, { ui: 1 }));
    if (stream && stream.ui) {
      id = stream.ui;
    } else {
      var url = sprintf('https://api.twitch.tv/kraken/users/%s', channel);
      var response = await(Twitch.request(url));
      id = response._id;
    }

    if (!id) {
      throw new Error(sprintf('Could not find Twitch channel ID for %s.', channel));
    }

    twitchIds[channel] = id;
  }

  var bot = new irc.Client('irc.twitch.tv', Config.twitch.username, {
    port: 6667,
    showErrors: true,
    password: '<PASSWORD>:' + Config.twitch.oauth,
    userName: Config.twitch.username,
    realName: Config.twitch.username,
    autoConnect: false,
    showErrors: true,
    stripColors: true,
    secure: false
  });

  bot.on('error', function(message) {
    Log.error('IRC error: ' + message);
  });

  Log.info('Connecting to Twitch IRC servers.');
  bot.connect(5, function() {
    Log.info('Connected to Twitch IRC servers.');

    for (var i = 0; i < channels.length; ++i) {
{'fraction_non_alphanumeric': 0.1533234859675037, 'fraction_numerical': 0.004431314623338257, 'mean_word_length': 3.291508238276299, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '2433416', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1156, 'n_words': 276}