Class: SL::DuckDuckGoSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/searches/duckduckgo.rb

Overview

DuckDuckGo Search

Class Method Summary collapse

Class Method Details

.search(search_type, search_terms, link_text) ⇒ Array

Searches DuckDuckGo for the given search terms

Parameters:

  • search_type (String)

    the type of search to perform

  • search_terms (String)

    the terms to search for

  • link_text (String)

    the text to display for the link

Returns:

  • (Array)

    an array containing the URL, title, and link text



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/searchlink/searches/duckduckgo.rb', line 36

def search(search_type, search_terms, link_text)
  return zero_click(search_terms, link_text) if search_type =~ /^z$/

  # return SL.ddg(search_terms, link_text) if search_type == 'g' && SL::GoogleSearch.api_key?

  terms = "%5C#{search_terms.url_encode}"
  page = Curl::Html.new("https://duckduckgo.com/?q=#{terms}", compressed: true)

  locs = page.meta['refresh'].match(%r{/l/\?uddg=(.*?)$})
  locs = page.body.match(%r{/l/\?uddg=(.*?)'}) if locs.nil?
  locs = page.body.match(/url=(.*?)'/) if locs.nil?

  return false if locs.nil?

  url = locs[1].url_decode.sub(/&rut=\w+/, '')

  result = url.strip.url_decode || false
  return false unless result

  return false if result =~ /internal-search\.duckduckgo\.com/

  # output_url = CGI.unescape(result)
  output_url = result

  output_title = if SL.config['include_titles'] || SL.titleize
                   SL::URL.title(output_url) || ''
                 else
                   ''
                 end

  output_url = SL.first_image(output_url) if search_type =~ /img$/

  [output_url, output_title, link_text]
end

.settingsHash

Returns a hash of settings for the DuckDuckGoSearch class

Returns:

  • (Hash)

    settings for the DuckDuckGoSearch class



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/searchlink/searches/duckduckgo.rb', line 13

def settings
  {
    trigger: '(?:g|ddg|z|ddgimg)',
    searches: [
      ['g', 'Google/DuckDuckGo Search'],
      ['ddg', 'DuckDuckGo Search'],
      ['z', 'DDG Zero Click Search'],
      ['ddgimg', 'Return the first image from the destination page']
    ]
  }
end

.zero_click(search_terms, link_text, disambiguate: false) ⇒ Array

Searches DuckDuckGo for the given search terms and returns a zero click result

Parameters:

  • search_terms (String)

    the terms to search for

  • link_text (String)

    the text to display for the link

  • disambiguate (Boolean) (defaults to: false)

    whether to disambiguate the search

Returns:

  • (Array)

    an array containing the URL, title, and link text



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/searchlink/searches/duckduckgo.rb', line 84

def zero_click(search_terms, link_text, disambiguate: false)
  search_terms.gsub!(/%22/, '"')
  d = disambiguate ? '0' : '1'
  url = "http://api.duckduckgo.com/?q=#{search_terms.url_encode}&format=json&no_redirect=1&no_html=1&skip_disambig=#{d}"
  result = Curl::Json.new(url, symbolize_names: true).json
  return SL.ddg(terms, link_text) unless result

  wiki_link = result[:AbstractURL] || result[:Redirect]
  title = result[:Heading] || false

  if !wiki_link.empty? && !title.empty?
    [wiki_link, title, link_text]
  elsif disambiguate
    SL.ddg(search_terms, link_text)
  else
    zero_click(search_terms, link_text, disambiguate: true)
  end
end