From 38965edea0816d66d88106402347063c5317798d Mon Sep 17 00:00:00 2001 From: Isaiah Zwick-Schachter Date: Fri, 15 Dec 2017 09:24:30 -0500 Subject: [PATCH] Fixed backoff handling --- lib/se/api.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/se/api.rb b/lib/se/api.rb index 4492e73..1f6c00e 100644 --- a/lib/se/api.rb +++ b/lib/se/api.rb @@ -6,6 +6,7 @@ require "net/http" require "json" require "uri" +require "time" module SE module API @@ -20,7 +21,7 @@ def initialize(key, **params) @params = params.merge({filter: '!*1_).BnZb8pdvWlZpJYNyauMekouxK9-RzUNUrwiB'}) @quota = nil @quota_used = 0 - @backoff = DateTime.now + @backoff = Time.now @logger_raw = Logger.new 'api_raw.log' @logger_json = Logger.new 'api_json.log' end @@ -50,7 +51,7 @@ def answers(ids = "", **params) def json(uri, **params) throw "No site specified" if params[:site].nil? - backoff_for = ((@backoff - DateTime.now) * 24 * 60 * 60) + backoff_for = @backoff-Time.now backoff_for = 0 if backoff_for <= 0 if backoff_for > 0 puts "Backing off for #{backoff_for}" @@ -67,7 +68,7 @@ def json(uri, **params) end @logger_raw.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp_raw}" resp = JSON.parse(resp_raw) - @backoff = DateTime.now + (resp["backoff"].to_i/(24*60*60)) + @backoff = Time.now + resp["backoff"].to_i @logger_json.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp}" @quota = resp["quota_remaining"] @quota_used += 1