<commit_before>/**
 * Module with helpers for getting listings from the Rovi API
 * https://github.com/radiantnode/alexa-dtv-tuner
 */

'use strict';

var _          = require('lodash'),
    config     = require('../config/config'),
    mysql      = require('mysql'),
    connection = mysql.createConnection(config.database),
    moment     = require('moment'),
    Promise    = require("bluebird"),
    channels   = require('./channels');

module.exports = function (config) {
  var module = {
    errorTypes: {
      SHOW_NOT_FOUND: 404,
      CHANNEL_NOT_FOUND: 405
    }
  };

  module.findShow = function (show_name)
  {
    return new Promise(function(resolve, reject){
      getListings(show_name)
        .then(function(airings){
          if(!airings.length > 0) return reject(module.errorTypes.SHOW_NOT_FOUND);

          var latestAiring = airings.shift(),
              showTime     = moment(latestAiring.AiringTime),
              otherAirings = airings,
              channelInfo  = channels.getChannel(latestAiring.ChannelNumber);

          if(!channelInfo) return reject(module.errorTypes.CHANNEL_NOT_FOUND);

          resolve({
            title:       latestAiring.Title,
            description: latestAiring.Copy,
            channel:     channelInfo,
            airing: {
              now:   showTime.isBefore(moment()),
              human: showTime.fromNow()
            },

            otherAirings: otherAirings.length > 0 ? _.map(otherAirings, function(other){
              return moment(other.AiringTime).format('hA')
            }) : null

          });

        });

    });

  }

  // private

  function getListings (show_name)
  {
    return new Promise(function(resolve, reject){
      connection.query(
        "SELECT * FROM entries WHERE Title LIKE '%?%' AND (AiringTime > NOW() OR DATE_ADD(AiringTime, INTERVAL Duration MINUTE) > NOW()) GROUP BY AiringTime ORDER BY AiringTime ASC",
        [show_name],
        function(err, results){
          if(err) reject(err)
          else    resolve(results)
        }

      );
    });

  }

  return module;
};<commit_msg>escaping<commit_after>/**
 * Module with helpers for getting listings from the Rovi API
 * https://github.com/radiantnode/alexa-dtv-tuner
 */

'use strict';

var _          = require('lodash'),
    config     = require('../config/config'),
    mysql      = require('mysql'),
    connection = mysql.createConnection(config.database),
    moment     = require('moment'),
    Promise    = require("bluebird"),
    channels   = require('./channels');

module.exports = function (config) {
  var module = {
    errorTypes: {
      SHOW_NOT_FOUND: 404,
      CHANNEL_NOT_FOUND: 405
    }
  };

  module.findShow = function (show_name)
  {
    return new Promise(function(resolve, reject){
      getListings(show_name)
        .then(function(airings){
          if(!airings.length > 0) return reject(module.errorTypes.SHOW_NOT_FOUND);

          var latestAiring = airings.shift(),
              showTime     = moment(latestAiring.AiringTime),
              otherAirings = airings,
              channelInfo  = channels.getChannel(latestAiring.ChannelNumber);

          if(!channelInfo) return reject(module.errorTypes.CHANNEL_NOT_FOUND);

          resolve({
            title:       latestAiring.Title,
            description: latestAiring.Copy,
            channel:     channelInfo,
            airing: {
              now:   showTime.isBefore(moment()),
              human: showTime.fromNow()
            },

            otherAirings: otherAirings.length > 0 ? _.map(otherAirings, function(other){
              return moment(other.AiringTime).format('hA')
            }) : null

          });

        });

    });

  }

  // private

  function getListings (show_name)
  {
    return new Promise(function(resolve, reject){
      connection.query(
        "SELECT * FROM entries WHERE Title LIKE ? AND (AiringTime > NOW() OR DATE_ADD(AiringTime, INTERVAL Duration MINUTE) > NOW()) GROUP BY AiringTime ORDER BY AiringTime ASC",
        ['%' + show_name + '%'],
        function(err, results){
          if(err) reject(err)
          else    resolve(results)
        }

      );
    });

  }

  return module;
};
{'fraction_non_alphanumeric': 0.11851149561507467, 'fraction_numerical': 0.003792367859682389, 'mean_word_length': 2.1897203325774752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6765182', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1185, 'n_words': 246}