-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_data_from_earth.rb
executable file
·170 lines (156 loc) · 4.01 KB
/
get_data_from_earth.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
require 'set'
# Set default number_of_dojos to detect if
# Clubs API has breaking changes in Actions
File.write('tmp/number_of_dojos', 0)
API_URI = URI.parse('https://clubs-api.raspberrypi.org/graphql')
HEADERS = {
'accept' => 'application/json',
'content-type' => 'application/json'
}
ALL_DOJOS_QUERY = <<~GRAPH_QL
query (
# No need to filter to fetch all dojo data on earth.
# $countryCode: String!,
#
# This 'after' has which page we have read and the next page to read.
$after: String,
) {
clubs(
after: $after,
first: 400,
filterBy: {
# No need to filter to fetch all dojo data on earth.
# countryCode: $countryCode,
#
# For DEBUG add this "JP" filter
#countryCode: "JP",
brand: CODERDOJO,
verified: true
}
) {
nodes {
name
latitude
longitude
countryCode
stage
urlSlug: url
id: uuid
#startTime
#endTime
#openToPublic
#frequency
#day
}
pageInfo {
endCursor
hasNextPage
}
}
}
GRAPH_QL
# This 'variables' is fixed parameter name and cannot be renamed.
# https://graphql.org/learn/queries/#variables
#variables = {
# # MEMO: No need to filter to fetch all dojo data on earth.
# # countryCode: 'JP'
#}
JP_DOJOS_QUERY = <<~GRAPH_QL
query (
$after: String
) {
clubs(
after: $after,
first: 400,
filterBy: {
countryCode: "JP",
verified: true
}
) {
nodes {
name
latitude
longitude
countryCode
stage
urlSlug: url
id: uuid
#startTime
#endTime
#openToPublic
#frequency
#day
}
pageInfo {
endCursor
hasNextPage
}
}
}
GRAPH_QL
def request_data(query:, variables:)
request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS)
request.body = { query:, variables: }.to_json
req_options = { use_ssl: API_URI.scheme == 'https' }
response = Net::HTTP.start(API_URI.hostname, API_URI.port, req_options) do |http|
http.request(request)
end
# pp JSON.parse(response.body, symbolize_names: true)
# JSON.parse(response.body, symbolize_names: true)[:data][:clubs]
response_data = JSON.parse(response.body, symbolize_names: true)
if response_data[:errors]
puts "Error: #{response_data[:errors]}"
return { nodes: [], pageInfo: { endCursor: nil, hasNextPage: false } }
end
response_data[:data][:clubs]
end
dojo_data = []
unique_ids = Set.new
page_number = 0
print 'Fetching page by page: '
# Fetch clubs for Japan without filtering by brand
variables = { after: nil }
query = JP_DOJOS_QUERY
begin
print "#{page_number = page_number.succ}.."
fetched_data = request_data(query: query, variables: variables)
fetched_data[:nodes].each do |dojo|
unless unique_ids.include?(dojo[:id])
dojo_data << dojo
unique_ids.add(dojo[:id])
end
end
page_info = fetched_data[:pageInfo]
variables[:after] = page_info[:endCursor]
end while page_info[:hasNextPage]
# Fetch clubs for other countries with filtering by brand
variables = { after: nil }
query = ALL_DOJOS_QUERY
begin
print "#{page_number = page_number.succ}.."
fetched_data = request_data(query: query, variables: variables)
fetched_data[:nodes].each do |dojo|
unless unique_ids.include?(dojo[:id])
dojo_data << dojo
unique_ids.add(dojo[:id])
end
end
page_info = fetched_data[:pageInfo]
variables[:after] = page_info[:endCursor]
end while page_info[:hasNextPage]
File.write('tmp/number_of_dojos', dojo_data.length)
File.open('dojos_earth.json', 'w') do |file|
file.puts JSON.pretty_generate(dojo_data.sort_by{|dojo| dojo[:id]})
end
# Show next step for developers
#puts DOJOS_JSON
puts ''
puts 'Fetched number of dojos: ' + File.read('tmp/number_of_dojos')
puts ''
puts 'Check out its details by:'
puts '$ cat dojos_earth.json'
puts ''