wikian 0.1.11 → 0.2.3
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 +35 -18
- 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 +20 -16
- 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: 6044c74a22baf5f757da1f68bc65f0758da46a41ee3567b19b0a61688d3b7034
|
4
|
+
data.tar.gz: 415490e2958ed48196c97ddbca6f3a86afff1c7ee535c9d1a1ed39bf3313bce8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3155eb0bc4f6a2a1648eb186072bda807b4af64ef79d9034e0c597900a5b2a9ed7b6d498a469545596673b37a0323360d59eef8f043b2710fcea462bf3ec7b55
|
7
|
+
data.tar.gz: cc705511bc328eeb488180ce7940f64eaa2e82f0b8302cbc26cb93e38aeff15b7f6ccd20b3f08d8ea5ec2dd2ab0eedeb9ca4297f01bae0b18a05995e5ec56f3e
|
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:
|
@@ -66,46 +77,52 @@ class Wikian
|
|
66
77
|
Options:
|
67
78
|
-a, --append append the input file
|
68
79
|
-c, --captcha ID:MESSAGE captcha info
|
69
|
-
-d, --debug
|
80
|
+
-d, --debug print debugging messages
|
81
|
+
-f, --force force post (this ignores edit conflicts)
|
82
|
+
-h, --help print this help message
|
70
83
|
-m, --message MESSAGE add a commit message (HIGHLY recommended)
|
71
84
|
-p, --prepend prepend the input file
|
72
85
|
-r, --remove-cookie remove API cookie
|
73
86
|
-s, --section NUMBER section to edit
|
74
87
|
-t, --template create template configuration file
|
75
|
-
-v, --version
|
88
|
+
-v, --version print version number
|
76
89
|
|
77
90
|
Subcommands:
|
78
|
-
c, contributions [N] get user last N contributions
|
79
|
-
g, get get wikitext
|
80
|
-
p, post post wikitext
|
81
|
-
s, search search wikitext
|
91
|
+
c, contributions [N] get user last N contributions (defaults to #{Contributions::DEFAULT_MAX_CONTRIBUTIONS})
|
92
|
+
g, get get wikitext from a Wikipedia article
|
93
|
+
p, post post wikitext to a Wikipedia article
|
94
|
+
s, search search wikitext in Wikipedia
|
82
95
|
|
83
96
|
Examples:
|
84
97
|
# create wiki.yml template
|
85
|
-
|
98
|
+
wi g -t
|
86
99
|
|
87
|
-
# download article
|
88
|
-
|
100
|
+
# download article
|
101
|
+
wi get -t
|
102
|
+
wi get https://en.wikipedia.org/wiki/Wikipedia:Sandbox
|
89
103
|
|
90
|
-
# upload file
|
91
|
-
|
104
|
+
# upload file
|
105
|
+
wi post Wikipedia:Sandbox.en.wikipedia.org.wiki
|
92
106
|
|
93
107
|
# upload file to Spanish Wikipedia
|
94
|
-
|
108
|
+
wi post Wikipedia:Sandbox.es.wikipedia.org.wiki
|
95
109
|
|
96
110
|
# upload file to English Wiktionary
|
97
|
-
|
111
|
+
wi post Wikipedia:Sandbox.en.wiktionary.org.wiki
|
112
|
+
|
113
|
+
# append wikitext to section 2 of the article
|
114
|
+
wi post -a -s 2 Wikipedia:Sandbox.en.wikipedia.org.wiki
|
98
115
|
|
99
|
-
#
|
100
|
-
|
116
|
+
# search for multiple terms in Wikipedia
|
117
|
+
wi search term 1:term 2:term 3
|
101
118
|
|
102
119
|
# heavy use of the API may require cache validation
|
103
|
-
|
120
|
+
wi post -c 1234:someMessage spider-Man.wiki
|
104
121
|
|
105
122
|
Comments:
|
106
123
|
Posted files must follow the convention:
|
107
|
-
<article_name>.<
|
108
|
-
where <
|
124
|
+
<article_name>.<site>.wiki
|
125
|
+
where <site> is a wikimedia site.
|
109
126
|
More info at: https://meta.wikimedia.org/wiki/Our_projects
|
110
127
|
eos
|
111
128
|
exit
|
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,11 @@
|
|
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
|
6
4
|
|
7
5
|
# class to be inherited by other Wikian classes
|
8
6
|
class Subcommand
|
9
|
-
attr_accessor :args, :res, :
|
7
|
+
attr_accessor :args, :res, :config, :query, :title, :api_url,
|
8
|
+
:debug, :output_file, :res_body
|
10
9
|
|
11
10
|
def initialize(args)
|
12
11
|
@args = args
|
@@ -19,11 +18,15 @@ class Wikian
|
|
19
18
|
|
20
19
|
@debug = (args & %w(-d --debug)).length > 0 ? true : false
|
21
20
|
|
22
|
-
|
23
|
-
|
21
|
+
@config =
|
22
|
+
if File.exist?(Wikian::CONFIG_FILE)
|
23
|
+
YAML.load(File.open(Wikian::CONFIG_FILE))
|
24
|
+
else
|
25
|
+
YAML.load(template)
|
26
|
+
end
|
24
27
|
|
25
28
|
# some params like 'titles' can contain multiple entries joined by '|'. More info in Wikipedia API docs
|
26
|
-
@params = Hash[
|
29
|
+
@params = Hash[config['api'].keys.zip(config['api'].values.map{|arr| arr.join("|")})]
|
27
30
|
rescue MissingConfigFileError => e
|
28
31
|
puts "#{e.class} try passing the '-t' option to generate #{Wikian::CONFIG_FILE} in #{__FILE__}"
|
29
32
|
exit
|
@@ -40,33 +43,34 @@ class Wikian
|
|
40
43
|
exit
|
41
44
|
end
|
42
45
|
|
46
|
+
# return args that dont start with '-'
|
47
|
+
def non_opt_args
|
48
|
+
args.reject {|p| p[0] == '-'}
|
49
|
+
end
|
50
|
+
|
43
51
|
# HTTP response file name. Its extension depends on the 'content-type' header
|
44
52
|
def response_file
|
45
|
-
output_file + '.' + res['content-type'].split('/').last.sub(/;.*/,'')
|
53
|
+
output_file + '.' + res.meta['content-type'].split('/').last.sub(/;.*/,'')
|
46
54
|
end
|
47
55
|
|
48
56
|
# write response in to `response_file`
|
49
57
|
def write_response
|
50
58
|
STDERR.puts "Writing to #{response_file}"
|
51
59
|
File.open(response_file, 'w') do |f|
|
52
|
-
f.puts prettify(
|
60
|
+
f.puts prettify(res_body)
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
56
64
|
def doit
|
57
65
|
puts api_url if debug
|
58
66
|
|
59
|
-
|
60
|
-
|
61
|
-
http = Net::HTTP.new(api_url.host, api_url.port)
|
62
|
-
|
63
|
-
http.use_ssl = true
|
67
|
+
@res=URI.open(api_url, config['meta']['headers'])
|
64
68
|
|
65
|
-
@res
|
69
|
+
@res_body = res.read
|
66
70
|
|
67
71
|
write_response
|
68
72
|
rescue => e
|
69
|
-
puts "#{e.class} in #{__FILE__}
|
73
|
+
puts "#{e.class} in #{__FILE__}", e.message
|
70
74
|
exit
|
71
75
|
end
|
72
76
|
|
@@ -74,7 +78,7 @@ class Wikian
|
|
74
78
|
|
75
79
|
# if response is JSON prettify it, otherwise return it unchanged
|
76
80
|
def prettify(str)
|
77
|
-
res['content-type'].match?('json') ? JSON.pretty_generate(JSON.parse(str)) : str
|
81
|
+
res.meta['content-type'].match?('json') ? JSON.pretty_generate(JSON.parse(str)) : str
|
78
82
|
end
|
79
83
|
end
|
80
84
|
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.3
|
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-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Get and update Wikipedia articles
|
14
14
|
email:
|