Class: DuckSearch::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/duck_search/client.rb

Constant Summary collapse

BASE_URL =
"https://html.duckduckgo.com"
SEARCH_PATH =
"/html"
RESULT_CAP =
5
DEFAULT_USER_AGENT =

Match ddgr’s actual User-Agent exactly

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " \
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy: nil, timeout: 15, open_timeout: 10, user_agent: DEFAULT_USER_AGENT) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
# File 'lib/duck_search/client.rb', line 18

def initialize(proxy: nil, timeout: 15, open_timeout: 10, user_agent: DEFAULT_USER_AGENT)
  @proxy        = proxy
  @timeout      = timeout
  @open_timeout = open_timeout
  @user_agent   = user_agent
end

Instance Attribute Details

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



16
17
18
# File 'lib/duck_search/client.rb', line 16

def open_timeout
  @open_timeout
end

#proxyObject (readonly)

Returns the value of attribute proxy.



16
17
18
# File 'lib/duck_search/client.rb', line 16

def proxy
  @proxy
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



16
17
18
# File 'lib/duck_search/client.rb', line 16

def timeout
  @timeout
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



16
17
18
# File 'lib/duck_search/client.rb', line 16

def user_agent
  @user_agent
end

Instance Method Details

#search(query) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/duck_search/client.rb', line 25

def search(query)
  # POST form body matching ddgr's page-0 payload
  form_data = {
    q:  query,
    b:  "",        # required blank field
    kf: "-1",      # disable favicons
    kh: "1",       # HTTPS always on
    kl: "us-en",   # region
    kp: "1",       # safe search (use -2 to disable)
    k1: "-1",      # ads off
  }

  response = connection.post(SEARCH_PATH) do |req|
    req.headers["User-Agent"]      = user_agent
    req.headers["Accept-Encoding"] = "gzip, deflate"
    req.headers["Accept"]          = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    req.headers["Accept-Language"] = "en-US,en;q=0.9"
    req.headers["DNT"]             = "1"
    req.headers["Content-Type"]    = "application/x-www-form-urlencoded"
    req.body = URI.encode_www_form(form_data)
  end

  unless response.success?
    raise HttpError.new("DuckDuckGo returned HTTP #{response.status}",
                        status: response.status,
                        url: "#{BASE_URL}#{SEARCH_PATH}")
  end

  parse_html(response.body)
rescue Faraday::Error => e
  raise HttpError.new("DuckDuckGo connection failed: #{e.message}",
                      url: "#{BASE_URL}#{SEARCH_PATH}")
end