GitHub Archive - Issues with retrieving data with ranges

106 views Asked by At

I am trying to retrieve data from [GitHub Archive]: https://www.githubarchive.org/ and is having trouble retrieving data when I add a range. It works when I use http://data.githubarchive.org/2015-01-01-15.json.gz, but getting a `open_http': 404 Not Found (OpenURI::HTTPError) message when using http://data.githubarchive.org/2015-01-01-{0..23}.json.gz.

Using curl http://data.githubarchive.org/2015-01-01-{0..23}.json.gz seems to be working.

Basically, my goal is to write a program to retrieve the top 42 most active repositories over a certain time range.

Here's my code, please let me know I'm using the API incorrectly or code issues.

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" }
1

There are 1 answers

1
user4997023 On

I believe brackets are not allowed in URL, so maybe you should try urlencoding it?