<commit_before>var http = require('http');

var leagueInfo = {
  leagueName: 'NHL',
  updateURL: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp'
};

var getGameArray = function(next) {
  var rawData = '';
  var request = http.get(leagueInfo.updateURL, function httpSetup(res) {
    res.on('data', function httpData(chunk) {
      rawData += chunk;
    });
    res.on('end', function httpEnd() {
      try {
        rawData = rawData.replace('loadScoreboard(','');
        rawData = rawData.substring(0,(rawData.length-2));
        var rawArray = JSON.parse(rawData).games;
        var gameArray = [];
        for (var i = 0; i < rawArray.length; i++) {
          gameArray.push(parseRawGame(rawArray[i]));
        }
        next(null, gameArray);
      } catch(e) {
        e.source = 'NHL';
        next(e);
      }
    });
  }).on('error', function httpError(e) {
    console.log('NHL http Error: ' + e.message);
  });
};

var parseRawGame = function(rawGame) {
  var game = { };
  var date = new Date();
  var gameState = '';
  var timeString = '';
  var periodString = '';
  switch (rawGame.gs) {
    case '1':
      gameState = 'Scheduled';
      var arr = rawGame.ts.split(' ');
      var dateArr = arr[arr.length-1].split('/');
      date.setMonth(parseInt(dateArr[0])-1);
      date.setDate(parseInt(dateArr[1]));
      timeString = rawGame.bs;
      break;
    case '2':
      gameState = 'Scheduled';
      break;
    case '3':
      if (rawGame.ts.indexOf('END') !== -1) {
        gameState = 'Intermission';
      } else {
        gameState = 'Progress';
      }
      timeString = rawGame.ts.split(' ')[0];
      periodString = rawGame.ts.split(' ')[1];
      break;
    case '4':
      if (rawGame.ts.indexOf('SHOOTOUT') !== -1) {
        gameState = 'Shootout';
      } else {
        timeString = rawGame.ts.split(' ')[0];
        periodString = rawGame.ts.split(' ')[1];
        if (periodString === 'OT') {
          gameState = 'Overtime';
        } else if (periodString === 'SO') {
          gameState = 'Shootout';
        } else {
          gameState = 'Progress';
        }
      }
      break;
    case '5':
      gameState = 'Ended';
      var arr = rawGame.ts.split(' ');
      var dateArr = arr[arr.length-1].split('/');
      date.setMonth(parseInt(dateArr[0])-1);
      date.setDate(parseInt(dateArr[1]));
      break;
  }
  game.GameSymbol = rawGame.id;
  game.Date = date;
  game.State = gameState;
  game.Time = timeString;
  game.Period = periodString;
  game.AwayTeamCity = rawGame.atn;
  game.AwayTeamName = rawGame.atv;
  game.AwayScore = (rawGame.ats === '' ? 0 : parseInt(rawGame.ats));
  game.HomeTeamCity = rawGame.htn;
  game.HomeTeamName = rawGame.htv;
  game.HomeScore = (rawGame.hts === '' ? 0 : parseInt(rawGame.hts));
  game.RawInstance = JSON.stringify(rawGame,null,2);
  return game;
};

var gameChanged = function(oldGame, newGame) {
  return (oldGame.GameSymbol === newGame.GameSymbol) &&
    ( (oldGame.State !== newGame.State) ||
      (oldGame.Period !== newGame.Period) ||
      (oldGame.AwayScore !== newGame.AwayScore) ||
      (oldGame.HomeScore !== newGame.HomeScore));
};

var gameChangeString = function(oldGame, newGame) {
  var dif = [];
  if (oldGame.State !== newGame.State)
    dif.push(oldGame.State + '-' + newGame.State);
  if (oldGame.Period !== newGame.Period)
    dif.push(oldGame.Period + '-' + newGame.Period);
  if (oldGame.AwayScore !== newGame.AwayScore)
    dif.push(oldGame.AwayScore + '-' + newGame.AwayScore);
  if (oldGame.HomeScore !== newGame.HomeScore)
    dif.push(oldGame.HomeScore + '-' + newGame.HomeScore);
  return dif.join(',');
};

var gameChangeTweet = function(oldGame, newGame) {
  var tweet = {
    InstanceID: newGame.InstanceID,
    TweetString: ''
  };
  if (oldGame.State !== newGame.State) {
    if (oldGame.State === 'Scheduled' && newGame.State === 'Progress') {
      tweet.TweetString = 'Start of game: ' +
      newGame.AwayTeamName + ' vs ' + newGame.HomeTeamName +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Progress' && newGame.State === 'Intermission') {
      tweet.TweetString = 'End of ' + newGame.Period + '. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Intermission' && newGame.State === 'Progress') {
      tweet.TweetString = 'Start of ' + newGame.Period + '. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Progress' && newGame.State === 'Overtime') {
      tweet.TweetString = 'Headed to overtime. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Overtime' && newGame.State === 'Shootout') {
      tweet.TweetString = 'Headed to a shootout. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (newGame.State === 'Final') {
      tweet.TweetString = 'Final ' + newGame.Period + ' ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
  }
  if (oldGame.AwayScore !== newGame.AwayScore) {
    tweet.TweetString = newGame.AwayTeamName + ' score. ' +<commit_msg>differentiate regular final from OT and SO final<commit_after>var http = require('http');

var leagueInfo = {
  leagueName: 'NHL',
  updateURL: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp'
};

var getGameArray = function(next) {
  var rawData = '';
  var request = http.get(leagueInfo.updateURL, function httpSetup(res) {
    res.on('data', function httpData(chunk) {
      rawData += chunk;
    });
    res.on('end', function httpEnd() {
      try {
        rawData = rawData.replace('loadScoreboard(','');
        rawData = rawData.substring(0,(rawData.length-2));
        var rawArray = JSON.parse(rawData).games;
        var gameArray = [];
        for (var i = 0; i < rawArray.length; i++) {
          gameArray.push(parseRawGame(rawArray[i]));
        }
        next(null, gameArray);
      } catch(e) {
        e.source = 'NHL';
        next(e);
      }
    });
  }).on('error', function httpError(e) {
    console.log('NHL http Error: ' + e.message);
  });
};

var parseRawGame = function(rawGame) {
  var game = { };
  var date = new Date();
  var gameState = '';
  var timeString = '';
  var periodString = '';
  switch (rawGame.gs) {
    case '1':
      gameState = 'Scheduled';
      var arr = rawGame.ts.split(' ');
      var dateArr = arr[arr.length-1].split('/');
      date.setMonth(parseInt(dateArr[0])-1);
      date.setDate(parseInt(dateArr[1]));
      timeString = rawGame.bs;
      break;
    case '2':
      gameState = 'Scheduled';
      break;
    case '3':
      if (rawGame.ts.indexOf('END') !== -1) {
        gameState = 'Intermission';
      } else {
        gameState = 'Progress';
      }
      timeString = rawGame.ts.split(' ')[0];
      periodString = rawGame.ts.split(' ')[1];
      break;
    case '4':
      if (rawGame.ts.indexOf('SHOOTOUT') !== -1) {
        gameState = 'Shootout';
      } else {
        timeString = rawGame.ts.split(' ')[0];
        periodString = rawGame.ts.split(' ')[1];
        if (periodString === 'OT') {
          gameState = 'Overtime';
        } else if (periodString === 'SO') {
          gameState = 'Shootout';
        } else {
          gameState = 'Progress';
        }
      }
      break;
    case '5':
      gameState = 'Ended';
      var arr = rawGame.ts.split(' ');
      var dateArr = arr[arr.length-1].split('/');
      date.setMonth(parseInt(dateArr[0])-1);
      date.setDate(parseInt(dateArr[1]));
      break;
  }
  game.GameSymbol = rawGame.id;
  game.Date = date;
  game.State = gameState;
  game.Time = timeString;
  game.Period = periodString;
  game.AwayTeamCity = rawGame.atn;
  game.AwayTeamName = rawGame.atv;
  game.AwayScore = (rawGame.ats === '' ? 0 : parseInt(rawGame.ats));
  game.HomeTeamCity = rawGame.htn;
  game.HomeTeamName = rawGame.htv;
  game.HomeScore = (rawGame.hts === '' ? 0 : parseInt(rawGame.hts));
  game.RawInstance = JSON.stringify(rawGame,null,2);
  return game;
};

var gameChanged = function(oldGame, newGame) {
  return (oldGame.GameSymbol === newGame.GameSymbol) &&
    ( (oldGame.State !== newGame.State) ||
      (oldGame.Period !== newGame.Period) ||
      (oldGame.AwayScore !== newGame.AwayScore) ||
      (oldGame.HomeScore !== newGame.HomeScore));
};

var gameChangeString = function(oldGame, newGame) {
  var dif = [];
  if (oldGame.State !== newGame.State)
    dif.push(oldGame.State + '-' + newGame.State);
  if (oldGame.Period !== newGame.Period)
    dif.push(oldGame.Period + '-' + newGame.Period);
  if (oldGame.AwayScore !== newGame.AwayScore)
    dif.push(oldGame.AwayScore + '-' + newGame.AwayScore);
  if (oldGame.HomeScore !== newGame.HomeScore)
    dif.push(oldGame.HomeScore + '-' + newGame.HomeScore);
  return dif.join(',');
};

var gameChangeTweet = function(oldGame, newGame) {
  var tweet = {
    InstanceID: newGame.InstanceID,
    TweetString: ''
  };
  if (oldGame.State !== newGame.State) {
    if (oldGame.State === 'Scheduled' && newGame.State === 'Progress') {
      tweet.TweetString = 'Start of game: ' +
      newGame.AwayTeamName + ' vs ' + newGame.HomeTeamName +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Progress' && newGame.State === 'Intermission') {
      tweet.TweetString = 'End of ' + newGame.Period + '. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Intermission' && newGame.State === 'Progress') {
      tweet.TweetString = 'Start of ' + newGame.Period + '. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Progress' && newGame.State === 'Overtime') {
      tweet.TweetString = 'Headed to overtime. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (oldGame.State === 'Overtime' && newGame.State === 'Shootout') {
      tweet.TweetString = 'Headed to a shootout. ' +
      newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
      newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
      makeGameLink(newGame);
    }
    if (newGame.State === 'Final') {
      if (newGame.Period !== '') {
        tweet.TweetString = 'Final ' + newGame.Period + '. ' +
        newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
        newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
        makeGameLink(newGame);
      } else {
        tweet.TweetString = 'Final. ' +
        newGame.AwayTeamName + ': ' + newGame.AwayScore + ', ' +
        newGame.HomeTeamName + ': ' + newGame.HomeScore + ' ' +
        makeGameLink(newGame);
      }      
    }
  }
  if (oldGame.AwayScore !== newGame.AwayScore) {
    tweet.TweetString = newGame.AwayTeamName + ' score. ' +
{'fraction_non_alphanumeric': 0.1721096751659099, 'fraction_numerical': 0.0045406915822563745, 'mean_word_length': 2.6556016597510372, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1662386', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3939, 'n_words': 669}