|
| 1 | +require 'open-uri' |
| 2 | +require 'tess_rdf_extractors' |
| 3 | + |
| 4 | +module Ingestors |
| 5 | + class OaiPmhBioschemasIngestor < Ingestor |
| 6 | + DUMMY_URL = 'https://example.com' |
| 7 | + |
| 8 | + attr_reader :verbose |
| 9 | + |
| 10 | + def self.config |
| 11 | + { |
| 12 | + key: 'oai_pmh_bioschemas', |
| 13 | + title: 'OAI-PMH (Bioschemas RDF)', |
| 14 | + user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0', |
| 15 | + mail: Rails.configuration.tess['contact_email'] |
| 16 | + } |
| 17 | + end |
| 18 | + |
| 19 | + def read(source_url) |
| 20 | + provider_events = [] |
| 21 | + provider_materials = [] |
| 22 | + totals = Hash.new(0) |
| 23 | + |
| 24 | + client = OAI::Client.new source_url, headers: { 'From' => config[:mail] } |
| 25 | + client.list_records(metadata_prefix: 'rdf').full.each do |record| |
| 26 | + metadata_tag = Nokogiri::XML(record.metadata.to_s) |
| 27 | + bioschemas_xml = metadata_tag.at_xpath('metadata/rdf:RDF', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')&.to_s |
| 28 | + output = read_content(bioschemas_xml) |
| 29 | + next unless output |
| 30 | + |
| 31 | + provider_events += output[:resources][:events] |
| 32 | + provider_materials += output[:resources][:materials] |
| 33 | + output[:totals].each do |key, value| |
| 34 | + totals[key] += value |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + if totals.keys.any? |
| 39 | + bioschemas_summary = "Bioschemas summary:\n" |
| 40 | + totals.each do |type, count| |
| 41 | + bioschemas_summary << "\n - #{type}: #{count}" |
| 42 | + end |
| 43 | + @messages << bioschemas_summary |
| 44 | + end |
| 45 | + |
| 46 | + deduplicate(provider_events).each do |event_params| |
| 47 | + add_event(event_params) |
| 48 | + end |
| 49 | + |
| 50 | + deduplicate(provider_materials).each do |material_params| |
| 51 | + add_material(material_params) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def read_content(content) |
| 56 | + output = { |
| 57 | + resources: { |
| 58 | + events: [], |
| 59 | + materials: [] |
| 60 | + }, |
| 61 | + totals: Hash.new(0) |
| 62 | + } |
| 63 | + |
| 64 | + return output unless content |
| 65 | + |
| 66 | + begin |
| 67 | + events = Tess::Rdf::EventExtractor.new(content, :rdfxml).extract do |p| |
| 68 | + convert_params(p) |
| 69 | + end |
| 70 | + courses = Tess::Rdf::CourseExtractor.new(content, :rdfxml).extract do |p| |
| 71 | + convert_params(p) |
| 72 | + end |
| 73 | + course_instances = Tess::Rdf::CourseInstanceExtractor.new(content, :rdfxml).extract do |p| |
| 74 | + convert_params(p) |
| 75 | + end |
| 76 | + learning_resources = Tess::Rdf::LearningResourceExtractor.new(content, :rdfxml).extract do |p| |
| 77 | + convert_params(p) |
| 78 | + end |
| 79 | + output[:totals]['Events'] += events.count |
| 80 | + output[:totals]['Courses'] += courses.count |
| 81 | + output[:totals]['CourseInstances'] += course_instances.count |
| 82 | + output[:totals]['LearningResources'] += learning_resources.count |
| 83 | + |
| 84 | + deduplicate(events + courses + course_instances).each do |event| |
| 85 | + output[:resources][:events] << event |
| 86 | + end |
| 87 | + |
| 88 | + deduplicate(learning_resources).each do |material| |
| 89 | + output[:resources][:materials] << material |
| 90 | + end |
| 91 | + rescue StandardError => e |
| 92 | + Rails.logger.error("#{e.class}: #{e.message}") |
| 93 | + Rails.logger.error(e.backtrace.join("\n")) if e.backtrace&.any? |
| 94 | + error = 'An error' |
| 95 | + comment = nil |
| 96 | + if e.is_a?(RDF::ReaderError) |
| 97 | + error = 'A parsing error' |
| 98 | + comment = 'Please check your page contains valid RDF/XML.' |
| 99 | + end |
| 100 | + message = "#{error} occurred while reading the source." |
| 101 | + message << " #{comment}" if comment |
| 102 | + @messages << message |
| 103 | + end |
| 104 | + |
| 105 | + output |
| 106 | + end |
| 107 | + |
| 108 | + # ---- This is copied unchanged from bioschemas_ingestor.rb and needs to be refactored. ---- |
| 109 | + |
| 110 | + # If duplicate resources have been extracted, prefer ones with the most metadata. |
| 111 | + def deduplicate(resources) |
| 112 | + return [] unless resources.any? |
| 113 | + |
| 114 | + puts "De-duplicating #{resources.count} resources" if verbose |
| 115 | + hash = {} |
| 116 | + scores = {} |
| 117 | + resources.each do |resource| |
| 118 | + resource_url = resource[:url] |
| 119 | + puts " Considering: #{resource_url}" if verbose |
| 120 | + if hash[resource_url] |
| 121 | + score = metadata_score(resource) |
| 122 | + # Replace the resource if this resource has a higher metadata score |
| 123 | + puts " Duplicate! Comparing #{score} vs. #{scores[resource_url]}" if verbose |
| 124 | + if score > scores[resource_url] |
| 125 | + puts ' Replacing resource' if verbose |
| 126 | + hash[resource_url] = resource |
| 127 | + scores[resource_url] = score |
| 128 | + end |
| 129 | + else |
| 130 | + puts ' Not present, adding' if verbose |
| 131 | + hash[resource_url] = resource |
| 132 | + scores[resource_url] = metadata_score(resource) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + puts "#{hash.values.count} resources after de-duplication" if verbose |
| 137 | + |
| 138 | + hash.values |
| 139 | + end |
| 140 | + |
| 141 | + # Score based on number of metadata fields available |
| 142 | + def metadata_score(resource) |
| 143 | + score = 0 |
| 144 | + resource.each_value do |value| |
| 145 | + score += 1 unless value.nil? || value == {} || value == [] || (value.is_a?(String) && value.strip == '') |
| 146 | + end |
| 147 | + |
| 148 | + score |
| 149 | + end |
| 150 | + |
| 151 | + def convert_params(params) |
| 152 | + params[:description] = convert_description(params[:description]) if params.key?(:description) |
| 153 | + |
| 154 | + params |
| 155 | + end |
| 156 | + end |
| 157 | +end |
0 commit comments