|
12 | 12 | require "drb" |
13 | 13 | require "fileutils" |
14 | 14 | require "json" |
| 15 | +require "mechanize" |
15 | 16 | require "net/http" |
16 | 17 | require "uri" |
17 | 18 | require "plugin/bsv_xml" |
@@ -77,7 +78,7 @@ def initialize(*args) |
77 | 78 | def update |
78 | 79 | LogFile.append("oddb/debug", " bsv_fhir: getting BsvFhirPlugin.update", Time.now) |
79 | 80 |
|
80 | | - target_url = find_latest_fhir_export_url |
| 81 | + target_url = latest_fhir_export_url |
81 | 82 | unless target_url |
82 | 83 | LogFile.append("oddb/debug", " bsv_fhir: no FHIR export URL found, aborting", Time.now) |
83 | 84 | return nil |
@@ -141,36 +142,11 @@ def update_preparations_from_fhir(io, opts = {}) |
141 | 142 |
|
142 | 143 | private |
143 | 144 |
|
144 | | - def fhir_export_url_for_date(date) |
145 | | - date_str = date.strftime("%Y%m%d") |
146 | | - "#{FHIR_BASE_URL}foph-sl-export-#{date_str}.ndjson" |
147 | | - end |
148 | | - |
149 | | - # BAG publishes NDJSON exports on irregular dates (not daily). |
150 | | - # Try today first, then go back day-by-day up to 30 days to find |
151 | | - # the latest available export. |
152 | | - def find_latest_fhir_export_url |
153 | | - today = @@today.respond_to?(:to_date) ? @@today.to_date : Date.today |
154 | | - 30.times do |offset| |
155 | | - candidate_date = today - offset |
156 | | - url = fhir_export_url_for_date(candidate_date) |
157 | | - uri = URI.parse(url) |
158 | | - http = Net::HTTP.new(uri.host, uri.port) |
159 | | - http.use_ssl = (uri.scheme == "https") |
160 | | - http.open_timeout = 10 |
161 | | - http.read_timeout = 10 |
162 | | - begin |
163 | | - response = http.request_head(uri.request_uri) |
164 | | - if response.is_a?(Net::HTTPSuccess) |
165 | | - LogFile.append("oddb/debug", " bsv_fhir: found export at #{url}", Time.now) |
166 | | - return url |
167 | | - end |
168 | | - rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET => e |
169 | | - LogFile.append("oddb/debug", " bsv_fhir: HEAD #{url} failed: #{e.message}", Time.now) |
170 | | - end |
171 | | - end |
172 | | - LogFile.append("oddb/debug", " bsv_fhir: no FHIR export found in last 30 days", Time.now) |
173 | | - nil |
| 145 | + def latest_fhir_export_url |
| 146 | + agent = Mechanize.new |
| 147 | + response = agent.get "https://epl.bag.admin.ch/api/sl/public/resources/current" |
| 148 | + resources = JSON.parse(response.body) |
| 149 | + "https://epl.bag.admin.ch/static/" + resources["fhir"]["fileUrl"] |
174 | 150 | end |
175 | 151 |
|
176 | 152 | def download_ndjson(target_url, save_dir, file_name) |
|
0 commit comments