Class: BulkSearch::Processor
- Inherits:
-
Object
- Object
- BulkSearch::Processor
- Defined in:
- lib/bulksearch/processor.rb
Overview
The processor fills in empty spreadsheet cells with results from Bing and/or Yahoo BOSS searches.
Instance Attribute Summary collapse
-
#providers ⇒ Object
A list of search providers to use with the processor (:bing, :boss).
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Processor
constructor
Constructor.
-
#process(file) ⇒ Object
Processes the file using the appropriate search services.
Constructor Details
#initialize(options = {}) ⇒ Processor
Constructor
11 12 13 |
# File 'lib/bulksearch/processor.rb', line 11 def initialize(={}) self.providers = [:providers] || [] end |
Instance Attribute Details
#providers ⇒ Object
A list of search providers to use with the processor (:bing, :boss).
23 24 25 |
# File 'lib/bulksearch/processor.rb', line 23 def providers @providers end |
Instance Method Details
#process(file) ⇒ Object
Processes the file using the appropriate search services.
35 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bulksearch/processor.rb', line 35 def process(file) file = File.(file) # Setup clients. bing_client = Binged::Client.new() # Open the spreadsheet. book = Spreadsheet.open(file) sheet = book.worksheet(0) # Read the headers. headers = sheet.row(0).map { |cell| cell.to_s } begin # Loop over the remaining rows and sheet.each(1) do |row| # Determine base options. = {} headers.each_with_index do |header, index| case header when 'TERMS' then [:terms] = row[index].to_s when 'SITE' then [:site] = row[index].to_s end end # Perform searches on the rest of the columns. headers.each_with_index do |header, index| next unless header.index(/^[A-Z]+$/).nil? # Combine search terms. terms = "#{[:terms].to_s} #{header}" # Only update the field if it's blank. if row[index].to_s =~ /^\s*$/ # Process using Bing. if providers.index(:bing) puts "? #{terms} (#{[:site]})" search = bing_client.web.containing(terms) search.from_site([:site]) unless [:site].nil? result = search.fetch.results[0..2].select {|result| URI.parse(result.url).path != '/' rescue false}.first if !result.nil? puts "#{result.url}\n\n" row[index] = result.url else puts "" end end # Process using Yahoo BOSS. if providers.index(:boss) puts "BOSS: #{terms}" BOSSMan::Search.web(terms, :count => 1) search = boss_client.web.containing(terms) result = search.results.first if !result.nil? puts "#{result.url}\n\n" row[index] = result.url else puts "" end end end end end # If an error occurs, save off the file before exiting. ensure # Backup original file & write over original file. FileUtils.mv(file, "#{File.dirname(file)}/.#{File.basename(file)}") book.write(file) end end |