<commit_before>/**
 * Created by Kylart on 27/05/2017.
 */

const {si, pantsu} = require('nyaapi')
const malScraper = require('mal-scraper')
const _ = require('lodash')
const {removeUnwanted} = require('./utils')

const sendRes = (object, res) => {
  res.status(200).send(JSON.stringify(object))
}

const formatMagnets = (data, searchData, choice, res) => {
  const magnets = []
  const eps = []
  const isPantsu = choice === 'pantsu'

  data.forEach((elem) => {
    elem.name = removeUnwanted(elem.name)
    const ep = elem.name.split(' ').splice(-2, 1)[0]
    eps.push(ep)

    if (ep <= searchData.untilEp && ep >= searchData.fromEp) {
      magnets.push({
        name: elem.name,
        link: isPantsu ? elem.magnet : elem.links.magnet
      })
    }
  })

  sendRes({
    minEp: _.min(eps),
    maxEp: _.max(eps),
    magnets
  }, res)
}

const download = (req, res) => {
  req.on('data', (chunk) => {
    chunk = JSON.parse(chunk)

    const choice = chunk.choice

    console.log('[Nyaa] (Download): Received a download request. Choice is ' + choice)

    const searchData = {
      quality: chunk.quality,
      name: chunk.name,
      fansub: chunk.fansub,
      fromEp: chunk.fromEp,
      untilEp: chunk.untilEp
    }

    console.log(searchData)

    const term = `[${searchData.fansub}] ${searchData.quality} ${searchData.name} ` + (choice === 'si' ? '-unofficial' : '')

    if (choice === 'si') {
      si.search(term).then((data) => {
        formatMagnets(data, searchData, choice, res)
      }).catch(/* istanbul ignore next */(err) => {
        console.log('[Nyaa]: An error occurred...\n' + err)
        res.status(204).send()
      })
    } else {
      pantsu.search(term).then((data) => {
        formatMagnets(data, searchData, choice, res)
      }).catch(/* istanbul ignore next */(err) => {
        console.log(err.message)
        res.status(204).send()
      })
    }
  })
}

const makeSearch = (data, res, isPantsu = false) => {
  let counter = 0
  const toReturn = []

  for (let i = 0; i < 18; ++i) {
    const realName = removeUnwanted(data[i].name)
    const name = realName.split(' ').slice(1).join(' ')
    const rawName = name.split(' ').slice(0, -3).join(' ')
    const researchName = rawName.split(' ').join('').toLowerCase()
    const ep = name.split(' ').splice(-2, 1)[0]
    const link = !isPantsu ? data[i].links.magnet : data[i].magnet

    malScraper.getInfoFromName(rawName)
      .then((item) => {
        item.rawName = rawName
        item.researchName = researchName
        item.magnetLink = link
        item.ep = ep
        item.synopsis = item.synopsis.length > 170
          ? item.synopsis.slice(0, 175) + '...'
          : item.synopsis

        toReturn[i] = item<commit_msg>repaired checkSynopsis on release<commit_after>/**
 * Created by Kylart on 27/05/2017.
 */

const {si, pantsu} = require('nyaapi')
const malScraper = require('mal-scraper')
const _ = require('lodash')
const {removeUnwanted} = require('./utils')

const sendRes = (object, res) => {
  res.status(200).send(JSON.stringify(object))
}

const formatMagnets = (data, searchData, choice, res) => {
  const magnets = []
  const eps = []
  const isPantsu = choice === 'pantsu'

  data.forEach((elem) => {
    elem.name = removeUnwanted(elem.name)
    const ep = elem.name.split(' ').splice(-2, 1)[0]
    eps.push(ep)

    if (ep <= searchData.untilEp && ep >= searchData.fromEp) {
      magnets.push({
        name: elem.name,
        link: isPantsu ? elem.magnet : elem.links.magnet
      })
    }
  })

  sendRes({
    minEp: _.min(eps),
    maxEp: _.max(eps),
    magnets
  }, res)
}

const download = (req, res) => {
  req.on('data', (chunk) => {
    chunk = JSON.parse(chunk)

    const choice = chunk.choice

    console.log('[Nyaa] (Download): Received a download request. Choice is ' + choice)

    const searchData = {
      quality: chunk.quality,
      name: chunk.name,
      fansub: chunk.fansub,
      fromEp: chunk.fromEp,
      untilEp: chunk.untilEp
    }

    console.log(searchData)

    const term = `[${searchData.fansub}] ${searchData.quality} ${searchData.name} ` + (choice === 'si' ? '-unofficial' : '')

    if (choice === 'si') {
      si.search(term).then((data) => {
        formatMagnets(data, searchData, choice, res)
      }).catch(/* istanbul ignore next */(err) => {
        console.log('[Nyaa]: An error occurred...\n' + err)
        res.status(204).send()
      })
    } else {
      pantsu.search(term).then((data) => {
        formatMagnets(data, searchData, choice, res)
      }).catch(/* istanbul ignore next */(err) => {
        console.log(err.message)
        res.status(204).send()
      })
    }
  })
}

const makeSearch = (data, res, isPantsu = false) => {
  let counter = 0
  const toReturn = []

  for (let i = 0; i < 18; ++i) {
    const realName = removeUnwanted(data[i].name)
    const name = realName.split(' ').slice(1).join(' ')
    const rawName = name.split(' ').slice(0, -3).join(' ')
    const researchName = rawName.split(' ').join('').toLowerCase()
    const ep = name.split(' ').splice(-2, 1)[0]
    const link = !isPantsu ? data[i].links.magnet : data[i].magnet

    malScraper.getInfoFromName(rawName)
      .then((item) => {
        item.rawName = rawName
        item.researchName = researchName
        item.magnetLink = link
        item.ep = ep
        item.fullSynopsis = item.synopsis
        item.synopsis = item.synopsis.length > 170
          ? item.synopsis.slice(0, 175) + '...'
          : item.synopsis

        toReturn[i] = item
{'fraction_non_alphanumeric': 0.17305605786618444, 'fraction_numerical': 0.013381555153707052, 'mean_word_length': 2.5938921377517867, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5124776', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1894, 'n_words': 404}