Class: Wikipedia

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cinch/plugins/miwiki/wikipedia.rb

Defined Under Namespace

Classes: Excerpt

Instance Method Summary collapse

Instance Method Details

#fetch(title) ⇒ Object


15
16
17
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 15

def fetch(title)
  get_content(title)
end

#get_categories(title, query = {}) ⇒ Object

Raises:

  • (StandardError)

53
54
55
56
57
58
59
60
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 53

def get_categories(title, query={})
  query = { action: "query", prop: "categories", format: "json", cllimit: "20", redirects: "", titles: title }.merge(query)

  response = self.class.get('/api.php', query: query)
  raise StandardError, response['error']['info'] if response.has_key?('error')
  raise StandardError, 'txt' unless response['query']['pages'].values[0].has_key?('categories')
  response['query']['pages'].values[0]['categories'].map {|v| v['title'] }
end

#get_content(title, query = {}) ⇒ Object

Raises:

  • (StandardError)

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
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 19

def get_content(title, query={})
  query = { action: "query", prop: "extracts", format: "json", exlimit: "10", redirects: "", titles: title }.merge(query)
  response = self.class.get('/api.php', query: query)
  raise StandardError, response['error']['info'] if response.has_key?('error')
  article = response['query']['pages'].values[0]
  if article.has_key?('missing')
    new_title = get_search(title)[1][0]
    return get_content(new_title)
  end

  extract = Nokogiri::HTML article['extract']

  summary = []
  if is_disambiguation?(article['title'])
    summary << (extract.at('p').text.match(/refer to/i) ? extract.at('p').text : 'Did you mean:')
    articles = extract.css('body > ul li').to_a.map(&:text)
    summary << "#{articles.map(&:strip)[0..4].join(', ')}#{"... (and %d more)" % (articles.length - 5) if articles.length > 5}"
    Excerpt.new(article['title'], summary.flatten * $/, :disambiguation)
  else
    type = is_list?(article['title']) ? :list : :article
    length = extract.css('p')[0].text.size > 384 ? 0..0 : 0..1
    summary << extract.css('p')[length].map {|line| line.text.strip.chomp }
    Excerpt.new(article['title'], summary.flatten * $/, type)
  end
end

#get_search(title, query = {}) ⇒ Object

Raises:

  • (StandardError)

62
63
64
65
66
67
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 62

def get_search(title, query={})
  query = { action: "opensearch", format: "json", search: title, limit: "10", namespace: "0", suggest: "" }.merge(query)
  response = self.class.get('/api.php', query: query)
  raise StandardError, 'No search results found.' if response[1].empty?
  return response
end

#is_disambiguation?(title, query = {}) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 49

def is_disambiguation?(title, query={})
  get_categories(title).any? {|category| category =~ /\bdisambiguation\b/i }
end

#is_list?(title, query = {}) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/cinch/plugins/miwiki/wikipedia.rb', line 45

def is_list?(title, query={})
  get_categories(title).any? {|category| category =~ /\blists?\b/i }
end