Class: Greeenboii::Search

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

Constant Summary collapse

SEARCH_ENGINES =
{
  "Google" => "https://www.google.com/search?client=opera-gx&q= ",
  "Bing" => "https://www.bing.com/search?q=",
  "DuckDuckGo" => "https://duckduckgo.com/?q="
}.freeze

Class Method Summary collapse

Class Method Details

.display_results(results) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/greeenboii.rb', line 324

def self.display_results(results)
  CLI::UI.frame_style = :bracket
  CLI::UI::Frame.open(CLI::UI.fmt("{{green:Search Results}}")) do
    results.each do |result|
      puts CLI::UI.fmt("{{v}} {{cyan:#{result[:engine]}}}")
      result[:links].each_with_index do |link, idx|
        puts CLI::UI.fmt("  {{*}} ##{idx + 1}: #{link}")
      end
    end
  end
end

.perform_searchObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/greeenboii.rb', line 243

def self.perform_search
  query = CLI::UI.ask("Enter your search query:", default: "")
  return if query.empty?

  results = []
  CLI::UI::SpinGroup.new do |spin_group|
    SEARCH_ENGINES.each do |engine, base_url|
      spin_group.add("Searching #{engine}...") do |spinner|
        suffix = case engine
                 when "Google"
                   "&sourceid=opera&ie=UTF-8&oe=UTF-8"
                 when "Bing"
                   "&sp=-1&pq=test&sc=6-4&qs=n&sk=&cvid=#{SecureRandom.hex(16)}"
                 when "DuckDuckGo"
                   "&t=h_&ia=web"
                 end
        search_url = "#{base_url}#{URI.encode_www_form_component(query)}#{suffix}"
        links = scrape_links(engine, search_url)
        results << { engine: engine, links: links }
        sleep 1.0 # Simulating search delay
        spinner.update_title("#{engine} search complete!")
      end
    end
  end

  display_results(results)
end


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/greeenboii.rb', line 271

def self.scrape_links(engine, url)
  headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
    "Accept-Language" => "en-US,en;q=0.5"
  }

  response = HTTParty.get(url, headers: headers)
  puts "Debug: HTTP Status: #{response.code}"

  doc = Nokogiri::HTML(response.body)
  puts "Debug: Page title: #{doc.title}"

  results = []

  case engine
  when "Google"
    doc.css("div.g").each do |result|
      link = result.css(".yuRUbf > a").first
      next unless link

      title = result.css("h3").text.strip
      url = link["href"]
      result.css(".VwiC3b").text.strip

      puts "Debug: Found Google result - Title: #{title}"
      results << url if url.start_with?("http")
    end

  when "Bing"
    doc.css("#b_results li.b_algo").each do |result|
      link = result.css("h2 a").first
      next unless link

      url = link["href"]
      puts "Debug: Found Bing result - URL: #{url}"
      results << url if url.start_with?("http")
    end

  else # DuckDuckGo
    doc.css(".result__body").each do |result|
      link = result.css(".result__title a").first
      next unless link

      url = link["href"]
      puts "Debug: Found DuckDuckGo result - URL: #{url}"
      results << url if url.start_with?("http")
    end
  end

  puts "Debug: Total results found: #{results.length}"
  results.take(8)
end