wikidata 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9c2ff62426ddfda2857a629064bd3e95d269867
4
- data.tar.gz: ecd1e1e45425d0f25d6dc125745e688391ac58c6
3
+ metadata.gz: e8e4a8a949931853b5e9f8388bdd5dd26d270e2a
4
+ data.tar.gz: 868a2e6298298fd9158c38c90bc1ed6040061911
5
5
  SHA512:
6
- metadata.gz: ae4db34040575940a822caeb1155a4e1d4dd857ff2cb66579b92045a37709f7ab8bdad98ab6f2d4c7e3598195ef990a64d721f95640b7037ee73631883cb35a0
7
- data.tar.gz: 2001dc68293fdd54b86106a397bc07f65ba92a3bf0f4a8493cb9bc5bc95b51730ebb922c0380633413101febd3c75a65473688f4499a1e34a16daa32e2128392
6
+ metadata.gz: b96b95417941d5d693a23e388db748a780d77daab3b050be4dd746a810c4414aba641b03da1758ae08ac3ae59424ac7b36615c80575b65ca650c9a74f60f3a03
7
+ data.tar.gz: b281420e1f3289d6b5fa5f5cae7b5110d2b885188215d70948718e2c946fa85d5ce452a2abfaf667235da0244f899f427de7212d275b46b0e1d52c7b3cc9601c
data/README.md CHANGED
@@ -25,32 +25,14 @@ Or install it yourself as:
25
25
  See all the claims for a particular topic.
26
26
 
27
27
  $ wikidata find "Kyle Chandler"
28
-
28
+
29
29
  This will fetch all the data (called "claims") Wikidata has on superstar actor Kyle Chandler and print it out in the terminal, like so:
30
30
 
31
31
  Kyle Chandler
32
32
  Wikidata ID: Q359604
33
33
  Claims: 14
34
- +-------------+---------------------------------------------------------------------+
35
- | property_id | value |
36
- +-------------+---------------------------------------------------------------------+
37
- | P21 | Q6581097 |
38
- +-------------+---------------------------------------------------------------------+
39
- | P27 | Q30 |
40
- +-------------+---------------------------------------------------------------------+
41
- | P106 | Q33999 |
42
- +-------------+---------------------------------------------------------------------+
43
- | P373 | Kyle Chandler |
44
- +-------------+---------------------------------------------------------------------+
45
- (...)
46
-
47
- You'll notice that doesn't seem very useful for humans, because Wikidata property keys and many of its values are not human readable. To have the gem resolve all of these opaque identifiers (which adds extra network calls), pass `-r`.
48
-
49
- $ wikidata find "Kyle Chandler" -r
50
-
51
- That will provide more useful results:
52
-
53
- (...)
34
+ +------------------------+------+---------------------------------------------------------------------+
35
+ | Property Label | id | value |
54
36
  +------------------------+------+---------------------------------------------------------------------+
55
37
  | sex (or gender) | P21 | male (Q6581097) |
56
38
  +------------------------+------+---------------------------------------------------------------------+
@@ -58,34 +40,84 @@ That will provide more useful results:
58
40
  +------------------------+------+---------------------------------------------------------------------+
59
41
  | occupation | P106 | actor / actress (Q33999) |
60
42
  +------------------------+------+---------------------------------------------------------------------+
43
+ | Commons category | P373 | Kyle Chandler |
44
+ +------------------------+------+---------------------------------------------------------------------+
45
+ | VIAF identifier | P214 | 56819238 |
46
+ +------------------------+------+---------------------------------------------------------------------+
47
+ | ISNI (ISO 27729) | P213 | 0000 0001 1444 9499 |
48
+ +------------------------+------+---------------------------------------------------------------------+
49
+ | LCCN identifier | P244 | no2007102838 |
50
+ +------------------------+------+---------------------------------------------------------------------+
51
+ | GND identifier | P227 | 1016952201 |
52
+ +------------------------+------+---------------------------------------------------------------------+
61
53
  | IMDb identifier | P345 | nm0151419 |
62
54
  +------------------------+------+---------------------------------------------------------------------+
55
+ | image | P18 | Kyle Chandler at the Texas Film Hall of Fame Awards, March 2009.jpg |
56
+ +------------------------+------+---------------------------------------------------------------------+
57
+ | instance of | P31 | human (Q5) |
58
+ +------------------------+------+---------------------------------------------------------------------+
63
59
  | place of birth | P19 | Buffalo (Q40435) |
64
60
  +------------------------+------+---------------------------------------------------------------------+
65
61
  | date of birth | P569 | 1965-09-17T00:00:00+00:00 |
66
62
  +------------------------+------+---------------------------------------------------------------------+
67
- (...)
63
+ | Freebase identifier | P646 | /m/069nzr |
64
+ +------------------------+------+---------------------------------------------------------------------+
65
+
66
+ For internationalization reasons, Wikidata property keys and many of its values are not human readable by default. (For example, the place of birth property is actually "P19".)
67
+
68
+ The CLI will automatically resolve the opaque identifiers by fetching their full descriptions from Wikidata as well. This adds a few more HTTP requests, so if you wish you can also pass `-f` to skip the resolving and return only opaque IDs, like so:
68
69
 
69
- Much better!
70
+ $ wikidata find "Kyle Chandler" -f
71
+
72
+ Kyle Chandler
73
+ Wikidata ID: Q359604
74
+ Claims: 14
75
+ +-------------+---------------------------------------------------------------------+
76
+ | property_id | value |
77
+ +-------------+---------------------------------------------------------------------+
78
+ | P21 | Q6581097 |
79
+ +-------------+---------------------------------------------------------------------+
80
+ | P27 | Q30 |
81
+ +-------------+---------------------------------------------------------------------+
82
+ | P106 | Q33999 |
83
+ +-------------+---------------------------------------------------------------------+
84
+ | P373 | Kyle Chandler |
85
+ +-------------+---------------------------------------------------------------------+
86
+ | P214 | 56819238 |
87
+ +-------------+---------------------------------------------------------------------+
70
88
 
71
89
  ### In Ruby
72
90
 
73
91
  You can use a convenient ActiveRecord-inspired syntax for finding information:
74
92
 
75
93
  ```ruby
94
+ require 'wikidata'
95
+
76
96
  los_angeles = Wikidata::Item.find_by_title "Los Angeles"
77
97
  los_angeles.id # => "Q65"
78
98
 
79
99
  # Let's find the mayor.
100
+ # The "head of government" property has an Wikidata property id of "P6".
80
101
  mayor = los_angeles.claims_for_property_id("P6").first.mainsnak.value.entity
81
102
  mayor.label # => "Eric Garcetti"
82
- ```
83
103
 
84
- That's the basics!
104
+ # There's a few convenience methods for fetching common associated entities
105
+ item = Wikidata::Item.find_by_title("Chelsea Clinton")
106
+ item.mothers.first.label # => "Hillary Rodham Clinton"
107
+
108
+ # There's also a convenience method for finding the default image
109
+ sf = Wikidata::Item.find_by_title("San Francisco")
110
+ sf.image
111
+ # => an instance of Wikidata::DataValues::CommonsMedia
112
+ sf.image.resolved
113
+ # => an instance of Wikidata::DataValues::CommonsMedia with additional data fetched
114
+ sf.image.resolved.file.urls.file
115
+ # => "http://upload.wikimedia.org/wikipedia/commons/3/3b/San_Francisco_%28Evening%29.jpg"
116
+ ```
85
117
 
86
118
  ## Contributing
87
119
 
88
- 1. Fork it ( http://github.com/<my-github-username>/wikidata/fork )
120
+ 1. Fork it ( http://github.com/wilg/wikidata/fork )
89
121
  2. Create your feature branch (`git checkout -b my-new-feature`)
90
122
  3. Commit your changes (`git commit -am 'Add some feature'`)
91
123
  4. Push to the branch (`git push origin my-new-feature`)
@@ -11,12 +11,15 @@ require "wikidata/statement"
11
11
  require "wikidata/snak"
12
12
  require "wikidata/datavalues/value"
13
13
  require "wikidata/datavalues/string"
14
+ require "wikidata/datavalues/commons_media"
14
15
  require "wikidata/datavalues/time"
15
16
  require "wikidata/datavalues/globecoordinate"
16
17
  require "wikidata/datavalues/entity"
17
18
 
18
19
  module Wikidata
19
20
 
21
+ @@verbose = false
22
+
20
23
  def self.use_only_default_language
21
24
  true
22
25
  end
@@ -25,4 +28,12 @@ module Wikidata
25
28
  use_only_default_language ? {languages: I18n.default_locale} : {}
26
29
  end
27
30
 
31
+ def self.verbose?
32
+ !!@@verbose
33
+ end
34
+
35
+ def self.verbose=(v)
36
+ @@verbose = !!v
37
+ end
38
+
28
39
  end
@@ -6,19 +6,25 @@ module Wikidata
6
6
  class CommandLine < Thor
7
7
 
8
8
  desc "find ARTICLE_NAME", "find a Wikidata entity by name"
9
- method_option :resolve_properties, :default => false, type: :boolean, aliases: "-r"
9
+ method_option :fast, :default => false, type: :boolean, aliases: "-f"
10
+ method_option :verbose, :default => false, type: :boolean, aliases: "-v"
10
11
  def find(article_name)
12
+ apply_options!
11
13
  display_item Wikidata::Item.find_by_title(article_name)
12
14
  end
13
15
 
14
16
  desc "get ID", "find a Wikidata entity by ID"
15
- method_option :resolve_properties, :default => false, type: :boolean, aliases: "-r"
17
+ method_option :fast, :default => false, type: :boolean, aliases: "-f"
18
+ method_option :verbose, :default => false, type: :boolean, aliases: "-v"
16
19
  def get(article_id)
20
+ apply_options!
17
21
  display_item Wikidata::Item.find_by_id(article_id)
18
22
  end
19
23
 
20
24
  desc "traverse ARTICLE_NAME relation_name", "find all related items until there are no more"
25
+ method_option :verbose, :default => false, type: :boolean, aliases: "-v"
21
26
  def traverse(article_name, relation_name)
27
+ apply_options!
22
28
  item = Wikidata::Item.find_by_title(article_name)
23
29
  if item
24
30
  puts "#{item.label.green} (#{item.id})"
@@ -36,6 +42,10 @@ module Wikidata
36
42
 
37
43
  protected
38
44
 
45
+ def apply_options!
46
+ Wikidata.verbose = options[:verbose]
47
+ end
48
+
39
49
  def display_item(item)
40
50
  if item
41
51
  puts " #{item.label.green}" if item.label
@@ -43,12 +53,16 @@ module Wikidata
43
53
  puts " Wikidata ID: #{item.id}"
44
54
  puts " Claims: #{item.claims.length}" if item.claims
45
55
  if item.claims.length > 0
46
- if options[:resolve_properties]
56
+ if !options[:fast]
47
57
  item.resolve_claims!
48
58
  table_data = item.claims.map do |claim|
49
- { :id => claim.mainsnak.property_id,
59
+ should_resolve_value = claim.mainsnak.value.class != Wikidata::DataValues::CommonsMedia
60
+ {
61
+ :id => claim.mainsnak.property_id,
50
62
  'Property Label' => claim.mainsnak.property.label,
51
- value: claim.mainsnak.value.resolved}
63
+ value: should_resolve_value ? claim.mainsnak.value.resolved : claim.mainsnak.value
64
+ # datatype: claim.mainsnak.property.datatype
65
+ }
52
66
  end
53
67
  else
54
68
  table_data = item.claims.map do |claim|
@@ -0,0 +1,18 @@
1
+ module Wikidata
2
+ module DataValues
3
+ class CommonsMedia < Wikidata::DataValues::Value
4
+
5
+ def to_s
6
+ data_hash.imagename
7
+ end
8
+
9
+ def resolve!
10
+ query = {image: data_hash.imagename, thumbwidth: 150}
11
+ puts "Getting: #{query}".yellow if Wikidata.verbose?
12
+ r = HTTParty.get('http://tools.wmflabs.org/magnus-toolserver/commonsapi.php', {query: query})
13
+ @data_hash = Hashie::Mash.new(r['response'].merge({imagename: data_hash.imagename}))
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -26,11 +26,6 @@ module Wikidata
26
26
  entity
27
27
  end
28
28
 
29
- def resolved
30
- resolve!
31
- super
32
- end
33
-
34
29
  def to_s
35
30
  if @item.nil?
36
31
  item_id
@@ -2,7 +2,11 @@ module Wikidata
2
2
  module DataValues
3
3
  class Value < Wikidata::HashedObject
4
4
 
5
+ def resolve!
6
+ end
7
+
5
8
  def resolved
9
+ resolve!
6
10
  self
7
11
  end
8
12
 
@@ -57,7 +57,7 @@ module Wikidata
57
57
 
58
58
  def self.query_and_build_objects(query)
59
59
  response = HTTParty.get('http://www.wikidata.org/w/api.php', {query: query})
60
- # puts "Getting: #{query}"
60
+ puts "Getting: #{query}".yellow if Wikidata.verbose?
61
61
  response['entities'].map do |entity_id, entity_hash|
62
62
  item = new(entity_hash)
63
63
  IdentityMap.cache!(entity_id, item)
@@ -41,5 +41,28 @@ module Wikidata
41
41
  []
42
42
  end
43
43
 
44
+ # Convenience methods
45
+
46
+ def image
47
+ image_claim = claims_for_property_id("P18").first
48
+ image_claim.mainsnak.value if image_claim
49
+ end
50
+
51
+ def mothers
52
+ entities_for_property_id :mother
53
+ end
54
+
55
+ def fathers
56
+ entities_for_property_id :father
57
+ end
58
+
59
+ def children
60
+ entities_for_property_id :children
61
+ end
62
+
63
+ def doctoral_advisors
64
+ entities_for_property_id :doctoral_advisor
65
+ end
66
+
44
67
  end
45
68
  end
@@ -10,16 +10,22 @@ module Wikidata
10
10
  end
11
11
 
12
12
  def value
13
- if datavalue['type'] == "wikibase-entityid"
14
- Wikidata::DataValues::Entity.new(datavalue.value)
15
- elsif datavalue['type'] == "time"
16
- Wikidata::DataValues::Time.new(datavalue.value)
17
- elsif datavalue['type'] == "globecoordinate"
18
- Wikidata::DataValues::Globecoordinate.new(datavalue.value)
19
- elsif datavalue['type'] == 'string'
20
- Wikidata::DataValues::String.new({string: datavalue.value})
21
- else
22
- datavalue
13
+ @value ||= begin
14
+ if datavalue['type'] == "wikibase-entityid"
15
+ Wikidata::DataValues::Entity.new(datavalue.value)
16
+ elsif datavalue['type'] == "time"
17
+ Wikidata::DataValues::Time.new(datavalue.value)
18
+ elsif datavalue['type'] == "globecoordinate"
19
+ Wikidata::DataValues::Globecoordinate.new(datavalue.value)
20
+ elsif datavalue['type'] == 'string'
21
+ if property_id == "P18" || (@property && @property.datatype == "commonsMedia")
22
+ Wikidata::DataValues::CommonsMedia.new({imagename: datavalue.value})
23
+ else
24
+ Wikidata::DataValues::String.new({string: datavalue.value})
25
+ end
26
+ else
27
+ datavalue
28
+ end
23
29
  end
24
30
  end
25
31
 
@@ -1,3 +1,3 @@
1
1
  module Wikidata
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,139 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikidata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wil Gieseler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: formatador
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: thor
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: i18n
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: hashie
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '2.0'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '2.0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: bundler
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ~>
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
117
  version: '1.5'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ~>
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.5'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rake
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  description:
@@ -144,7 +144,7 @@ executables:
144
144
  extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
- - .gitignore
147
+ - ".gitignore"
148
148
  - Gemfile
149
149
  - LICENSE.txt
150
150
  - README.md
@@ -152,6 +152,7 @@ files:
152
152
  - bin/wikidata
153
153
  - lib/wikidata.rb
154
154
  - lib/wikidata/command_line.rb
155
+ - lib/wikidata/datavalues/commons_media.rb
155
156
  - lib/wikidata/datavalues/entity.rb
156
157
  - lib/wikidata/datavalues/globecoordinate.rb
157
158
  - lib/wikidata/datavalues/string.rb
@@ -176,17 +177,17 @@ require_paths:
176
177
  - lib
177
178
  required_ruby_version: !ruby/object:Gem::Requirement
178
179
  requirements:
179
- - - '>='
180
+ - - ">="
180
181
  - !ruby/object:Gem::Version
181
182
  version: '0'
182
183
  required_rubygems_version: !ruby/object:Gem::Requirement
183
184
  requirements:
184
- - - '>='
185
+ - - ">="
185
186
  - !ruby/object:Gem::Version
186
187
  version: '0'
187
188
  requirements: []
188
189
  rubyforge_project:
189
- rubygems_version: 2.0.3
190
+ rubygems_version: 2.2.0
190
191
  signing_key:
191
192
  specification_version: 4
192
193
  summary: Ruby client for Wikidata