PageRenderTime 31ms CodeModel.GetById 26ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ddg.coffee

https://gitlab.com/raulhc/telegram-bot
CoffeeScript | 66 lines | 59 code | 5 blank | 2 comment | 7 complexity | 2f09751d012bb95abae542b0b51897ea MD5 | raw file
 1{log:logger, round2, html2txt, inspect, timeout} = require './util'
 2{parse:parseURL} = require 'url'
 3{request} = require '../lib/request'
 4
 5# Busca no DuckDuckGo
 6# http://api.duckduckgo.com/?q=<query>&format=json&pretty=1
 7module.exports = ddg = (query, langs, callback=(->))->
 8  logger "DDG Search for \"#{query}\", Open API..."
 9  encQuery = encodeURIComponent query
10  url = 'http://api.duckduckgo.com/?format=json&q=' + encQuery
11  request(url, 'Accept-Language': ddg.mkAcceptLang langs)
12    .then (res)->
13      try
14        logger.debug 'DDG response data', data:res.data
15        data = JSON.parse res.data
16        result = data.Results?[0] or {}
17        resultUrl = data.DefinitionURL or data.AbstractURL or result.FirstURL
18        resultTxt = data.Definition or data.AbstractText or result.Text
19        if resultUrl and resultTxt
20          callback null, url: resultUrl, content: resultTxt
21        else
22          callback new Error 'no result.'
23      catch err
24        logger.error 'DDG data parser fail', err
25        callback err
26    .catch (err)->
27      callback err
28
29ddg.mkAcceptLang = (langs...)->
30  langs = langs[0] if langs[0].constructor is Array
31  qval = 1.1
32  ( "#{lang};q=#{round2 qval-=.1}" for lang in langs ).join ','
33
34ddg.siteAPI = (query, langs, callback=(->))->
35  logger "DDG Search for \"#{query}\", Site API..."
36  lang = ddg.mkQueryLang langs[0]
37  encQuery = encodeURIComponent query
38  url = "https://duckduckgo.com/d.js?q=#{encQuery}&l=#{lang}&norw=1"
39  headers = {
40    'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:46.0) Gecko/20150101 Firefox/46.0'
41    'Accept-Language': ddg.mkAcceptLang langs
42    'Accept-Encoding': 'gzip'
43    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
44    'Connection': 'Keep-Alive'
45  }
46  request(url, headers)
47    .then (res)->
48      if 0 <= res.data.indexOf '"c":"http://www.google.com/search?q="+q'
49        throw Error 'no result. (blocked)'
50      unless match = res.data.split(/\s+/).join(' ').match /nrn\('d',(\[\{.*\}\])\)/
51        throw Error 'DDG Site API Changed? (nrn)'
52      result = JSON.parse(match[1])[0]
53      unless result.a? and result.t? and result.c?.match /^https?:/
54        throw Error "DDG Site API Changed? (#{inspect result})"
55      if result.c.match /google.com\/search\?.*q=/
56        throw Error 'no result.'
57      timeout 0, -> callback null, title: html2txt(result.t), url: result.c, content: html2txt(result.a)
58    .catch (err)-> timeout 0, -> callback err
59
60ddg.mkQueryLang = (lang)->
61  lang = lang.toLowerCase().split '-'
62  if lang.length is 2
63    lang[1] + '-' + lang[0]
64  else
65    lang[0]
66