oddb2xml 2.7.1 → 2.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -2
- data/.standard.yml +2 -0
- data/Gemfile +3 -3
- data/History.txt +24 -0
- data/README.md +3 -3
- data/Rakefile +24 -23
- data/bin/check_artikelstamm +11 -11
- data/bin/compare_v5 +23 -23
- data/bin/oddb2xml +14 -13
- data/lib/oddb2xml/builder.rb +1070 -1038
- data/lib/oddb2xml/calc.rb +232 -233
- data/lib/oddb2xml/chapter_70_hack.rb +38 -32
- data/lib/oddb2xml/cli.rb +252 -236
- data/lib/oddb2xml/compare.rb +70 -59
- data/lib/oddb2xml/compositions_syntax.rb +451 -430
- data/lib/oddb2xml/compressor.rb +20 -20
- data/lib/oddb2xml/downloader.rb +157 -129
- data/lib/oddb2xml/extractor.rb +295 -295
- data/lib/oddb2xml/options.rb +34 -35
- data/lib/oddb2xml/parslet_compositions.rb +265 -269
- data/lib/oddb2xml/semantic_check.rb +39 -33
- data/lib/oddb2xml/util.rb +163 -163
- data/lib/oddb2xml/version.rb +1 -1
- data/lib/oddb2xml/xml_definitions.rb +32 -33
- data/lib/oddb2xml.rb +1 -1
- data/oddb2xml.gemspec +34 -34
- data/shell.nix +17 -0
- data/spec/artikelstamm_spec.rb +111 -110
- data/spec/builder_spec.rb +490 -505
- data/spec/calc_spec.rb +552 -593
- data/spec/check_artikelstamm_spec.rb +26 -26
- data/spec/cli_spec.rb +173 -174
- data/spec/compare_spec.rb +9 -11
- data/spec/composition_syntax_spec.rb +390 -409
- data/spec/compressor_spec.rb +48 -48
- data/spec/data/transfer.dat +1 -0
- data/spec/data_helper.rb +47 -49
- data/spec/downloader_spec.rb +251 -260
- data/spec/extractor_spec.rb +171 -159
- data/spec/fixtures/vcr_cassettes/oddb2xml.json +1 -1
- data/spec/galenic_spec.rb +233 -256
- data/spec/options_spec.rb +116 -119
- data/spec/parslet_spec.rb +896 -863
- data/spec/spec_helper.rb +153 -153
- data/test_options.rb +39 -42
- data/tools/win_fetch_cacerts.rb +2 -3
- metadata +42 -12
data/lib/oddb2xml/compressor.rb
CHANGED
@@ -1,31 +1,30 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require 'minitar'
|
5
|
-
require 'zip'
|
1
|
+
require "zlib"
|
2
|
+
require "minitar"
|
3
|
+
require "zip"
|
6
4
|
|
7
5
|
module Oddb2xml
|
8
|
-
|
6
|
+
class Compressor
|
9
7
|
include Archive::Tar
|
10
8
|
attr_accessor :contents
|
11
|
-
def initialize(prefix=
|
9
|
+
def initialize(prefix = "oddb", options = {})
|
12
10
|
@options = options
|
13
|
-
@options[:compress_ext] ||=
|
14
|
-
@options[:format]
|
15
|
-
@compress_file = "#{prefix}_#{@options[:format]
|
16
|
-
|
17
|
-
|
11
|
+
@options[:compress_ext] ||= "tar.gz"
|
12
|
+
@options[:format] ||= :xml
|
13
|
+
@compress_file = "#{prefix}_#{@options[:format]}_" + Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}")
|
14
|
+
# @compress_file = File.join(WORK_DIR, "#{prefix}_#{@options[:format].to_s}_" +
|
15
|
+
# Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}"))
|
18
16
|
@contents = []
|
19
17
|
super()
|
20
18
|
end
|
19
|
+
|
21
20
|
def finalize!
|
22
|
-
if @contents.empty?
|
21
|
+
if @contents.empty? && (@contents.size == 0)
|
23
22
|
return false
|
24
23
|
end
|
25
24
|
begin
|
26
25
|
case @compress_file
|
27
26
|
when /\.tar\.gz$/
|
28
|
-
tgz = Zlib::GzipWriter.new(File.open(@compress_file,
|
27
|
+
tgz = Zlib::GzipWriter.new(File.open(@compress_file, "wb"))
|
29
28
|
Minitar.pack(@contents, tgz)
|
30
29
|
when /\.zip$/
|
31
30
|
Zip::File.open(@compress_file, Zip::File::CREATE) do |zip|
|
@@ -35,18 +34,19 @@ module Oddb2xml
|
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
38
|
-
if File.
|
37
|
+
if File.exist? @compress_file
|
39
38
|
puts "#{__LINE__}: @compress_file"
|
40
39
|
@contents.each do |file|
|
41
40
|
@tmpfile = file
|
42
|
-
|
43
|
-
FileUtils.rm(file) if file && File.
|
41
|
+
puts "#{__LINE__}: @tmpfile"
|
42
|
+
FileUtils.rm(file, verbose: true) if file && File.exist?(file)
|
44
43
|
end
|
45
44
|
end
|
46
|
-
|
47
|
-
|
45
|
+
rescue Errno::ENOENT
|
46
|
+
puts "Unable to compress #{@compress_file}"
|
47
|
+
raise RuntimeError
|
48
48
|
end
|
49
|
-
|
49
|
+
true
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
data/lib/oddb2xml/downloader.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
#
|
1
|
+
require "net/ntlm/version" # needed to avoid error: uninitialized constant Net::NTLM::VERSION
|
2
|
+
require "rubyntlm"
|
3
|
+
require "mechanize"
|
4
|
+
require "zip"
|
5
|
+
require "savon"
|
6
|
+
require "open-uri"
|
2
7
|
|
3
|
-
|
4
|
-
require 'rubyntlm'
|
5
|
-
require 'mechanize'
|
6
|
-
require 'zip'
|
7
|
-
require 'savon'
|
8
|
-
require 'open-uri'
|
9
|
-
|
10
|
-
SkipMigelDownloader = true # https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls
|
8
|
+
SKIP_MIGEL_DOWNLOADER = true # https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls
|
11
9
|
|
12
10
|
module Oddb2xml
|
13
11
|
module DownloadMethod
|
14
12
|
private
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
|
14
|
+
def download_as(file, option = "w+")
|
15
|
+
temp_file = File.join(WORK_DIR, File.basename(file))
|
16
|
+
@file2save = File.join(DOWNLOADS, File.basename(file))
|
18
17
|
report_download(@url, @file2save)
|
19
18
|
data = nil
|
19
|
+
FileUtils.makedirs(File.dirname(file), verbose: true)
|
20
20
|
if Oddb2xml.skip_download(file)
|
21
21
|
io = File.open(file, option)
|
22
22
|
data = io.read
|
@@ -24,48 +24,54 @@ module Oddb2xml
|
|
24
24
|
begin
|
25
25
|
io = File.open(file, option)
|
26
26
|
data = Oddb2xml.uri_open(@url).read
|
27
|
+
io.sync = true
|
27
28
|
io.write(data)
|
28
29
|
rescue => error
|
29
30
|
puts "error #{error} while fetching #{@url}"
|
30
31
|
ensure
|
31
|
-
io.close if io
|
32
|
-
Oddb2xml.download_finished(
|
32
|
+
io.close if io && !io.closed? # win
|
33
|
+
Oddb2xml.download_finished(temp_file)
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
|
+
data
|
36
37
|
end
|
37
38
|
end
|
39
|
+
|
38
40
|
class Downloader
|
39
|
-
attr_reader :type, :agent, :url
|
40
|
-
def initialize(options={}, url=nil)
|
41
|
-
@options
|
42
|
-
@url
|
41
|
+
attr_reader :type, :agent, :url, :file2save
|
42
|
+
def initialize(options = {}, url = nil)
|
43
|
+
@options = options
|
44
|
+
@url = url
|
43
45
|
@retry_times = 3
|
44
46
|
HTTPI.log = false # disable httpi warning
|
45
47
|
Oddb2xml.log "Downloader from #{@url} for #{self.class}"
|
46
48
|
init
|
47
49
|
end
|
50
|
+
|
48
51
|
def report_download(url, file)
|
49
52
|
Oddb2xml.log sprintf("%-20s: download_as %-24s from %s",
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
self.class.to_s.split("::").last,
|
54
|
+
File.basename(file),
|
55
|
+
url)
|
53
56
|
end
|
57
|
+
|
54
58
|
def init
|
55
59
|
@agent = Mechanize.new
|
56
|
-
@agent.user_agent =
|
57
|
-
@agent.redirect_ok
|
58
|
-
@agent.redirection_limit
|
60
|
+
@agent.user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0"
|
61
|
+
@agent.redirect_ok = true
|
62
|
+
@agent.redirection_limit = 5
|
59
63
|
@agent.follow_meta_refresh = true
|
60
|
-
if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i
|
61
|
-
|
64
|
+
if RUBY_PLATFORM =~ (/mswin|mingw|bccwin|cygwin/i) &&
|
65
|
+
ENV["SSL_CERT_FILE"].nil?
|
62
66
|
cert_store = OpenSSL::X509::Store.new
|
63
|
-
cert_store.add_file(File.expand_path(
|
67
|
+
cert_store.add_file(File.expand_path("../../../tools/cacert.pem", __FILE__))
|
64
68
|
@agent.cert_store = cert_store
|
65
69
|
end
|
66
70
|
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
67
71
|
end
|
72
|
+
|
68
73
|
protected
|
74
|
+
|
69
75
|
def retrievable?
|
70
76
|
if @retry_times > 0
|
71
77
|
sleep 5
|
@@ -75,29 +81,35 @@ module Oddb2xml
|
|
75
81
|
false
|
76
82
|
end
|
77
83
|
end
|
84
|
+
|
78
85
|
def read_xml_from_zip(target, zipfile)
|
79
|
-
Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.
|
86
|
+
Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exist?(zipfile)}"
|
80
87
|
if Oddb2xml.skip_download?
|
81
88
|
entry = nil
|
82
|
-
Dir.glob(File.join(
|
89
|
+
Dir.glob(File.join(DOWNLOADS, "*")).each do |name|
|
90
|
+
if target.match(name)
|
91
|
+
entry = name
|
92
|
+
break
|
93
|
+
end
|
94
|
+
end
|
83
95
|
if entry
|
84
|
-
dest = "#{
|
96
|
+
dest = "#{DOWNLOADS}/#{File.basename(entry)}"
|
85
97
|
@file2save = dest
|
86
|
-
if File.
|
98
|
+
if File.exist?(dest)
|
87
99
|
Oddb2xml.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
|
88
100
|
return IO.read(dest)
|
89
101
|
else
|
90
102
|
Oddb2xml.log "read_xml_from_zip could not read #{dest}"
|
91
103
|
end
|
92
104
|
else
|
93
|
-
Oddb2xml.log "read_xml_from_zip could not find #{target
|
105
|
+
Oddb2xml.log "read_xml_from_zip could not find #{target}"
|
94
106
|
end
|
95
107
|
end
|
96
|
-
xml =
|
97
|
-
if RUBY_PLATFORM
|
98
|
-
Zip::File.open(zipfile) do |
|
99
|
-
|
100
|
-
if entry.name
|
108
|
+
xml = ""
|
109
|
+
if RUBY_PLATFORM.match?(/mswin|mingw|bccwin|cygwin/i)
|
110
|
+
Zip::File.open(zipfile) do |a_zip_file|
|
111
|
+
a_zip_file.each do |entry|
|
112
|
+
if entry.name&.match?(target)
|
101
113
|
Oddb2xml.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
|
102
114
|
io = entry.get_input_stream
|
103
115
|
until io.eof?
|
@@ -106,19 +118,19 @@ module Oddb2xml
|
|
106
118
|
bytes = nil
|
107
119
|
end
|
108
120
|
io.close if io.respond_to?(:close)
|
109
|
-
dest = "#{
|
110
|
-
File.open(dest,
|
121
|
+
dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
|
122
|
+
File.open(dest, "w+") { |f| f.write xml }
|
111
123
|
Oddb2xml.log "read_xml_from_zip saved as #{dest}"
|
112
124
|
end
|
113
125
|
end
|
114
126
|
end
|
115
127
|
else
|
116
128
|
Zip::File.foreach(zipfile) do |entry|
|
117
|
-
if entry.name
|
129
|
+
if entry.name&.match?(target)
|
118
130
|
Oddb2xml.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
|
119
|
-
dest = "#{
|
131
|
+
dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
|
120
132
|
entry.get_input_stream { |io| xml = io.read }
|
121
|
-
File.open(dest,
|
133
|
+
File.open(dest, "w+") { |f| f.write xml }
|
122
134
|
Oddb2xml.log "read_xml_from_zip saved as #{dest}"
|
123
135
|
end
|
124
136
|
end
|
@@ -126,119 +138,129 @@ module Oddb2xml
|
|
126
138
|
xml
|
127
139
|
end
|
128
140
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
141
|
+
unless SKIP_MIGEL_DOWNLOADER
|
142
|
+
class MigelDownloader < Downloader
|
143
|
+
include DownloadMethod
|
144
|
+
def download
|
145
|
+
@url ||= "https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls"
|
146
|
+
download_as("oddb2xml_files_nonpharma.xls", "rb")
|
147
|
+
end
|
134
148
|
end
|
135
|
-
end
|
149
|
+
end
|
136
150
|
class EphaDownloader < Downloader
|
137
151
|
include DownloadMethod
|
138
152
|
def download
|
139
|
-
@url ||=
|
140
|
-
file =
|
141
|
-
content = download_as(file,
|
142
|
-
FileUtils.rm_f(file, :
|
153
|
+
@url ||= "https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/interactions_de_utf8.csv"
|
154
|
+
file = "epha_interactions.csv"
|
155
|
+
content = download_as(file, "w+")
|
156
|
+
FileUtils.rm_f(file, verbose: true)
|
143
157
|
content
|
144
158
|
end
|
145
159
|
end
|
160
|
+
|
146
161
|
class LppvDownloader < Downloader
|
147
162
|
include DownloadMethod
|
148
163
|
def download
|
149
|
-
@url ||=
|
150
|
-
download_as(
|
164
|
+
@url ||= "https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/LPPV.txt"
|
165
|
+
download_as("oddb2xml_files_lppv.txt", "w+")
|
151
166
|
end
|
152
167
|
end
|
168
|
+
|
153
169
|
class ZurroseDownloader < Downloader
|
154
170
|
include DownloadMethod
|
155
171
|
def download
|
156
|
-
@url ||=
|
157
|
-
zipfile = File.join(
|
172
|
+
@url ||= "http://pillbox.oddb.org/TRANSFER.ZIP"
|
173
|
+
zipfile = File.join(WORK_DIR, "transfer.zip")
|
158
174
|
download_as(zipfile)
|
159
|
-
dest = File.join(
|
160
|
-
cmd = "unzip -o '#{zipfile}' -d '#{
|
175
|
+
dest = File.join(DOWNLOADS, "transfer.dat")
|
176
|
+
cmd = "unzip -o '#{zipfile}' -d '#{DOWNLOADS}'"
|
161
177
|
system(cmd)
|
162
178
|
if @options[:artikelstamm]
|
163
|
-
cmd = "iconv -f ISO8859-1 -t utf-8 -o #{dest.sub(
|
179
|
+
cmd = "iconv -f ISO8859-1 -t utf-8 -o #{dest.sub(".dat", ".utf8")} #{dest}"
|
164
180
|
Oddb2xml.log(cmd)
|
165
181
|
system(cmd)
|
166
182
|
end
|
167
183
|
# read file and convert it to utf-8
|
168
|
-
File.open(dest,
|
184
|
+
File.open(dest, "r:iso-8859-1:utf-8").read
|
169
185
|
ensure
|
170
|
-
FileUtils.rm(zipfile) if File.exist?(dest) && File.exist?(zipfile)
|
186
|
+
FileUtils.rm(zipfile, verbose: true) if File.exist?(dest) && File.exist?(zipfile)
|
171
187
|
end
|
172
188
|
end
|
189
|
+
|
173
190
|
class MedregbmDownloader < Downloader
|
174
191
|
include DownloadMethod
|
175
|
-
def initialize(type
|
192
|
+
def initialize(type = :company)
|
176
193
|
@type = type
|
177
|
-
case @type
|
194
|
+
action = case @type
|
178
195
|
when :company # betrieb
|
179
|
-
|
180
|
-
when :person
|
181
|
-
|
196
|
+
"CreateExcelListBetriebs"
|
197
|
+
when :person # medizinalperson
|
198
|
+
"CreateExcelListMedizinalPersons"
|
182
199
|
else
|
183
|
-
|
200
|
+
""
|
184
201
|
end
|
185
202
|
url = "https://www.medregbm.admin.ch/Publikation/#{action}"
|
186
203
|
super({}, url)
|
187
204
|
end
|
205
|
+
|
188
206
|
def download
|
189
|
-
file = "medregbm_#{@type
|
190
|
-
download_as(file,
|
207
|
+
file = "medregbm_#{@type}.txt"
|
208
|
+
download_as(file, "w+:iso-8859-1:utf-8")
|
191
209
|
report_download(@url, file)
|
192
|
-
FileUtils.rm_f(file, :
|
210
|
+
FileUtils.rm_f(file, verbose: true) # we need it only in the download
|
193
211
|
file
|
194
212
|
end
|
195
213
|
end
|
214
|
+
|
196
215
|
class BagXmlDownloader < Downloader
|
197
216
|
include DownloadMethod
|
198
217
|
def init
|
199
218
|
super
|
200
|
-
@url ||=
|
219
|
+
@url ||= "http://www.xn--spezialittenliste-yqb.ch/File.axd?file=XMLPublications.zip"
|
201
220
|
end
|
221
|
+
|
202
222
|
def download
|
203
|
-
file = File.join(
|
223
|
+
file = File.join(WORK_DIR, "XMLPublications.zip")
|
204
224
|
download_as(file)
|
205
225
|
report_download(@url, file)
|
206
226
|
if defined?(RSpec)
|
207
|
-
src = File.join(Oddb2xml::SpecData,
|
208
|
-
content =
|
209
|
-
FileUtils.cp(src, File.join(
|
227
|
+
src = File.join(Oddb2xml::SpecData, "Preparations.xml")
|
228
|
+
content = File.read(src)
|
229
|
+
FileUtils.cp(src, File.join(DOWNLOADS, File.basename(file)))
|
210
230
|
else
|
211
|
-
content = read_xml_from_zip(/Preparations.xml/, File.join(
|
231
|
+
content = read_xml_from_zip(/Preparations.xml/, File.join(DOWNLOADS, File.basename(file)))
|
212
232
|
end
|
213
233
|
if @options[:artikelstamm]
|
214
234
|
cmd = "xmllint --format --output Preparations.xml Preparations.xml"
|
215
235
|
Oddb2xml.log(cmd)
|
216
236
|
system(cmd)
|
217
237
|
end
|
218
|
-
FileUtils.rm_f(file, :
|
238
|
+
FileUtils.rm_f(file, verbose: true) unless defined?(RSpec)
|
219
239
|
content
|
220
240
|
end
|
221
241
|
end
|
242
|
+
|
222
243
|
class RefdataDownloader < Downloader
|
223
|
-
def initialize(options={}, type
|
224
|
-
@type = (type == :pharma ?
|
225
|
-
url = "
|
244
|
+
def initialize(options = {}, type = :pharma)
|
245
|
+
@type = (type == :pharma ? "Pharma" : "NonPharma")
|
246
|
+
url = "https://refdatabase.refdata.ch/Service/Article.asmx?WSDL"
|
226
247
|
super(options, url)
|
227
248
|
end
|
249
|
+
|
228
250
|
def init
|
229
251
|
config = {
|
230
|
-
:
|
231
|
-
:
|
232
|
-
:
|
233
|
-
:
|
252
|
+
log_level: :info,
|
253
|
+
log: false, # $stdout
|
254
|
+
raise_errors: true,
|
255
|
+
wsdl: @url
|
234
256
|
}
|
235
257
|
@client = Savon::Client.new(config)
|
236
258
|
end
|
259
|
+
|
237
260
|
def download
|
238
261
|
begin
|
239
|
-
|
240
|
-
|
241
|
-
soap = %(<?xml version="1.0" encoding="UTF-8"?>
|
262
|
+
@file2save = File.join(DOWNLOADS, "refdata_#{@type}.xml")
|
263
|
+
soap = %(<?xml version="1.0" encoding="UTF-8"?>
|
242
264
|
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://refdatabase.refdata.ch/Article_in" xmlns:ns2="http://refdatabase.refdata.ch/">
|
243
265
|
<SOAP-ENV:Body>
|
244
266
|
<ns2:DownloadArticleInput>
|
@@ -249,15 +271,15 @@ module Oddb2xml
|
|
249
271
|
</ns1:ATYPE></ns2:DownloadArticleInput></SOAP-ENV:Body>
|
250
272
|
)
|
251
273
|
report_download(@url, @file2save)
|
252
|
-
return IO.read(@file2save) if Oddb2xml.skip_download?
|
253
|
-
FileUtils.rm_f(@file2save, :
|
254
|
-
response = @client.call(:download, :
|
274
|
+
return IO.read(@file2save) if Oddb2xml.skip_download? && File.exist?(@file2save)
|
275
|
+
FileUtils.rm_f(@file2save, verbose: true)
|
276
|
+
response = @client.call(:download, xml: soap)
|
255
277
|
if response.success?
|
256
|
-
if xml = response.to_xml
|
257
|
-
xml =
|
278
|
+
if (xml = response.to_xml)
|
279
|
+
xml = File.read(File.join(Oddb2xml::SpecData, File.basename(@file2save))) if defined?(RSpec)
|
258
280
|
response = nil # win
|
259
|
-
FileUtils.makedirs(
|
260
|
-
File.open(@file2save,
|
281
|
+
FileUtils.makedirs(DOWNLOADS)
|
282
|
+
File.open(@file2save, "w+") { |file| file.write xml }
|
261
283
|
if @options[:artikelstamm]
|
262
284
|
cmd = "xmllint --format --output #{@file2save} #{@file2save}"
|
263
285
|
Oddb2xml.log(cmd)
|
@@ -278,34 +300,36 @@ module Oddb2xml
|
|
278
300
|
xml
|
279
301
|
end
|
280
302
|
end
|
303
|
+
|
281
304
|
class SwissmedicDownloader < Downloader
|
282
|
-
BASE_URL =
|
305
|
+
BASE_URL = "https://www.swissmedic.ch"
|
283
306
|
include DownloadMethod
|
284
|
-
def initialize(type
|
285
|
-
url = BASE_URL +
|
307
|
+
def initialize(type = :orphan, options = {})
|
308
|
+
url = BASE_URL + "/swissmedic/de/home/services/listen_neu.html"
|
286
309
|
doc = Nokogiri::HTML(Oddb2xml.uri_open(url))
|
287
310
|
@type = type
|
288
311
|
@options = options
|
289
312
|
case @type
|
290
313
|
when :orphan
|
291
|
-
@direct_url_link = BASE_URL + doc.xpath("//a").find{|x| /Humanarzneimittel mit Status Orphan Drug/.match(x.children.text) }.attributes[
|
314
|
+
@direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Humanarzneimittel mit Status Orphan Drug/.match(x.children.text) }.attributes["href"].value
|
292
315
|
when :package
|
293
|
-
@direct_url_link = BASE_URL + doc.xpath("//a").find{|x| /Zugelassene Packungen/.match(x.children.text) }.attributes[
|
316
|
+
@direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Zugelassene Packungen/.match(x.children.text) }.attributes["href"].value
|
294
317
|
end
|
295
318
|
end
|
319
|
+
|
296
320
|
def download
|
297
|
-
@file2save = File.join(
|
321
|
+
@file2save = File.join(DOWNLOADS, "swissmedic_#{@type}.xlsx")
|
298
322
|
report_download(@url, @file2save)
|
299
|
-
if
|
323
|
+
if @options[:calc] && @options[:skip_download] && File.exist?(@file2save) && ((Time.now - File.ctime(@file2save)).to_i < 24 * 60 * 60)
|
300
324
|
Oddb2xml.log "SwissmedicDownloader #{__LINE__}: Skip downloading #{@file2save} #{File.size(@file2save)} bytes"
|
301
|
-
|
325
|
+
return File.expand_path(@file2save)
|
302
326
|
end
|
303
327
|
begin
|
304
|
-
FileUtils.rm(File.expand_path(@file2save), :verbose => !defined?(RSpec)) if File.exists?(File.expand_path(@file2save))
|
305
328
|
@url = @direct_url_link
|
306
|
-
download_as(@file2save,
|
329
|
+
download_as(@file2save, "w+")
|
307
330
|
if @options[:artikelstamm]
|
308
|
-
|
331
|
+
# ssconvert is in the package gnumeric (Debian)
|
332
|
+
cmd = "ssconvert '#{@file2save}' '#{File.join(DOWNLOADS, File.basename(@file2save).sub(/\.xls.*/, ".csv"))}' 2> /dev/null"
|
309
333
|
Oddb2xml.log(cmd)
|
310
334
|
system(cmd)
|
311
335
|
end
|
@@ -315,41 +339,45 @@ module Oddb2xml
|
|
315
339
|
ensure
|
316
340
|
Oddb2xml.download_finished(@file2save, false)
|
317
341
|
end
|
318
|
-
|
342
|
+
File.expand_path(@file2save)
|
319
343
|
end
|
320
344
|
end
|
345
|
+
|
321
346
|
class SwissmedicInfoDownloader < Downloader
|
322
347
|
def init
|
323
348
|
super
|
324
349
|
@agent.ignore_bad_chunking = true
|
325
350
|
@url ||= "http://download.swissmedicinfo.ch/Accept.aspx?ReturnUrl=%2f"
|
326
351
|
end
|
352
|
+
|
327
353
|
def download
|
328
|
-
file = File.join(
|
354
|
+
file = File.join(DOWNLOADS, "swissmedic_info.zip")
|
329
355
|
report_download(@url, file)
|
330
|
-
FileUtils.rm_f(file, :
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
356
|
+
FileUtils.rm_f(file, verbose: true) unless Oddb2xml.skip_download?
|
357
|
+
unless File.exist?(file)
|
358
|
+
begin
|
359
|
+
response = nil
|
360
|
+
if (home = @agent.get(@url))
|
361
|
+
form = home.form_with(id: "Form1")
|
362
|
+
bttn = form.button_with(name: "ctl00$MainContent$btnOK")
|
363
|
+
if (page = form.submit(bttn))
|
364
|
+
form = page.form_with(id: "Form1")
|
365
|
+
bttn = form.button_with(name: "ctl00$MainContent$BtnYes")
|
366
|
+
response = form.submit(bttn)
|
367
|
+
end
|
340
368
|
end
|
369
|
+
if response
|
370
|
+
response.save_as(file)
|
371
|
+
response = nil # win
|
372
|
+
end
|
373
|
+
rescue Timeout::Error, Errno::ETIMEDOUT
|
374
|
+
retrievable? ? retry : raise
|
375
|
+
rescue NoMethodError
|
376
|
+
# pass
|
377
|
+
ensure
|
378
|
+
Oddb2xml.download_finished(file)
|
341
379
|
end
|
342
|
-
|
343
|
-
response.save_as(file)
|
344
|
-
response = nil # win
|
345
|
-
end
|
346
|
-
rescue Timeout::Error, Errno::ETIMEDOUT
|
347
|
-
retrievable? ? retry : raise
|
348
|
-
rescue NoMethodError
|
349
|
-
# pass
|
350
|
-
ensure
|
351
|
-
Oddb2xml.download_finished(file)
|
352
|
-
end unless File.exists?(file)
|
380
|
+
end
|
353
381
|
read_xml_from_zip(/^AipsDownload_/iu, file)
|
354
382
|
end
|
355
383
|
end
|