|
3 | 3 | require 'net/http' |
4 | 4 | require 'uri' |
5 | 5 | require 'json' |
| 6 | +require 'set' |
6 | 7 |
|
7 | 8 | # Set default number_of_dojos to detect if |
8 | 9 | # Clubs API has breaking changes in Actions |
|
59 | 60 |
|
60 | 61 | # This 'variables' is fixed parameter name and cannot be renamed. |
61 | 62 | # https://graphql.org/learn/queries/#variables |
62 | | -variables = { |
63 | | - # MEMO: No need to filter to fetch all dojo data on earth. |
64 | | - # countryCode: 'JP' |
65 | | -} |
| 63 | +#variables = { |
| 64 | +# # MEMO: No need to filter to fetch all dojo data on earth. |
| 65 | +# # countryCode: 'JP' |
| 66 | +#} |
| 67 | +JP_DOJOS_QUERY = <<~GRAPH_QL |
| 68 | + query ( |
| 69 | + $after: String |
| 70 | + ) { |
| 71 | + clubs( |
| 72 | + after: $after, |
| 73 | + first: 400, |
| 74 | + filterBy: { |
| 75 | + countryCode: "JP", |
| 76 | + verified: true |
| 77 | + } |
| 78 | + ) { |
| 79 | + nodes { |
| 80 | + name |
| 81 | + latitude |
| 82 | + longitude |
| 83 | + countryCode |
| 84 | + stage |
| 85 | + urlSlug: url |
| 86 | + startTime |
| 87 | + endTime |
| 88 | + openToPublic |
| 89 | + frequency |
| 90 | + day |
| 91 | + id: uuid |
| 92 | + } |
| 93 | + pageInfo { |
| 94 | + endCursor |
| 95 | + hasNextPage |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +GRAPH_QL |
66 | 100 |
|
67 | | -def request_data(variables:) |
| 101 | +def request_data(query:, variables:) |
68 | 102 | request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS) |
69 | | - request.body = { query: ALL_DOJOS_QUERY, variables: }.to_json |
| 103 | + request.body = { query:, variables: }.to_json |
70 | 104 | req_options = { use_ssl: API_URI.scheme == 'https' } |
71 | 105 |
|
72 | 106 | response = Net::HTTP.start(API_URI.hostname, API_URI.port, req_options) do |http| |
73 | 107 | http.request(request) |
74 | 108 | end |
75 | 109 |
|
76 | 110 | # pp JSON.parse(response.body, symbolize_names: true) |
77 | | - JSON.parse(response.body, symbolize_names: true)[:data][:clubs] |
| 111 | + # JSON.parse(response.body, symbolize_names: true)[:data][:clubs] |
| 112 | + response_data = JSON.parse(response.body, symbolize_names: true) |
| 113 | + if response_data[:errors] |
| 114 | + puts "Error: #{response_data[:errors]}" |
| 115 | + return { nodes: [], pageInfo: { endCursor: nil, hasNextPage: false } } |
| 116 | + end |
| 117 | + |
| 118 | + response_data[:data][:clubs] |
78 | 119 | end |
79 | 120 |
|
80 | 121 | dojo_data = [] |
| 122 | +unique_ids = Set.new |
81 | 123 | page_number = 0 |
82 | 124 | print 'Fetching page by page: ' |
| 125 | + |
| 126 | +# Fetch clubs for Japan without filtering by brand |
| 127 | +variables = { after: nil } |
| 128 | +query = JP_DOJOS_QUERY |
83 | 129 | begin |
84 | 130 | print "#{page_number = page_number.succ}.." |
| 131 | + fetched_data = request_data(query: query, variables: variables) |
| 132 | + fetched_data[:nodes].each do |dojo| |
| 133 | + unless unique_ids.include?(dojo[:id]) |
| 134 | + dojo_data << dojo |
| 135 | + unique_ids.add(dojo[:id]) |
| 136 | + end |
| 137 | + end |
| 138 | + page_info = fetched_data[:pageInfo] |
| 139 | + variables[:after] = page_info[:endCursor] |
| 140 | +end while page_info[:hasNextPage] |
85 | 141 |
|
86 | | - fetched_data = request_data(variables:) |
87 | | - dojo_data += fetched_data[:nodes] |
88 | | - page_info = fetched_data[:pageInfo] |
89 | | - #puts fetched_data[:pageInfo], fetched_data[:nodes].first |
90 | | - |
| 142 | +# Fetch clubs for other countries with filtering by brand |
| 143 | +variables = { after: nil } |
| 144 | +query = ALL_DOJOS_QUERY |
| 145 | +begin |
| 146 | + print "#{page_number = page_number.succ}.." |
| 147 | + fetched_data = request_data(query: query, variables: variables) |
| 148 | + fetched_data[:nodes].each do |dojo| |
| 149 | + unless unique_ids.include?(dojo[:id]) |
| 150 | + dojo_data << dojo |
| 151 | + unique_ids.add(dojo[:id]) |
| 152 | + end |
| 153 | + end |
| 154 | + page_info = fetched_data[:pageInfo] |
91 | 155 | variables[:after] = page_info[:endCursor] |
92 | 156 | end while page_info[:hasNextPage] |
93 | 157 |
|
|
0 commit comments