lib/blekko-search/blekko.rb in blekko-search-0.0.1 vs lib/blekko-search/blekko.rb in blekko-search-0.0.2

- old
+ new

@@ -1,14 +1,17 @@ class Blekko - HOST = "www.blekko.com" + HOST = "blekko.com" DEFAULT_MAX_FREQUENCY_PER_SECOND = 1 + SECURE_PROTOCOL = "https://" + NON_SECURE_PROTOCOL = "http://" - attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie + attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie, + :last_request_at def initialize(args={}) @api_key = args[:api_key] - @protocol = args[:secure] ? "https://" : "http://" + @protocol = args[:secure] ? SECURE_PROTOCOL : NON_SECURE_PROTOCOL @username = args[:username] @password = args[:password] @max_frequency_per_second = args[:max_frequency_per_second] || DEFAULT_MAX_FREQUENCY_PER_SECOND login if @api_key && @username && @password end @@ -23,26 +26,44 @@ def slashtag(name, args={}) Blekko::Slashtag.new(self, name, args) end + def request(url) + sleep(seconds_until_next_request) + self.last_request_at = Time.now + open(url, headers) + end + def login_uri - URI("https://blekko.com/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}") + URI("#{SECURE_PROTOCOL}#{HOST}/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}") end def headers { - "Cookie" => "A=AfGTtGoSio7Hnc1xiaSrwkJX4ggMfcFiBXufPUPQXZvB5TRe36Q6tPI4woK6SKO8%2Bh8qeD7z0qEk%2B4Ceg5N9HA95UTpznKUvuuEfb04GiwhAlKARpLnp18%2BI6EYQfes1PB0QNnhHwEAC3kLjyJqCZbsxVw8ud4Z6F%2Fbg6BvJj28L;", - "User-Agent" => "Ruby" + "Cookie" => login_cookie, + "User-Agent" => "blekko-search-#{BlekkoSearch::VERSION}" } end def login raise ArgumentError, "Username and password are required" unless username && password Net::HTTP.start(login_uri.host, login_uri.port, use_ssl: true) do |http| response = http.request Net::HTTP::Get.new login_uri.request_uri self.login_cookie = response.get_fields('Set-Cookie').find { |c| c =~ /\AA=/ } end + end + + def delay_between_requests + 1 / max_frequency_per_second.to_f + end + + def earliest_next_request + last_request_at ? last_request_at + delay_between_requests : Time.now + end + + def seconds_until_next_request + [earliest_next_request - Time.now, 0].max end end