Class: ODDB::NarcoticPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/narcotic.rb

Constant Summary

MEDDATA_SERVER =
DRbObject.new(nil, MEDDATA_URI)

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Plugin

#l10n_sessions, #log_info, #recipients, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

- (NarcoticPlugin) initialize(app)

A new instance of NarcoticPlugin



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'src/plugin/narcotic.rb', line 72

def initialize(app)
  @unknown_registrations = []
  @new_substances = []
  @new_narcotics = 0
  @unknown_packages = []
  @narcotic_texts = {}
  @reserve_substances = []
  @unknwon_substances_text = []
  @removed_narcotics = []
  @packages = []
  @updated_packages = {}
  @updated_narcs = {}
  @narcs = {}
  super(app)
end

Instance Method Details

- (Object) casrns(row)



87
88
89
90
91
92
93
# File 'src/plugin/narcotic.rb', line 87

def casrns(row)
  casrns = row.at(1).to_s.split('/').collect { |casrn|
    if(/\d+/u.match(casrn))
      casrn.to_s.strip
    end
  }.compact
end

- (Object) category(row)



94
95
96
97
98
99
100
# File 'src/plugin/narcotic.rb', line 94

def category(row)
  category = row.at(5).to_s
  if(category == "")
    category = "a"
  end
  category
end

- (Object) name(row)



101
102
103
# File 'src/plugin/narcotic.rb', line 101

def name(row)
  row.at(0).to_s.strip
end

- (Object) postprocess(language)



104
105
106
107
108
109
110
# File 'src/plugin/narcotic.rb', line 104

def postprocess(language)
  @packages.each { |row|
    update_package(row, language)
  }
  update_narcotic_texts(language)
  prune_narcotics
end

- (Object) process_row(row, language)



111
112
113
114
115
116
117
118
119
120
121
122
# File 'src/plugin/narcotic.rb', line 111

def process_row(row, language)
  if /^7611/u.match(row.at(3))
    casrn = casrns(row).first
    if narc = update_narcotic(row, casrn, language)
      @narcs.store(casrn, narc) if casrn
      @updated_narcs.store(narc.oid, narc)
    end
    update_substance(row, casrn, narc, language)
  elsif(/^7680/u.match(row.at(3)))
    @packages.push(row)
  end
end

- (Object) prune_narcotics



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
# File 'src/plugin/narcotic.rb', line 123

def prune_narcotics
  unless @updated_packages.empty?
    @app.each_package do |pac|
      unless pac.narcotics.empty?
        upd = @updated_packages[pac.pointer]
        pac.narcotics.dup.each do |narc|
          unless upd && upd.include?(narc)
            pac.remove_narcotic narc
            row = [
              pac.name_base,
              narc.casrn,
              pac.pharmacode,
              pac.barcode.to_s,
              pac.company_name,
              narc.category,
            ]
            @removed_narcotics.push(report_text(row))
          end
        end
      end
    end
  end
  unless @narcs.empty?
    @app.narcotics.each do |oid, narc|
      unless @updated_narcs.include?(oid)
        @app.delete narc.pointer
      end
    end
  end
end

- (Object) report



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'src/plugin/narcotic.rb', line 153

def report
  [
    "Narcotics: #{@updated_narcs.size}",
    "Narcotics with CASRN: #{@narcs.size}",
    "Packages with Narcotics: #{@updated_packages.size}",
    "Unknown registrations: #{@unknown_registrations.size}",
    "Unknown packages: #{@unknown_packages.size}",
    "Created Substances: #{@new_substances.size}",
    "Created Narcotics: #{@new_narcotics}",
    "Created Narcotic Texts: #{@narcotic_texts.size}",
    "Removed Narcotics #{@removed_narcotics.size}",
    "\n",
    "Name", "Casrn | Pharmacode | Ean-Code | Company | Level",
    "\n",
    "Unknown registrations: #{@unknown_registrations.size}\n",
    @unknown_registrations,
    "\n",
    "Unknown packages: \#{@unknown_packages.size}\nPackungen, die weder anhand des Swissmedic-Codes noch anhand des\nPharmacodes in der ODDB gefunden wurden. Kann auch ausser-Handel\nPackungen beinhalten.\nDiese Produkte werden in ch.oddb.org nicht angezeigt (zu wenig Informationen).\n",
    @unknown_packages,
    "\n",
    "New substances: #{@new_substances.size}\n",
    @new_substances,
    "\n",
    "New Narcotic Texts: #{@narcotic_texts.size}\n",
    @narcotic_texts,
    "\n",
    "Removed Narcotics #{@removed_narcotics.size}\n",
    @removed_narcotics,
  ].join("\n")
end

- (Object) report_text(row)



190
191
192
193
194
195
196
# File 'src/plugin/narcotic.rb', line 190

def report_text(row)
  if(name = row.first)
    name + "\n" + row[1..-1].join(" | ") + "\n"
  else
    row.join(" | ") + "\n"
  end
end

- (Object) smcd(row)



197
198
199
200
201
202
# File 'src/plugin/narcotic.rb', line 197

def smcd(row)
  smcd = row.at(3).to_s
  if(/\d+/u.match(smcd))
    smcd[4,8]
  end
end

- (Object) strip_name(row)



203
204
205
206
# File 'src/plugin/narcotic.rb', line 203

def strip_name(row)
  orig = name(row)
  orig.split(/\(\s*((unter\s*)?Vorbehalt|voir)/iu, 2)
end

- (Object) text2name(text, language)



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'src/plugin/narcotic.rb', line 207

def text2name(text, language)
  case language
  when :de 
    if(match = /^(.+)haltige/u.match(text))
      match[1]
    end
  when :fr
    if(match = /contenant (de la |du |d')([^\s]+)/u.match(text))
      match[2]
    end
  else
    raise "unhandled language: '#{language}'"
  end
end

- (Object) update(languages = [:de, :fr])



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'src/plugin/narcotic.rb', line 221

def update(languages = [:de, :fr])
  pattern = /^(Verzeichnis\s+aller|Indice\s+de\s+tous)/u
  agent = Mechanize.new
  url = "http://www.swissmedic.ch/produktbereiche/00447/00536/index.html"
  dir = File.join ARCHIVE_PATH, 'pdf'
  success = false
  languages.each do |language|
    retries = 3
    begin
      page = agent.get url + "?lang=#{language}"
      link = page.links.find do |link| pattern.match link.text end
      pdf = link.click
      latest = File.join dir, "narcotics-#{language}-latest.pdf"
      unless File.exist?(latest) && File.read(latest) == pdf.body
        name = @@today.strftime "narcotics-#{language}-%d.%m.%Y.pdf"
        path = File.join dir, name
        pdf.save path
        update_from_pdf path, language
        ## if everything went well, save the pdf as 'latest'
        pdf.save latest
        success = true
      end
    rescue EOFError
      if retries > 0
        retries -= 1
        sleep 60 ** (2 - retries)
      else
        raise "Downloading Narcotics-PDF file still fails with EOFError after 3 retries"
      end
    end
  end
  success
end

- (Object) update_from_csv(path, language)



254
255
256
257
258
259
# File 'src/plugin/narcotic.rb', line 254

def update_from_csv(path, language)
  CSV.open(path, 'r', ';').each { |row|
    process_row(row)
  }
  postprocess(language)
end

- (Object) update_from_pdf(path, language)



260
261
262
263
264
265
266
# File 'src/plugin/narcotic.rb', line 260

def update_from_pdf(path, language)
  parser = Rpdf2txt::Parser.new(File.read(path), 'UTF-8')
  callback = Proc.new do |row| process_row row, language end
  handler = NarcoticHandler.new callback
  parser.extract_text(handler)
  postprocess(language)
end

- (Object) update_narcotic(row, casrn, language)



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'src/plugin/narcotic.rb', line 267

def update_narcotic(row, casrn, language)
  smcd = smcd(row)
  category = category(row)
  name = name(row)
  values = {}
  # When smcd and casrn are nil, we have a text description
  if(category == "c" && (subst = text2name(name, language)))
    @narcotic_texts.store(subst, name)
    nil
  elsif(casrn || smcd)
    if(category)
      values.store(:category, category)
    end
    # Search narcotic objects by casrn or swissmedic code.
    # The new narc objects will be stored with the
    # swissmedic code or with the casrn number
    narc = @app.narcotic_by_casrn(casrn) \
      || @app.narcotic_by_smcd(smcd)
    if(narc) 
      pointer =  narc.pointer
    else
      @new_narcotics += 1
      pointer = Persistence::Pointer.new(:narcotic).creator
    end
    @app.update(pointer, values, :swissmedic)
  end
end

- (Object) update_narcotic_texts(language)



294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'src/plugin/narcotic.rb', line 294

def update_narcotic_texts(language)
  @narcotic_texts.each { |name, text|
    @reserve_substances.delete_if { |substance|
      if(substance.send(language).include?(name))
        ptr = substance.narcotic.pointer + :reservation_text
        args = {
          language => text,
        } 
        @app.update(ptr.creator, args, :swissmedic)
      end
    }
  }
end

- (Object) update_package(row, language)



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'src/plugin/narcotic.rb', line 307

def update_package(row, language)
  if((smcd = smcd(row)) && (registration = @app.registration(smcd[0,5])))
    package = registration.package(smcd[5,3])
    if package.nil?
      pcode = row.at(2)
      unless pcode
        begin
          MEDDATA_SERVER.session(:product) { |meddata|
            results = meddata.search(:ean13 => row.at(3))
            if(results.size == 1)
              data = meddata.detail(results.first, {:pharmacode => [3,2]})
              pcode = data[:pharmacode]
            end
          }
        rescue MedData::OverflowError
          ## obviously something went wrong, and we have received too many
          #  results.. since we're only interested in results of size 1, we
          #  can safely ignore this.
        end
      end
      package = Package.find_by_pharmacode(pcode) if pcode
    end
    if package
      casrns(row).each { |casrn|
        if !casrn.to_s.strip.empty? && (narc = @narcs[casrn])
          package.add_narcotic(narc)
          (@updated_packages[package.pointer] ||= []).push narc
        end
      }
      package.odba_store
    else
      @unknown_packages.push(report_text(row))
    end
  else
    @unknown_registrations.push(report_text(row))
  end
end

- (Object) update_substance(row, casrn, narc, language)



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'src/plugin/narcotic.rb', line 344

def update_substance(row, casrn, narc, language)
  smcd = smcd(row)
  name, rest = strip_name(row)
  pointer = Persistence::Pointer.new(:substance).creator
  data = {
    language          => name.strip,
    :narcotic         => narc,
    :casrn            => casrn,
    :swissmedic_code  => smcd,
  }
  substance = @app.substance_by_smcd(smcd) \
    || @app.substance(name)
  if(substance)
    pointer = substance.pointer
  else
    @new_substances.push(report_text(row))
  end
  substance = @app.update(pointer, data, :swissmedic)
  if(rest)
    @reserve_substances.push(substance)
  end
  substance
end