forked from jsomers/hacker-classics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.rb
More file actions
35 lines (30 loc) · 972 Bytes
/
fetch.rb
File metadata and controls
35 lines (30 loc) · 972 Bytes
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
34
35
require 'open-uri'
require 'nokogiri'
require 'date'
require 'json'
require 'pry'
POINT_THRESHOLD = 50
EXCLUDE_KEYWORDS = /\b(engineer|tech|software|programming)\b/i # Matches whole words like 'engineer' or 'tech', case-insensitive
stories = Hash.new { |h, k| h[k] = [] }
["philosophy", "history", "literature"].each do |topic|
(0..200).each do |page|
ct = 0
puts "Fetch #{"https://hn.algolia.com/api/v1/search?tags=story&query=#{topic}&page=#{page}"}..."
html = URI.parse("https://hn.algolia.com/api/v1/search?tags=story&query=#{topic}&page=#{page}").read
data = JSON.parse(html)
data['hits'].each do |h|
title = h['title'] || ""
if h['points'] > POINT_THRESHOLD && !title.match?(EXCLUDE_KEYWORDS)
puts " #{h['title']} -- #{h['points']}"
stories[topic] << 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