Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
Fixes for wikipedia articles that don't have (full) entries in wikidata
Browse files Browse the repository at this point in the history
  • Loading branch information
njh committed Nov 24, 2012
1 parent ff7bc83 commit a61236c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/wikidata_api.rb
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions lib/wikipedia_thing.rb
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a61236c

Please sign in to comment.