我正在尝试从[GitHub存档]中检索数据:https://www.githubarchive.org/并且在添加范围时无法检索数据。它在我使用http://data.githubarchive.org/2015-01-01-15.json.gz时有效,但在使用http://data.githubarchive.org/2015-01-01- {0..23} .json.gz时收到'open_http':404 Not Found(OpenURI :: HTTPError)消息。
使用curl http://data.githubarchive.org/2015-01-01- {0..23} .json.gz似乎正在运行。
基本上,我的目标是编写一个程序来检索特定时间范围内前42个最活跃的存储库。
这是我的代码,请让我知道我错误地使用了API或代码问题。
require 'open-uri'
require 'zlib'
require 'yajl'
require 'pry'
require 'date'
events = Hash.new(0)
type = 'PushEvent'
after = '2015-01-01T13:00:00Z'
before = '2015-01-02T03:12:14-03:00'
f_after_time = DateTime.parse(after).strftime('%Y-%m-%d-%H')
f_after_time = DateTime.parse(before).strftime('%Y-%m-%d-%H')
base = 'http://data.githubarchive.org/'
# query = '2015-01-01-15.json.gz'
query = '2015-01-01-{0..23}.json.gz'
url = base + query
uri = URI.encode(url)
gz = open(uri)
js = Zlib::GzipReader.new(gz).read
Yajl::Parser.parse(js) do |event|
if event['type'] == type
if event['repository']
repo_name = event['repository']['url'].gsub('https://github.com/', '')
events[repo_name] +=1
elsif event['repo'] #to account for older api
repo_name = event['repo']['url'].gsub('https://github.com/', '')
events[repo_name] +=1
end
end
end
# Sort events based on # of events and return top 42 repos
sorted_events = events.sort_by {|_key, value| value}.reverse.first(42)
sorted_events.each { |e| puts "#{e[0]} - #{e[1]} events" }
答案 0 :(得分:1)
我认为URL中不允许括号,所以也许你应该尝试urlencoding呢?