<commit_before>var Promise = require('promise');
var log4js = require('log4js');
var logger = log4js.getLogger();
var SlackWebhook = require('slack-webhook');
var slack = process.env.SLACK_WEBHOOK ? new SlackWebhook(process.env.SLACK_WEBHOOK, {
  defaults: {
    username: 'JukeBot'
  }
}) : null;

module.exports = {
  addVideo: addVideo,
  sendAddMessages: sendAddMessages
};

function addVideo(video) {
  logger.info('Adding video ' + video.key)
  return new Promise(function(resolve, reject) {
    Video.findOne({
      playing: true
    }).exec(function(err, current) {
      if (!current) {
        video.startTime = new Date();
        video.playing = true;
        video.played = true;
        setTimeout(endCurrentVideo, video.durationSeconds * 1000);
        video.save(function() {
          resolve(video);
        });
      } else {
        resolve(video);
      }
    });
  });
}

function sendAddMessages(video) {
  return new Promise(function(resolve, reject) {
    Video.publishCreate(video);
    if (slack) {
      slack.send({
        text: '@' + video.user + ' added a song to the playlist' + (video.playing ? ' and it\'s playing now' : '') + '! <' + sails.config.serverUrl + '|Listen to JukeBot>',
        attachments: [formatSlackAttachment(video)]
      }).then(function() {
        resolve(video);
      });
    } else {
      resolve(video);
    }
  });
}

function formatSlackAttachment(video) {
  return {
    title: video.title,
    title_link: 'https://www.youtube.com/watch?v=' + video.key,
    thumb_url: video.thumbnail
  };
}

function endCurrentVideo() {
  Video.findOne({
    playing: true
  }).exec(function(err, current) {
    current.playing = false;
    current.save(function() {
      logger.info('Publishing end song ' + current.key);
      Video.publishUpdate(current.id, current);
      findNextVideo();
    });
  });
}

function findNextVideo() {
  Video.find({
    played: false
  }).sort('createdAt ASC').exec(function(err, upcoming) {
    if (upcoming.length > 0) {
      startVideo(upcoming[0]);
    }
  });
}

function startVideo(video) {
  video.playing = true;
  video.played = true;
  video.startTime = new Date();
  logger.info('Setting timeout');
  setTimeout(endCurrentVideo, video.durationSeconds * 1000);
  video.save(function() {
    logger.info('Stopping video ' + video.key);
    Video.publishUpdate(video.id, video);
    if (slack) {
      slack.send({
        text: '*' + video.title + '* is now playing! <' + sails.config.serverUrl + '|Listen to JukeBot>',
        'mrkdwn': true
      }).then(function() {
        logger.info('Started playing video ' + video.key);
      });
    } else {
      logger.info('Started playing video ' + video.key);
    }
  });
}<commit_msg>add a catch statement here<commit_after>var Promise = require('promise');
var log4js = require('log4js');
var logger = log4js.getLogger();
var SlackWebhook = require('slack-webhook');
var slack = process.env.SLACK_WEBHOOK ? new SlackWebhook(process.env.SLACK_WEBHOOK, {
  defaults: {
    username: 'JukeBot'
  }
}) : null;

module.exports = {
  addVideo: addVideo,
  sendAddMessages: sendAddMessages
};

function addVideo(video) {
  logger.info('Adding video ' + video.key)
  return new Promise(function(resolve, reject) {
    Video.findOne({
      playing: true
    }).exec(function(err, current) {
      if (!current) {
        video.startTime = new Date();
        video.playing = true;
        video.played = true;
        setTimeout(endCurrentVideo, video.durationSeconds * 1000);
        video.save(function() {
          resolve(video);
        });
      } else {
        resolve(video);
      }
    });
  });
}

function sendAddMessages(video) {
  return new Promise(function(resolve, reject) {
    Video.publishCreate(video);
    if (slack) {
      slack.send({
        text: '@' + video.user + ' added a song to the playlist' + (video.playing ? ' and it\'s playing now' : '') + '! <' + sails.config.serverUrl + '|Listen to JukeBot>',
        attachments: [formatSlackAttachment(video)]
      }).then(function() {
        resolve(video);
      });
    } else {
      resolve(video);
    }
  });
}

function formatSlackAttachment(video) {
  return {
    title: video.title,
    title_link: 'https://www.youtube.com/watch?v=' + video.key,
    thumb_url: video.thumbnail
  };
}

function endCurrentVideo() {
  Video.findOne({
    playing: true
  }).exec(function(err, current) {
    current.playing = false;
    current.save(function() {
      logger.info('Publishing end song ' + current.key);
      Video.publishUpdate(current.id, current);
      findNextVideo();
    });
  });
}

function findNextVideo() {
  Video.find({
    played: false
  }).sort('createdAt ASC').exec(function(err, upcoming) {
    if (upcoming.length > 0) {
      startVideo(upcoming[0]);
    }
  });
}

function startVideo(video) {
  video.playing = true;
  video.played = true;
  video.startTime = new Date();
  logger.info('Setting timeout');
  setTimeout(endCurrentVideo, video.durationSeconds * 1000);
  video
    .save(() => {
      logger.info('Stopping video ' + video.key);
      Video.publishUpdate(video.id, video);
      if (slack) {
        slack.send({
          text: '*' + video.title + '* is now playing! <' + sails.config.serverUrl + '|Listen to JukeBot>',
          'mrkdwn': true
        }).then(function() {
          logger.info('Started playing video ' + video.key);
        });
      } else {
        logger.info('Started playing video ' + video.key);
      }
    })
    .catch((e) => logger.info(`Unable to save video ${video.key} - ${e}`);
}
{'fraction_non_alphanumeric': 0.1553485038523562, 'fraction_numerical': 0.004658663321985307, 'mean_word_length': 2.865650969529086, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2944520', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1775, 'n_words': 356}