forked from jsomers/hacker-classics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.rb
33 lines (28 loc) · 785 Bytes
/
fetch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'open-uri'
require 'nokogiri'
require 'date'
require 'json'
require 'pry'
POINT_THRESHOLD = 40
stories = Hash.new { |h, k| h[k] = [] }
(1900..2010).each do |year|
(0..100).each do |page|
ct = 0
puts "Fetch #{"https://hn.algolia.com/api/v1/search?tags=story&query='#{year}'&page=#{page}"}..."
html = URI.parse("https://hn.algolia.com/api/v1/search?tags=story&query='#{year}'&page=#{page}").read
data = JSON.parse(html)
data['hits'].each do |h|
if h['title'].include?("(#{year})") && h['points'] > POINT_THRESHOLD
puts " #{h['title']} -- #{h['points']}"
stories[year] << h
ct += 1
end
end
if ct == 0
break
end
end
end
File.open("./stories.json", "w") do |f|
f.puts JSON.generate(stories)
end