-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbill_fetcher.rb
51 lines (43 loc) · 1017 Bytes
/
bill_fetcher.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require "open-uri"
require "yaml"
require "json"
class Bill
@@billFolder = "bills"
@@filename = "update.yml"
@@lastPage = 1
@@lastId = 1
def init()
if File.exist?(@@filename)
update = YAML.load_file(@@filename)
@@lastPage = update["last_page"]
@@lastId = update["last_id"]
end
end
def update()
begin
jsonStr = getPage(@@lastPage)
json = JSON.parse(jsonStr)
nextUrl = json["next"]
json["results"].each do |result|
billId = result['url'][/\d+/]
puts "fetching page:#{@@lastPage} bill id:#{billId}"
filepath = "#{@@billFolder}/#{billId}.json"
if not File.exists?(filepath)
File.open(filepath, "w") do |f|
f.write(result.to_json)
puts "Saving the bill into #{filepath}"
end
end
#puts result
#puts "-----------------"
end
@@lastPage += 1
end while nextUrl
end
def getPage(pagenum)
contents = URI.parse("https://twly.herokuapp.com/api/bill/?page=#{pagenum}&format=json").read
end
end
p = Bill.new
p.init()
p.update()