import request from 'request'
import cheerio from 'cheerio'

export default class GoogleScraper {
  constructor(options) {
    this.options = options;
  }

  get getGoogleLinks() {
    return new Promise((resolve, reject) => {
      this.getHtml().then((body) => {
        return resolve(this.extractLink(body))
      }).catch((err) => {
        return reject(err);
      })
    })
  }

  getHtml() {
    return new Promise((resolve, reject) => {
      request(`http://www.google.${this.options.tld}/search?hl=${this.options.language}&num=${this.options.results}&q=${this.options.keyword}`, (err, res, body) => {
        if (err) {
          return reject(err)
        } else if (res.statusCode !== 200) {
          const error = new Error(`Unexpected status code: ${res.statusCode}`)
          error.res = res
          return reject(error)
        }
        return resolve(body)
      })
    })
  }

  extractLink(html) { // eslint-disable-line class-methods-use-this
    const arrayLinks = [];
    const $ = cheerio.load(html);
    $('h3.r a').each((i, link) => {
      const linkClean = $(link).attr('href').match('(?=http|https).*(?=&sa)')
      if (linkClean !== null) {
        arrayLinks.push(linkClean[0]);
      }
    })
    return arrayLinks;
  }

}

@@ -52,16 +52,52 @@
 cheerio'
+%0Aimport * as config from '../config'
 %0A%0Aexport
@@ -127,16 +127,17 @@
 raper %7B%0A
+%0A
   constr
@@ -439,47 +439,22 @@
     
-return new Promise((resolve, reject)
+const option
  =
-%3E
  %7B%0A 
@@ -462,37 +462,30 @@
     
-request(%60http://www.google.$%7B
+url: config.urlSearch(
 this
@@ -500,22 +500,10 @@
 .tld
-%7D/search?hl=$%7B
+, 
 this
@@ -523,16 +523,10 @@
 uage
-%7D&num=$%7B
+, 
 this
@@ -545,14 +545,10 @@
 ults
-%7D&q=$%7B
+, 
 this
@@ -563,18 +563,91 @@
 .keyword
-%7D%60
+),%0A    %7D%0A    return new Promise((resolve, reject) =%3E %7B%0A      request(option
 , (err, 
@@ -1217,17 +1217,8 @@
 lean
- !== null
 ) %7B%0A

use config file