PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/ducky_search.rb

https://github.com/alexshook/ducky_search
Ruby | 39 lines | 33 code | 3 blank | 3 comment | 3 complexity | 605cdece69eec6610909946740b1c885 MD5 | raw file
  1. module DuckySearch
  2. Capybara.current_driver = :webkit
  3. Capybara.app_host = "https://duckduckgo.com/"
  4. class Scraper
  5. include Capybara::DSL
  6. def scrape_duck_duck_go(query_string)
  7. visit '/'
  8. fill_in 'q', with: query_string
  9. # click magnifying class, value='S'
  10. click_link_or_button 'S'
  11. # starting return results as JSON
  12. results_array = []
  13. limit_results = 20
  14. result_number = 0
  15. # get all results by div class, return title, snippet, url
  16. all('div.result__body').each do |result|
  17. links = []
  18. new_obj = {
  19. id: result_number,
  20. title: result.find('h2.result__title').text,
  21. content: result.find('div.result__snippet').text,
  22. url: result.find('h2.result__title a.result__a')[:href]
  23. }
  24. results_array << new_obj
  25. result_number += 1
  26. break if result_number == limit_results
  27. end
  28. return results_array
  29. end
  30. def get_topic_summary(query_string)
  31. results = HTTParty.get("http://api.duckduckgo.com/?q=#{query_string}&format=json")
  32. abstract = JSON.parse(results)['AbstractText']
  33. return abstract.size > 3 ? abstract : "Sorry, a summary is not available for this topic."
  34. end
  35. end
  36. end