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