wikian 0.1.12 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +12 -8
- data/lib/wikian.rb +16 -1
- data/lib/wikian/contributions.rb +4 -4
- data/lib/wikian/get.rb +34 -37
- data/lib/wikian/monkeypatches.rb +1 -1
- data/lib/wikian/post.rb +78 -17
- data/lib/wikian/search.rb +12 -7
- data/lib/wikian/subcommand.rb +22 -17
- data/lib/wikian/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cae14274502ac01e5cf329b8a252ac275aae719b6bc1c3744b767cde19d6aed
|
4
|
+
data.tar.gz: 9efbfc03f6f86468ee4156035b29572dae7e80020a2edf07fc06c4b3980e166a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96abbb700231c2da44492719500a2c0c85284cfff9e97bc6aadebc534cbb4fd0c7c878c9ade610c421188ba29f47fcd4f32777ba6baa71bdfad84ee97e81067a
|
7
|
+
data.tar.gz: 4255b20473b74cd31d7f0afc00b67fc03623bea91babcfb9c875d71970cf3e8717d26abe3777fd81aa8800f08263a1fa366b0ecc0abd18ed4cf5b99a9211f5b4
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -70,24 +70,28 @@ $ wi post -s 2 Wikipedia:Sandbox.en.wikipedia.org.wiki
|
|
70
70
|
Article uploaded
|
71
71
|
```
|
72
72
|
|
73
|
-
To search for a
|
73
|
+
To search for a term in Wikipedia:
|
74
74
|
|
75
75
|
```
|
76
|
-
$ wi
|
77
|
-
Creating template wiki.yml
|
78
|
-
|
79
|
-
$ wi s
|
76
|
+
$ wi s Mr. Fixit
|
80
77
|
Writing to Mr. Fixit.json
|
81
78
|
```
|
82
79
|
|
83
80
|
To get your last 10 Wikipedia contributions:
|
84
81
|
|
85
82
|
```
|
86
|
-
$ wi c -t
|
87
|
-
Creating template wiki.yml
|
88
|
-
|
89
83
|
$ wi c
|
90
84
|
Writing to User:Example_User.contributions.en.wikipedia.org.json
|
91
85
|
```
|
92
86
|
|
87
|
+
To tune the request parameters create a config file with any of these:
|
88
|
+
```
|
89
|
+
$ wi g -t
|
90
|
+
|
91
|
+
$ wi c -t
|
92
|
+
|
93
|
+
$ wi s -t
|
94
|
+
```
|
95
|
+
|
96
|
+
|
93
97
|
Vim users should try [mediawiki.vim](https://en.wikipedia.org/wiki/Help:Text_editor_support#Vim) which adds syntax highlighting and abbreviations for wikitext files.
|
data/lib/wikian.rb
CHANGED
@@ -13,6 +13,7 @@ require 'fileutils'
|
|
13
13
|
require 'json'
|
14
14
|
require 'net/http'
|
15
15
|
require 'open-uri'
|
16
|
+
require 'tempfile'
|
16
17
|
require 'yaml'
|
17
18
|
|
18
19
|
class Wikian
|
@@ -43,6 +44,7 @@ class Wikian
|
|
43
44
|
api = Wikian::Get.new(args)
|
44
45
|
api.doit
|
45
46
|
api.extract_wikitext
|
47
|
+
api.save_metadata
|
46
48
|
elsif subcommand[0] == 's'
|
47
49
|
api = Wikian::Search.new(args)
|
48
50
|
api.doit
|
@@ -58,6 +60,15 @@ class Wikian
|
|
58
60
|
puts "#{e.class} #{e.message} in #{__FILE__}"
|
59
61
|
end
|
60
62
|
|
63
|
+
def self.meta_dir
|
64
|
+
'.wikian'
|
65
|
+
end
|
66
|
+
|
67
|
+
# file to store metadata of fetched articles
|
68
|
+
def self.meta_file
|
69
|
+
File.join(meta_dir, 'meta.yml')
|
70
|
+
end
|
71
|
+
|
61
72
|
def help
|
62
73
|
puts <<~eos
|
63
74
|
Usage:
|
@@ -67,6 +78,7 @@ class Wikian
|
|
67
78
|
-a, --append append the input file
|
68
79
|
-c, --captcha ID:MESSAGE captcha info
|
69
80
|
-d, --debug print debugging messages
|
81
|
+
-f, --force force post (this ignores edit conflicts)
|
70
82
|
-h, --help print this help message
|
71
83
|
-m, --message MESSAGE add a commit message (HIGHLY recommended)
|
72
84
|
-p, --prepend prepend the input file
|
@@ -76,7 +88,7 @@ class Wikian
|
|
76
88
|
-v, --version print version number
|
77
89
|
|
78
90
|
Subcommands:
|
79
|
-
c, contributions [N] get user last N contributions
|
91
|
+
c, contributions [N] get user last N contributions (defaults to #{Contributions::DEFAULT_MAX_CONTRIBUTIONS})
|
80
92
|
g, get get wikitext from a Wikipedia article
|
81
93
|
p, post post wikitext to a Wikipedia article
|
82
94
|
s, search search wikitext in Wikipedia
|
@@ -101,6 +113,9 @@ class Wikian
|
|
101
113
|
# append wikitext to section 2 of the article
|
102
114
|
wi post -a -s 2 Wikipedia:Sandbox.en.wikipedia.org.wiki
|
103
115
|
|
116
|
+
# search for multiple terms in Wikipedia
|
117
|
+
wi search term 1:term 2:term 3
|
118
|
+
|
104
119
|
# heavy use of the API may require cache validation
|
105
120
|
wi post -c 1234:someMessage spider-Man.wiki
|
106
121
|
|
data/lib/wikian/contributions.rb
CHANGED
@@ -11,17 +11,17 @@ class Wikian
|
|
11
11
|
|
12
12
|
max_contributions = args.find(&:numeric?) || DEFAULT_MAX_CONTRIBUTIONS
|
13
13
|
|
14
|
-
raise(BadUrlError, "Try passing the '-t' option") unless
|
14
|
+
raise(BadUrlError, "Try passing the '-t' option") unless config['meta']['site']
|
15
15
|
|
16
|
-
@output_file = 'User:' + ENV['WIKI_USER'] + '.contributions.' +
|
16
|
+
@output_file = 'User:' + ENV['WIKI_USER'] + '.contributions.' + config['meta']['site']
|
17
17
|
|
18
18
|
@params.merge!('ucuser' => ENV['WIKI_USER'], 'uclimit' => max_contributions, 'format' => Wikian::RESPONSE_FORMAT)
|
19
19
|
|
20
20
|
@query = @params.to_query
|
21
21
|
|
22
|
-
@api_url = URI("https://#{
|
22
|
+
@api_url = URI("https://#{config['meta']['site']}/w/api.php?#{query}")
|
23
23
|
rescue => e
|
24
|
-
puts "#{e.class} in #{__FILE__}
|
24
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
25
25
|
exit
|
26
26
|
end
|
27
27
|
|
data/lib/wikian/get.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
class Wikian
|
2
2
|
class WikianGetError < StandardError; end
|
3
|
-
class ExtractWikiError < WikianGetError; end
|
4
3
|
class ArgumentRequiredError < WikianGetError; end
|
5
4
|
|
6
5
|
class Get < Subcommand
|
7
|
-
attr_accessor :title
|
6
|
+
attr_accessor :title, :url, :latest_revision
|
8
7
|
|
9
8
|
def initialize(args)
|
10
9
|
raise ArgumentRequiredError if args.empty?
|
11
10
|
|
12
11
|
super
|
13
12
|
|
14
|
-
url = URI(args.find{|arg| arg =~ URI.regexp})
|
13
|
+
@url = URI(args.find{|arg| arg =~ URI.regexp})
|
15
14
|
|
16
15
|
raise BadUrlError unless url.path
|
17
16
|
|
@@ -25,7 +24,7 @@ class Wikian
|
|
25
24
|
|
26
25
|
@api_url = URI("https://#{url.host}/w/api.php?#{query}")
|
27
26
|
rescue => e
|
28
|
-
puts "#{e.class} in #{__FILE__}
|
27
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
29
28
|
exit
|
30
29
|
end
|
31
30
|
|
@@ -33,46 +32,43 @@ class Wikian
|
|
33
32
|
#
|
34
33
|
# return: nil
|
35
34
|
def extract_wikitext
|
36
|
-
|
37
|
-
raise ExtractWikiError, 'JSON response has no pages'
|
38
|
-
end
|
35
|
+
pages = JSON.parse(res_body).dig('query','pages')
|
39
36
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
File.open(wiki_file,'w') do |f|
|
47
|
-
content = revision.dig('slots', 'main', 'content') ||
|
48
|
-
revision.dig('slots', '*') ||
|
49
|
-
revision.dig('*')
|
50
|
-
STDERR.puts "Warning: nil 'content' in #{Wikian::CONFIG_FILE}" unless content
|
51
|
-
STDERR.puts "Writing to #{wiki_file}"
|
52
|
-
f.puts content
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
37
|
+
# Wikipedia is inconsistent in their value for 'pages', it's sometimes a hash, sometimes an array
|
38
|
+
@latest_revision = (pages.respond_to?(:keys) ? pages.values.first : pages.first)['revisions'].first
|
39
|
+
|
40
|
+
content = latest_revision.dig('slots', 'main', 'content') ||
|
41
|
+
latest_revision.dig('slots', '*') ||
|
42
|
+
latest_revision.dig('*')
|
56
43
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
44
|
+
wiki_file= File.basename(response_file, File.extname(response_file)) + '.wiki'
|
45
|
+
|
46
|
+
File.open(wiki_file,'w') do |f|
|
47
|
+
STDERR.puts "Warning: nil 'content' in #{Wikian::CONFIG_FILE}" unless content
|
48
|
+
STDERR.puts "Writing to #{wiki_file}"
|
49
|
+
f.puts content
|
64
50
|
end
|
65
51
|
|
66
52
|
rescue => e
|
67
|
-
puts "An error occurred while extracting the wikitext"
|
68
|
-
"Try using a new config file by pasing the '-t' option.",
|
69
|
-
"Or pass the '-d' option for debugging"
|
53
|
+
puts "An error occurred while extracting the wikitext"
|
70
54
|
exit
|
71
55
|
end
|
72
56
|
|
57
|
+
# save article metadata
|
58
|
+
#
|
59
|
+
# metadata like article timestamp is used to solve edit conflicts
|
60
|
+
def save_metadata
|
61
|
+
FileUtils.mkdir_p(Wikian.meta_dir)
|
62
|
+
|
63
|
+
metadata = File.exist?(Wikian.meta_file) ? YAML.load(File.open(Wikian.meta_file)) : {'meta' => {}}
|
64
|
+
metadata['meta'].merge!(title => {'timestamp' => latest_revision['timestamp']})
|
65
|
+
|
66
|
+
File.write(Wikian.meta_file, YAML.dump(metadata))
|
67
|
+
end
|
68
|
+
|
73
69
|
def template
|
74
70
|
<<~eos
|
75
|
-
# for a list of parameters to use here see: https://www.mediawiki.org/wiki/API:
|
71
|
+
# for a list of parameters to use here see: https://www.mediawiki.org/wiki/API:Revisions
|
76
72
|
meta:
|
77
73
|
headers:
|
78
74
|
user-agent: Wikian
|
@@ -83,9 +79,10 @@ class Wikian
|
|
83
79
|
- revisions
|
84
80
|
rvprop:
|
85
81
|
- content
|
86
|
-
|
87
|
-
|
88
|
-
|
82
|
+
- timestamp
|
83
|
+
#rvsection: # get specific sections
|
84
|
+
# - 0
|
85
|
+
# - 2
|
89
86
|
rvslots:
|
90
87
|
- main
|
91
88
|
formatversion:
|
data/lib/wikian/monkeypatches.rb
CHANGED
data/lib/wikian/post.rb
CHANGED
@@ -1,19 +1,24 @@
|
|
1
1
|
class Wikian
|
2
2
|
class WikianPostError < StandardError; end
|
3
3
|
class WikiFileError < WikianPostError; end
|
4
|
+
class WikiFileNameError < WikianPostError; end
|
5
|
+
class WikiMergeError < WikianPostError; end
|
4
6
|
|
5
7
|
class Post
|
6
8
|
attr_accessor :args, :baseurl, :header, :input_file, :debug, :login_token,
|
7
|
-
:login_cookie, :csrf_token, :csrf_cookie, :query, :body_text,
|
9
|
+
:login_cookie, :csrf_token, :csrf_cookie, :query, :body_text,
|
10
|
+
:username, :params, :latest_revision, :latest_content, :metadata
|
8
11
|
|
9
12
|
def initialize(args)
|
10
13
|
@args = args
|
11
14
|
|
15
|
+
long_to_short_options
|
16
|
+
|
12
17
|
# input wikitext file
|
13
|
-
@input_file = args.find{|f| File.exist? f}
|
14
|
-
raise WikiFileError unless input_file
|
18
|
+
raise WikiFileError unless @input_file = args.find{|f| File.exist? f}
|
15
19
|
|
16
|
-
site = input_file.match(/\.(.*)\.wiki/)[1
|
20
|
+
site = input_file.match(/\.(.*)\.wiki/)&.[](1)
|
21
|
+
raise(WikiFileNameError, "Use the Input file name convention <article_name>.<site>.wiki") unless site
|
17
22
|
|
18
23
|
@baseurl = "https://#{site}/w/api.php"
|
19
24
|
|
@@ -21,15 +26,20 @@ class Wikian
|
|
21
26
|
|
22
27
|
@username = ENV['WIKI_USER']
|
23
28
|
|
24
|
-
@debug = (args & %w(-d
|
29
|
+
@debug = (args & %w(-d)).length > 0 ? true : false
|
25
30
|
rescue => e
|
26
|
-
puts "#{e.class} in #{__FILE__}
|
31
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
27
32
|
exit
|
28
33
|
end
|
29
34
|
|
35
|
+
# transform long options like '--message' to short options like '-m'
|
36
|
+
def long_to_short_options
|
37
|
+
args.map! {|opt| opt[0,2] == '--' ? opt[1,2] : opt}
|
38
|
+
end
|
39
|
+
|
30
40
|
def post
|
31
41
|
# remove expired cookie
|
32
|
-
if expired_cookie? || args.have?(%w(-r
|
42
|
+
if expired_cookie? || args.have?(%w(-r))
|
33
43
|
FileUtils.rm_f(csrf_cookie_file)
|
34
44
|
end
|
35
45
|
|
@@ -41,10 +51,17 @@ class Wikian
|
|
41
51
|
|
42
52
|
get_csrf_cookie
|
43
53
|
end
|
44
|
-
get_csrf_token
|
45
|
-
|
46
54
|
build_query_string
|
47
55
|
|
56
|
+
get_latest_revision
|
57
|
+
|
58
|
+
if !args.include?('-f') && @body_text && Time.parse(params['starttimestamp']) < Time.parse(params['basetimestamp'])
|
59
|
+
puts "Edit conflict detected"
|
60
|
+
merge_versions(@body_text, latest_content)
|
61
|
+
end
|
62
|
+
|
63
|
+
get_csrf_token
|
64
|
+
|
48
65
|
upload_article
|
49
66
|
end
|
50
67
|
|
@@ -84,9 +101,19 @@ class Wikian
|
|
84
101
|
puts(res.body) if debug
|
85
102
|
end
|
86
103
|
|
104
|
+
def get_latest_revision
|
105
|
+
res = URI.open("#{baseurl}?action=query&prop=revisions&titles=#{params['title']}&rvslots=main&rvprop=content|timestamp&format=json")
|
106
|
+
@latest_revision = JSON.parse(res.read).dig('query', 'pages').values.first.dig('revisions').first
|
107
|
+
params['basetimestamp'] = latest_revision['timestamp']
|
108
|
+
@latest_content = latest_revision.dig('slots', 'main', 'content') ||
|
109
|
+
latest_revision.dig('slots', 'main', '*') ||
|
110
|
+
latest_revision.dig('slots', '*') ||
|
111
|
+
latest_revision.dig('*')
|
112
|
+
end
|
113
|
+
|
87
114
|
def get_csrf_token
|
88
115
|
puts("\nGetting csrf token using csrf cookies") if debug
|
89
|
-
url = URI("#{baseurl}?action=query&meta=tokens&format=json&
|
116
|
+
url = URI("#{baseurl}?action=query&meta=tokens&format=json&prop=info|revisions&rvprop=timestamp")
|
90
117
|
res = URI.open(url, header.merge('cookie' => csrf_cookie))
|
91
118
|
json = JSON.parse(res.read)
|
92
119
|
@csrf_token = json.dig('query','tokens','csrftoken')
|
@@ -94,32 +121,65 @@ class Wikian
|
|
94
121
|
end
|
95
122
|
|
96
123
|
def build_query_string
|
97
|
-
params={}
|
124
|
+
@params={}
|
98
125
|
params['action'] = 'edit'
|
99
126
|
params['format'] = Wikian::RESPONSE_FORMAT
|
100
127
|
params['title'] = input_file.sub(/\..*/,'')
|
128
|
+
@metadata = File.exist?(Wikian.meta_file) ? YAML.load(File.open(Wikian.meta_file)) : {}
|
129
|
+
params['starttimestamp'] =
|
130
|
+
if timestamp = metadata.dig('meta', params['title'], 'timestamp')
|
131
|
+
timestamp
|
132
|
+
else
|
133
|
+
FileUtils.mkdir_p(Wikian.meta_dir)
|
134
|
+
metadata = {'meta' => {params['title'] => {'timestamp' => File.mtime(input_file).utc.iso8601}}}
|
135
|
+
File.write(Wikian.meta_file, YAML.dump(metadata))
|
136
|
+
end
|
101
137
|
wikitext = File.read(input_file)
|
102
|
-
if args.have?(%w(-a
|
138
|
+
if args.have?(%w(-a))
|
103
139
|
params['appendtext'] = wikitext
|
104
|
-
elsif args.have?(%w(-p
|
140
|
+
elsif args.have?(%w(-p))
|
105
141
|
params['prependtext'] = wikitext
|
106
142
|
else
|
107
143
|
# pass the wikitext in request body
|
108
144
|
@body_text = wikitext
|
109
145
|
end
|
110
|
-
if args.have?(%w(-c
|
146
|
+
if args.have?(%w(-c))
|
111
147
|
params['captchaid'], params['captchaword'] = args[args.index('-c')+1].split(':')
|
112
148
|
end
|
113
|
-
if args.have?(%w(-m
|
149
|
+
if args.have?(%w(-m))
|
114
150
|
params['summary'] = args[args.index('-m')+1]
|
115
151
|
end
|
116
|
-
if args.have?(%w(-s
|
152
|
+
if args.have?(%w(-s))
|
117
153
|
params['section'] = args[args.index('-s')+1]
|
118
154
|
end
|
119
|
-
|
155
|
+
end
|
156
|
+
|
157
|
+
# merge two version with diff.
|
158
|
+
# TODO the merge command is ADDING differences, but it should MERGE differences.
|
159
|
+
def merge_versions(content_one, content_two)
|
160
|
+
tmp_local = Tempfile.open {|f| f.write content_one; f}
|
161
|
+
tmp_latest = Tempfile.open {|f| f.write content_two; f}
|
162
|
+
|
163
|
+
diff_cmd = "diff #{tmp_local.path} #{tmp_latest.path}"
|
164
|
+
system("#{diff_cmd}")
|
165
|
+
|
166
|
+
# raise error until the above TODO is solved
|
167
|
+
raise WikiMergeError, "Please merge with latest version and try again"
|
168
|
+
|
169
|
+
merge_cmd = "diff --line-format %L #{tmp_local.path} #{tmp_latest.path}"
|
170
|
+
@body_text = %x(#{merge_cmd})
|
171
|
+
rescue => e
|
172
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
173
|
+
exit
|
174
|
+
end
|
175
|
+
|
176
|
+
def update_metadata
|
177
|
+
metadata['meta'].merge!(params['title'] => {'timestamp' => Time.now.utc.iso8601})
|
178
|
+
File.write(Wikian.meta_file, YAML.dump(metadata))
|
120
179
|
end
|
121
180
|
|
122
181
|
def upload_article
|
182
|
+
@query = URI.encode_www_form(params)
|
123
183
|
puts("\nUploading the wiki article using csrf token #{csrf_token}") if debug
|
124
184
|
url = URI("#{baseurl}?#{query}")
|
125
185
|
req = Net::HTTP::Post.new(url, header.merge('cookie' => csrf_cookie, 'content-type' => 'application/x-www-form-urlencoded'))
|
@@ -136,6 +196,7 @@ class Wikian
|
|
136
196
|
"Try pasing the '-r' option to remove '#{csrf_cookie_file}'",
|
137
197
|
"Or pass '-d' option for debugging"
|
138
198
|
else
|
199
|
+
update_metadata
|
139
200
|
puts "Article uploaded"
|
140
201
|
end
|
141
202
|
end
|
data/lib/wikian/search.rb
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
class Wikian
|
2
2
|
class WikianGetError < StandardError; end
|
3
|
-
class
|
3
|
+
class MissingSearchTermError < WikianGetError; end
|
4
4
|
|
5
5
|
class Search < Subcommand
|
6
6
|
def initialize(args)
|
7
7
|
super
|
8
8
|
|
9
|
-
|
9
|
+
srsearch = non_opt_args.join(' ')
|
10
10
|
|
11
|
-
|
11
|
+
raise MissingSearchTermError if srsearch.empty?
|
12
|
+
|
13
|
+
@params.merge!('srsearch' => srsearch, 'format' => Wikian::RESPONSE_FORMAT)
|
14
|
+
|
15
|
+
@output_file = srsearch
|
12
16
|
|
13
17
|
@query = @params.to_query
|
14
18
|
|
15
|
-
@api_url = URI("https://#{
|
19
|
+
@api_url = URI("https://#{config['meta']['site']}/w/api.php?#{query}")
|
16
20
|
rescue => e
|
17
|
-
puts "#{e.class} in #{__FILE__}
|
21
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
18
22
|
exit
|
19
23
|
end
|
20
24
|
|
@@ -30,8 +34,9 @@ class Wikian
|
|
30
34
|
- query
|
31
35
|
list:
|
32
36
|
- search
|
33
|
-
srsearch: # text to search for. You can use multiple
|
34
|
-
|
37
|
+
#srsearch: # text to search for. You can use multiple
|
38
|
+
# - Mr. Fixit
|
39
|
+
# - Term 2
|
35
40
|
eos
|
36
41
|
end
|
37
42
|
end
|
data/lib/wikian/subcommand.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
#!/usr/bin/env -S ruby -W0
|
2
1
|
class Wikian
|
3
2
|
class WikianSubcommandError < StandardError; end
|
4
|
-
class MissingConfigFileError < WikianSubcommandError; end
|
5
3
|
class BadUrlError < WikianSubcommandError; end
|
4
|
+
class InitializeError < WikianSubcommandError; end
|
6
5
|
|
7
6
|
# class to be inherited by other Wikian classes
|
8
7
|
class Subcommand
|
9
|
-
attr_accessor :args, :res, :
|
8
|
+
attr_accessor :args, :res, :config, :query, :title, :api_url,
|
9
|
+
:debug, :output_file, :res_body
|
10
10
|
|
11
11
|
def initialize(args)
|
12
12
|
@args = args
|
@@ -19,12 +19,16 @@ class Wikian
|
|
19
19
|
|
20
20
|
@debug = (args & %w(-d --debug)).length > 0 ? true : false
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
@config =
|
23
|
+
if File.exist?(Wikian::CONFIG_FILE)
|
24
|
+
YAML.load(File.open(Wikian::CONFIG_FILE))
|
25
|
+
else
|
26
|
+
YAML.load(template)
|
27
|
+
end
|
24
28
|
|
25
29
|
# some params like 'titles' can contain multiple entries joined by '|'. More info in Wikipedia API docs
|
26
|
-
@params = Hash[
|
27
|
-
rescue
|
30
|
+
@params = Hash[config['api'].keys.zip(config['api'].values.map{|arr| arr.join("|")})]
|
31
|
+
rescue InitializeError => e
|
28
32
|
puts "#{e.class} try passing the '-t' option to generate #{Wikian::CONFIG_FILE} in #{__FILE__}"
|
29
33
|
exit
|
30
34
|
end
|
@@ -40,33 +44,34 @@ class Wikian
|
|
40
44
|
exit
|
41
45
|
end
|
42
46
|
|
47
|
+
# return args that dont start with '-'
|
48
|
+
def non_opt_args
|
49
|
+
args.reject {|p| p[0] == '-'}
|
50
|
+
end
|
51
|
+
|
43
52
|
# HTTP response file name. Its extension depends on the 'content-type' header
|
44
53
|
def response_file
|
45
|
-
output_file + '.' + res['content-type'].split('/').last.sub(/;.*/,'')
|
54
|
+
output_file + '.' + res.meta['content-type'].split('/').last.sub(/;.*/,'')
|
46
55
|
end
|
47
56
|
|
48
57
|
# write response in to `response_file`
|
49
58
|
def write_response
|
50
59
|
STDERR.puts "Writing to #{response_file}"
|
51
60
|
File.open(response_file, 'w') do |f|
|
52
|
-
f.puts prettify(
|
61
|
+
f.puts prettify(res_body)
|
53
62
|
end
|
54
63
|
end
|
55
64
|
|
56
65
|
def doit
|
57
66
|
puts api_url if debug
|
58
67
|
|
59
|
-
|
60
|
-
|
61
|
-
http = Net::HTTP.new(api_url.host, api_url.port)
|
62
|
-
|
63
|
-
http.use_ssl = true
|
68
|
+
@res=URI.open(api_url, config['meta']['headers'])
|
64
69
|
|
65
|
-
@res
|
70
|
+
@res_body = res.read
|
66
71
|
|
67
72
|
write_response
|
68
73
|
rescue => e
|
69
|
-
puts "#{e.class} in #{__FILE__}
|
74
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
70
75
|
exit
|
71
76
|
end
|
72
77
|
|
@@ -74,7 +79,7 @@ class Wikian
|
|
74
79
|
|
75
80
|
# if response is JSON prettify it, otherwise return it unchanged
|
76
81
|
def prettify(str)
|
77
|
-
res['content-type'].match?('json') ? JSON.pretty_generate(JSON.parse(str)) : str
|
82
|
+
res.meta['content-type'].match?('json') ? JSON.pretty_generate(JSON.parse(str)) : str
|
78
83
|
end
|
79
84
|
end
|
80
85
|
end
|
data/lib/wikian/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wikian
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sergioro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Get and update Wikipedia articles
|
14
14
|
email:
|