Class: WikiData

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidata.rb,
lib/wikidata/fetcher.rb,
lib/wikidata/category.rb

Direct Known Subclasses

Category, Fetcher

Defined Under Namespace

Classes: Category, Fetcher

Class Method Summary collapse

Class Method Details

.ids_from_pages(lang, titles) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wikidata.rb', line 11

def self.ids_from_pages(lang, titles)
  client = MediawikiApi::Client.new "https://#{lang}.wikipedia.org/w/api.php"
  res = titles.compact.each_slice(50).map do |sliced|
    page_args = {
      prop:       'pageprops',
      ppprop:     'wikibase_item',
      redirects:  1,
      titles:     sliced.join('|'),
      token_type: false,
    }
    response = client.action :query, page_args
    redirected_from = Hash[(response.data['redirects'] || []).map { |h| [h['to'], h['from']] }]
    response.data['pages'].select { |_k, v| v.key? 'pageprops' }.map do |_k, v|
      [redirected_from[v['title']] || v['title'], v['pageprops']['wikibase_item']]
    end
  end
  results = Hash[res.flatten(1)]
  missing = titles - results.keys
  warn "Can't find Wikidata IDs for: #{missing.join(', ')} in #{lang}" if missing.any?
  results
end