Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added links to Wikidata from thing pages.

  • Loading branch information...
commit cd28836d2e7651d89651cf2396da4b574efaa2c1 1 parent 0973940
@njh authored
Showing with 65 additions and 4 deletions.
  1. +20 −3 lib/wikidata_api.rb
  2. +45 −1 lib/wikipedia_thing.rb
View
23 lib/wikidata_api.rb
@@ -9,9 +9,9 @@ def self.api_uri
def self.get_sitelink(id, site='enwiki')
data = self.get('wbgetitems', {
:ids => id,
+ :sites => site,
:props => 'sitelinks',
- :languages => 'en',
- :sites => site
+ :languages => 'en'
})
if data['items'].nil?
@@ -24,5 +24,22 @@ def self.get_sitelink(id, site='enwiki')
return data['items'][id]['sitelinks'][site]
end
end
-
+
+ def self.find_by_title(title, site='enwiki')
+ data = self.get('wbgetitems', {
+ :titles => title,
+ :sites => site,
+ :props => 'info|aliases|labels|descriptions',
+ :languages => 'en'
+ })
+
+ if data['items'].nil?
+ raise MediaWiki::Exception.new('Empty response')
+ elsif data['items'].empty?
+ raise MediaWiki::NotFound.new('Failed to lookup title in Wikidata')
+ end
+
+ data['items'].values.first
+ end
+
end
View
46 lib/wikipedia_thing.rb
@@ -13,7 +13,9 @@ class WikipediaThing < BaseModel
has :updated_at, :kind => DateTime, :default => nil
has :freebase_guid, :kind => String, :default => nil
has :freebase_mid, :kind => String, :default => nil
-
+ has :wikidata_id, :kind => String, :default => nil
+ has :wikidata_label, :kind => String, :default => nil
+ has :wikidata_description, :kind => String, :default => nil
def load
data = WikipediaApi.parse(pageid)
@@ -72,6 +74,39 @@ def fetch_freebase_uris
end
end
+ def wikidata_id
+ fetch_wikidata
+ @wikidata_id
+ end
+
+ def wikidata_url
+ if wikidata_id
+ @wikidata_url ||= RDF::URI.parse("http://wikidata.org/wiki/"+wikidata_id)
+ end
+ end
+
+ def fetch_wikidata
+ # Only make call to Wikidata once
+ unless @called_wikidata
+ @called_wikidata = true
+ # 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']
+ end
+ rescue Timeout::Error => e
+ $stderr.puts "Timed out while reading from Wikidata: #{e.message}"
+ rescue MediaWikiApi::Exception => e
+ $stderr.puts "Error while reading from Wikidata: #{e.message}"
+ end
+ end
+ end
+
def has_coordinates?
!(latitude.nil? || longitude.nil?)
end
@@ -98,6 +133,15 @@ def to_rdf
graph << [self.uri, RDF::OWL.sameAs, freebase_mid_uri] unless freebase_mid_uri.nil?
graph << [self.uri, RDF::GEO.lat, latitude] unless latitude.nil?
graph << [self.uri, RDF::GEO.long, longitude] unless longitude.nil?
+
+ # Link to WikiData
+ unless wikidata_id.nil?
+ graph << [self.uri, RDF::FOAF.page, wikidata_url]
+ graph << [wikidata_url, RDF::RDFS.label, wikidata_label]
+ graph << [wikidata_url, RDF::RDFS.comment, wikidata_description]
+ end
+
+ # External links
externallinks.each do |link|
graph << [self.uri, RDF::FOAF.page, link]
end
Please sign in to comment.
Something went wrong with that request. Please try again.