Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes for wikipedia articles that don't have (full) entries in wikidata

  • Loading branch information...
commit a61236c20983c47468aba18a65f20dbeb88bb3f4 1 parent ff7bc83
@njh authored
Showing with 13 additions and 11 deletions.
  1. +3 −3 lib/wikidata_api.rb
  2. +10 −8 lib/wikipedia_thing.rb
View
6 lib/wikidata_api.rb
@@ -34,9 +34,9 @@ def self.find_by_title(title, site='enwiki')
})
if data['items'].nil?
- raise MediaWiki::Exception.new('Empty response')
- elsif data['items'].empty?
- raise MediaWiki::NotFound.new('Failed to lookup title in Wikidata')
+ raise MediaWikiApi::Exception.new('Empty response')
+ elsif data['items'].empty? or data['items'].keys.first == "-1"
+ raise MediaWikiApi::NotFound.new('Failed to lookup title in Wikidata')
end
data['items'].values.first
View
18 lib/wikipedia_thing.rb
@@ -92,12 +92,14 @@ def fetch_wikidata
# Attempt to lookup in WikiData, but silently fail on error
begin
data = WikidataApi.find_by_title(title)
- self.wikidata_id = data['title']
- if data['labels'].has_key?('en')
- self.wikidata_label = data['labels']['en']['value']
- end
- if data['descriptions'].has_key?('en')
- self.wikidata_description = data['descriptions']['en']['value']
+ if data.has_key?('id')
+ self.wikidata_id = data['title']
+ if data.has_key?('labels') and data['labels'].has_key?('en')
+ self.wikidata_label = data['labels']['en']['value']
+ end
+ if data.has_key?('descriptions') and data['descriptions'].has_key?('en')
+ self.wikidata_description = data['descriptions']['en']['value']
+ end
end
rescue Timeout::Error => e
$stderr.puts "Timed out while reading from Wikidata: #{e.message}"
@@ -138,8 +140,8 @@ def to_rdf
unless wikidata_id.nil?
graph << [self.uri, RDF::FOAF.page, wikidata_url]
graph << [wikidata_url, RDF.type, RDF::FOAF.Document]
- graph << [wikidata_url, RDF::RDFS.label, wikidata_label]
- graph << [wikidata_url, RDF::RDFS.comment, wikidata_description]
+ graph << [wikidata_url, RDF::RDFS.label, wikidata_label] unless wikidata_label.nil?
+ graph << [wikidata_url, RDF::RDFS.comment, wikidata_description] unless wikidata_description.nil?
end
# External links
Please sign in to comment.
Something went wrong with that request. Please try again.