Class: Wikian::Get

Inherits:
Subcommand show all
Defined in:
lib/wikian/get.rb

Instance Attribute Summary collapse

Attributes inherited from Subcommand

#api_url, #args, #debug, #output_file, #query, #res, #yaml

Instance Method Summary collapse

Methods inherited from Subcommand

#doit, #make_template, #response_file, #write_response

Constructor Details

#initialize(args) ⇒ Get

Returns a new instance of Get.



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

def initialize(args)
  raise ArgumentRequiredError if args.empty?

  super

  url = URI(args.find{|arg| arg =~ URI.regexp})

  raise BadUrlError unless url.path

  @title = File.basename(url.path)

  @output_file = title + '.' + url.host

  @params.merge!('titles' => title, 'format' => Wikian::RESPONSE_FORMAT)

  @query = @params.to_query

  @api_url = URI("https://#{url.host}/w/api.php?#{query}")
rescue => e
  puts "#{e.class} in #{__FILE__}. #{e.message}"
  exit
end

Instance Attribute Details

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/wikian/get.rb', line 7

def title
  @title
end

Instance Method Details

#extract_wikitextObject

extract wikitext from the response file and save it into a ‘.wiki` file

return: nil



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
# File 'lib/wikian/get.rb', line 35

def extract_wikitext
  if !res['content-type'].match?('json') || !(pages = JSON.parse(res.body).dig('query','pages'))
    raise ExtractWikiError, 'JSON response has no pages'
  end

  create_wiki = -> (title, revisions) do
    revisions.each do |revision|
      wiki_file= File.basename(response_file, File.extname(response_file)) + '.wiki'
      if revision['revid'].nil? && revisions.size > 1
        STDERR.puts "Warning: you should specify 'revid' in #{Wikian::CONFIG_FILE} to prevent overriding different revisions"
      end
      File.open(wiki_file,'w') do |f|
        content = revision.dig('slots', 'main', 'content') ||
                  revision.dig('slots', '*') ||
                  revision.dig('*')
        STDERR.puts "Warning: nil 'content' in #{Wikian::CONFIG_FILE}" unless content
        STDERR.puts "Writing to #{wiki_file}"
        f.puts content
      end
    end
  end

  # this is ugly, but Wikipedia is inconsistent in their JSON value for 'pages'. Sometimes it's a hash, sometimes it's an array.
  if pages.respond_to? :keys
    create_wiki.call(pages.values.first['title'], pages.values.first['revisions'])
  else
    pages.each do |page|
      create_wiki.call(page['title'], page['revisions'])
    end
  end

rescue => e
  puts "An error occurred while extracting the wikitext",
       "Try using a new config file by pasing the '-t' option.",
       "Or pass the '-d' option for debugging"
  exit
end

#templateObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wikian/get.rb', line 73

def template
  "    meta:\n      headers:\n        user-agent: Wikian\n    api:\n      action:\n        - query\n      prop:\n        - revisions\n      rvprop:\n        - content\n      #rvsection: # get specific sections\n      #  - 0\n      #  - 2\n      rvslots:\n        - main\n      formatversion:\n        - 2\n  eos\nend\n"