const request = require('request-promise')
const cheerio = require('cheerio')
const url = require('url')
const downloadArticle = require('./downloadArticle')

const parsers = {}

const patterns = {}

function getMedia (hostname) {
  for (const id in patterns) {
    if (patterns[id].test(hostname)) {
      return id
    }
  }
  return null
}

/**
 * Get the content of an article given its URI
 *
 * @param {string} uri - URI of the news article.
 *
 * @return {Promise<string>} Promise of the HTML
 */
module.exports = function getContent (uri) {
  // Decide if use the new "getContent" function or the old one
  const media = getMedia(url.parse(uri).hostname)

  if (media) {
    return oldGetContent(uri)
  } else {
    return newGetContent(uri)
  }
}

/**
 * @private
 *
 * Get the content of an article given its URI
 *
 * @param {string} uri - URI of the news article.
 *
 * @return {Promise<string>} Promise of the HTML
 */
function newGetContent (uri) {
  return downloadArticle(uri)
    .then(article => article.html)
}

/**
 * @private
 * @deprecated since version 1.3. This will be replaced with "getContent"
 *
 * Get the content of an article given its URI
 *
 * @param {string} uri - URI of the news article.
 *
 * @return {Promise<string>} Promise of the HTML
 */
function oldGetContent (uri) {
  const media = getMedia(url.parse(uri).hostname)

  if (!media) {
    throw new Error('The provided URI do not match with any known media')
  }

  const options = {
    transform: function (body) {
      return cheerio.load(body)
    },
    uri
  }

  return request(options)
    .then(parsers[media])
}

@@ -3,1614 +3,324 @@
 nst 
-request = require('request-promise')%0Aconst cheerio = require('cheerio')%0Aconst url = require('url')%0Aconst downloadArticle = require('./downloadArticle')%0A%0Aconst parsers = %7B%7D%0A%0Aconst patterns = %7B%7D%0A%0Afunction getMedia (hostname) %7B%0A  for (const id in patterns) %7B%0A    if (patterns%5Bid%5D.test(hostname)) %7B%0A      return id%0A    %7D%0A  %7D%0A  return null%0A%7D%0A%0A/**%0A * Get the content of an article given its URI%0A *%0A * @param %7Bstring%7D uri - URI of the news article.%0A *%0A * @return %7BPromise%3Cstring%3E%7D Promise of the HTML%0A */%0Amodule.exports = function getContent (uri) %7B%0A  // Decide if use the new %22getContent%22 function or the old one%0A  const media = getMedia(url.parse(uri).hostname)%0A%0A  if (media) %7B%0A    return oldGetContent(uri)%0A  %7D else %7B%0A    return newGetContent(uri)%0A  %7D%0A%7D%0A%0A/**%0A * @private%0A *%0A * Get the content of an article given its URI%0A *%0A * @param %7Bstring%7D uri - URI of the news article.%0A *%0A * @return %7BPromise%3Cstring%3E%7D Promise of the HTML%0A */%0Afunction newGetContent (uri) %7B%0A  return downloadArticle(uri)%0A    .then(article =%3E article.html)%0A%7D%0A%0A/**%0A * @private%0A * @deprecated since version 1.3. This will be replaced with %22getContent%22%0A *%0A * Get the content of an article given its URI%0A *%0A * @param %7Bstring%7D uri - URI of the news article.%0A *%0A * @return %7BPromise%3Cstring%3E%7D Promise of the HTML%0A */%0Afunction oldGetContent (uri) %7B%0A  const media = getMedia(url.parse(uri).hostname)%0A%0A  if (!media) %7B%0A    throw new Error('The provided URI do not match with any known media')%0A  %7D%0A%0A  const options = %7B%0A    transform: function (body) %7B%0A      return cheerio.load(body)%0A    %7D,%0A    uri%0A  %7D%0A%0A  return request(options)%0A    .then(parsers%5Bmedia%5D
+downloadArticle = require('./downloadArticle')%0A%0A/**%0A * Get the content of an article given its URI%0A *%0A * @param %7Bstring%7D uri - URI of the news article.%0A *%0A * @return %7BPromise%3Cstring%3E%7D Promise of the HTML%0A */%0Amodule.exports = function getContent (uri) %7B%0A  return downloadArticle(uri)%0A    .then(article =%3E article.html
 )%0A%7D%0A

Remove old "getContent"