forked from definitionstudio/linktv_platform
/
freebase_api.rb
76 lines (65 loc) · 2.16 KB
/
freebase_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class FreebaseApi < EntityDb
def provides_data?
true
end
def uri_to_identifier uri
uri.gsub(/http:\/\/([^\.]+.)?freebase.com(\/ns)?(.+)/, '\3')
end
def identifier_to_uri identifier
"http://www.freebase.com#{identifier}"
end
def lookup identifier, options = {}
result = super identifier, options
return result unless result.nil?
require 'open-uri'
mode = options[:xrefs] ? 'standard' : 'basic'
uri = "http://www.freebase.com/experimental/topic/#{mode}?id=#{identifier}"
file = open uri
json_text = file.read
data = JSON.parse(json_text)
return nil unless data
entity_data = data[identifier]
return nil unless entity_data && entity_data[:status.to_s] == '200 OK'
return nil unless entity_data[:code.to_s] == '/api/status/ok'
return nil unless entity_result = entity_data[:result.to_s]
# Not using symbols for keys since this will be JSON-encoded and restored later
result = {
'entity_db_id' => self.id,
'identifier' => identifier,
'uri' => identifier_to_uri(identifier),
'name' => entity_result[:text.to_s],
'description' => entity_result[:description.to_s],
'thumbnail_uri' => entity_result[:thumbnail.to_s],
'xrefs' => []
}
unless (webpages = entity_result[:webpage.to_s]).nil?
# Add supported entity identifiers
webpages.each do |webpage|
entity_uri = webpage[:url.to_s]
next unless (entity_db = EntityDb.entity_db_by_uri entity_uri)
result[:xrefs] << {
'entity_db_id' => entity_db.id,
'identifier' => entity_db.uri_to_identifier(entity_uri),
'uri' => entity_uri
}
end
end
result
end
end
# == Schema Information
#
# Table name: entity_dbs
#
# id :integer(4) not null, primary key
# type :string(255)
# name :string(255)
# description :text
# url :string(1024)
# icon_css_class :string(255)
# identifier_regex :string(255)
# active :boolean(1) default(FALSE), not null
# deleted :boolean(1) default(FALSE), not null
# created_at :datetime
# updated_at :datetime
#