level 1
我最爱诸葛亮
楼主
之前没接触过ruby,今天突然老板甩过来个活,自己看了下,应该是个搜索结果采集的东西。想请大家帮忙看下具体的输出是什么,是多个结果还是一个?包括的内容是title和url么?因为后续可能不大会用到ruby,所以就直接当伸手党了,望包容一下下。。
require 'google/api_client'
class SearchProvider::Google < SearchProvider::Provider
def self.provider_name
"Google Search"
end
def self.options
{
:cx=> {name: "Custom Search ID (cx)",
description: "Custom search engine id (default seaches entire web)",
required: false
},
:site => { name: "Limit to Site",
description: "Allows limiting to a specific domain",
required: false
},
:days_to_search => { name: "Max result age (days)",
description: "Limit the age of the searched results (days)",
required: false
}
}
end
def initialize(query, options={})
super
@google_developer_key = Rails.configuration.try(:google_developer_key)
@cx = options[:cx].present? ? options[:cx] : Rails.configuration.try(:google_cx)
@application_name = Rails.configuration.try(:google_application_name)
@application_version = Rails.configuration.try(:google_application_verion)
@site_search = options[:site_search].present? ? options[:site_search] : nil
end
def run
if(@google_developer_key.blank?)
Rails.logger.error "Unable to search Google. No developer key. Please define an developer key as google_developer_key in the Scumblr initializer."
return []
end
if(@cx.blank?)
Rails.logger.error "Unable to search Google. No cx. Please define a cx as google_cx in the Scumblr initializer or pass in as a search option."
return []
end
results =[]
client = Google::APIClient.new(:key => @google_developer_key, :authorization => nil, :application_name=>@application_name, :application_version=>@application_version)
search = client.discovered_api('customsearch')
(1..100).step(100) do |offset|
# Make an API call using a reference to a discovered method.
parameters = {
'q' => @query,
'key' => @google_developer_key,
'cx' => @cx,
'siteSearch' => @site_search,
'start' => offset
}
if(@options[:days_to_search].present?)
parameters['dateRestrict'] = "d#{@options[:days_to_search]}"
end
response = client.execute(
:api_method => search.cse.list,
:parameters => parameters
)
Rails.logger.warn "Response received #{response}"
results += parse_response(response)
end
return results
end
private
def parse_response(response)
begin
results_json = Oj.load(response.body)['items']
results = []
results_json.each do |result|
results << {title: result["title"], url: result["link"], domain: result["displayLink"]}
end
results
rescue
[]
end
end
end
2014年12月03日 16点12分
1
require 'google/api_client'
class SearchProvider::Google < SearchProvider::Provider
def self.provider_name
"Google Search"
end
def self.options
{
:cx=> {name: "Custom Search ID (cx)",
description: "Custom search engine id (default seaches entire web)",
required: false
},
:site => { name: "Limit to Site",
description: "Allows limiting to a specific domain",
required: false
},
:days_to_search => { name: "Max result age (days)",
description: "Limit the age of the searched results (days)",
required: false
}
}
end
def initialize(query, options={})
super
@google_developer_key = Rails.configuration.try(:google_developer_key)
@cx = options[:cx].present? ? options[:cx] : Rails.configuration.try(:google_cx)
@application_name = Rails.configuration.try(:google_application_name)
@application_version = Rails.configuration.try(:google_application_verion)
@site_search = options[:site_search].present? ? options[:site_search] : nil
end
def run
if(@google_developer_key.blank?)
Rails.logger.error "Unable to search Google. No developer key. Please define an developer key as google_developer_key in the Scumblr initializer."
return []
end
if(@cx.blank?)
Rails.logger.error "Unable to search Google. No cx. Please define a cx as google_cx in the Scumblr initializer or pass in as a search option."
return []
end
results =[]
client = Google::APIClient.new(:key => @google_developer_key, :authorization => nil, :application_name=>@application_name, :application_version=>@application_version)
search = client.discovered_api('customsearch')
(1..100).step(100) do |offset|
# Make an API call using a reference to a discovered method.
parameters = {
'q' => @query,
'key' => @google_developer_key,
'cx' => @cx,
'siteSearch' => @site_search,
'start' => offset
}
if(@options[:days_to_search].present?)
parameters['dateRestrict'] = "d#{@options[:days_to_search]}"
end
response = client.execute(
:api_method => search.cse.list,
:parameters => parameters
)
Rails.logger.warn "Response received #{response}"
results += parse_response(response)
end
return results
end
private
def parse_response(response)
begin
results_json = Oj.load(response.body)['items']
results = []
results_json.each do |result|
results << {title: result["title"], url: result["link"], domain: result["displayLink"]}
end
results
rescue
[]
end
end
end