wikian 0.1.3 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 230aab5c1251d1a48b64b443def5c114335818903a163607352628929bb2ef65
4
- data.tar.gz: 8bcb57a4b979a1e76968919545c9596131079d8e635b6850b39f030c98670eaa
3
+ metadata.gz: 87a4af9592758ef39b75a05614ac443ba2fed75a618ae07cd684f0752ee4f617
4
+ data.tar.gz: 6b0c11ebc5322d0617b8be7fbda496375da3550184a23fedcaae54ae386d7a42
5
5
  SHA512:
6
- metadata.gz: 0cff3cf0dcac13ed75a2c4cc04bb66c368abde7091d3e291e1a4f985322bb227c49a5e9e2ba29eb6b5f56c38479608468956aea34155a3773eacdfc6f65efd36
7
- data.tar.gz: 170ae0a237c2b5bb8a25ea77b6f3a925ebcd701de134d1e02f6d6d4699f5072782849f835e87b84d7730c71bac57500dfbab68efef22bda1836b980d64589e33
6
+ metadata.gz: ecb8533aa37113d4649c0d5f3000932682dcc0fa0dee25b72ee81e4d193da0197a981d53d35a5372f869c918b4eb5d017382a27c169c70ff465e334a271bda79
7
+ data.tar.gz: 8f07fa08ffe918b7bea4fa032552c27b340bf80e03414022b67f1f8b684a0f71645d8d7530353757a01e55a2c2f2170dd6a2aacc4e22af8886315675d42fc7d5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wikian (0.1.3)
4
+ wikian (0.1.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Wikian
2
2
 
3
- Want to be happier while editing wiki files?
4
- Me too, that's why I use [Wikian](https://rubygems.org/gems/wikian):
3
+ Want to be a happier [Wikipedian](https://en.wikipedia.org/wiki/Wikipedia:Wikipedians)?
4
+ Then you want [Wikian](https://rubygems.org/gems/wikian), which lets you edit Wikipedia pages locally, with your favorite text editor.
5
+ To install it:
5
6
 
6
7
  ```
7
8
  $ gem install wikian
@@ -39,7 +40,7 @@ $ cat User:$WIKI_USER.en.wikitionary.org.wiki
39
40
  == Last section==
40
41
  testing
41
42
 
42
- $ wi post -p User:$WIKI_USER.en.wikitionary.org.wiki
43
+ $ wi post -a User:$WIKI_USER.en.wikitionary.org.wiki
43
44
  Article uploaded
44
45
  ```
45
46
 
@@ -55,12 +56,38 @@ Writing to Wikipedia:Sandbox.en.wikipedia.org.json
55
56
  Writing to Wikipedia:Sandbox.en.wikipedia.org.wiki
56
57
  ```
57
58
 
58
- You can then edit the the article in your favorite text editor and uploaded:
59
+ Then edit the article and upload it:
59
60
 
60
61
  ```bash
61
62
  $ wi post Wikipedia:Sandbox.en.wikipedia.org.wiki
62
63
  Article uploaded
63
64
  ```
64
65
 
65
- Vim users should try out [mediawiki.vim](https://en.wikipedia.org/wiki/Help:Text_editor_support#Vim) which defines syntax highlighting and abbreviations for wikitext files.
66
+ To append wikitext to section 2:
66
67
 
68
+ ```bash
69
+ $ wi post -s 2 Wikipedia:Sandbox.en.wikipedia.org.wiki
70
+ Article uploaded
71
+ ```
72
+
73
+ To search for a pattern in Wikipedia:
74
+
75
+ ```
76
+ $ wi search -t
77
+ Creating template wiki.yml
78
+
79
+ $ wi s
80
+ Writing to Mr. Fixit.json
81
+ ```
82
+
83
+ To get your last 10 Wikipedia contributions:
84
+
85
+ ```
86
+ $ wi c -t
87
+ Creating template wiki.yml
88
+
89
+ $ wi c
90
+ Writing to User:Example_User.contributions.en.wikipedia.org.json
91
+ ```
92
+
93
+ 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.
@@ -1,7 +1,9 @@
1
1
  $LOAD_PATH.unshift __dir__
2
2
 
3
3
  require 'wikian/subcommand'
4
+ require 'wikian/contributions'
4
5
  require 'wikian/get'
6
+ require 'wikian/monkeypatches'
5
7
  require 'wikian/post'
6
8
  require 'wikian/search'
7
9
  require 'wikian/version'
@@ -13,24 +15,6 @@ require 'net/http'
13
15
  require 'open-uri'
14
16
  require 'yaml'
15
17
 
16
- # external libraries
17
- require 'byebug'
18
-
19
- class Array
20
- # return true if `self` and `elms` have any element in common
21
- def have?(elms)
22
- (self & elms).length > 0 ? true : false
23
- end
24
- alias_method :has?, :have?
25
- end
26
-
27
- class Hash
28
- # return a query string representation of a hash
29
- def to_query
30
- URI.decode(URI.encode_www_form(self))
31
- end
32
- end
33
-
34
18
  class Wikian
35
19
  class WikianError < StandardError; end
36
20
  class UnknownSubcommandError < WikianError; end
@@ -47,11 +31,13 @@ class Wikian
47
31
  def run
48
32
  if args.have?(%w(-h --help))
49
33
  help
34
+ elsif args.have?(%w(-v --version))
35
+ version
50
36
  end
51
37
 
52
38
  @subcommand = args.shift
53
39
 
54
- raise(UnknownSubcommandError, "Unkown Subcommand") unless %w(g p s get post search).include?(subcommand)
40
+ raise(UnknownSubcommandError, "Unkown Subcommand") unless %w(c g p s contributions get post search).include?(subcommand)
55
41
 
56
42
  if subcommand[0] == 'g'
57
43
  api = Wikian::Get.new(args)
@@ -60,13 +46,16 @@ class Wikian
60
46
  elsif subcommand[0] == 's'
61
47
  api = Wikian::Search.new(args)
62
48
  api.doit
49
+ elsif subcommand[0] == 'c'
50
+ api = Wikian::Contributions.new(args)
51
+ api.doit
63
52
  else
64
53
  api = Wikian::Post.new(args)
65
54
  api.post
66
55
  end
67
56
 
68
57
  rescue UnknownSubcommandError => e
69
- puts "#{e.class} #{e.message}"
58
+ puts "#{e.class} #{e.message} in #{__FILE__}"
70
59
  end
71
60
 
72
61
  def help
@@ -81,13 +70,15 @@ class Wikian
81
70
  -m, --message MESSAGE add a commit message (HIGHLY recommended)
82
71
  -p, --prepend prepend the input file
83
72
  -r, --remove-cookie remove API cookie
73
+ -s, --section NUMBER section to edit
84
74
  -t, --template create template configuration file
85
75
  -v, --version
86
76
 
87
77
  Subcommands:
88
- g, get get wikitext file from a wikipedia article
89
- p, post post wikitext file to a wikipedia article
90
- s, search search wikitext file to a wikipedia article
78
+ c, contributions [N] get user last N contributions. N defaults to 20
79
+ g, get get wikitext file from a wikipedia article
80
+ p, post post wikitext file to a wikipedia article
81
+ s, search search wikitext file to a wikipedia article
91
82
 
92
83
  Examples:
93
84
  # create wiki.yml template
@@ -119,4 +110,9 @@ class Wikian
119
110
  eos
120
111
  exit
121
112
  end
113
+
114
+ def version
115
+ puts "wikian #{VERSION}"
116
+ exit
117
+ end
122
118
  end
@@ -0,0 +1,49 @@
1
+ class Wikian
2
+ class WikianGetError < StandardError; end
3
+ class ExtractWikiError < WikianGetError; end
4
+ class ArgumentRequiredError < WikianGetError; end
5
+
6
+ class Contributions < Subcommand
7
+ DEFAULT_MAX_CONTRIBUTIONS = 20
8
+
9
+ def initialize(args)
10
+ super
11
+
12
+ max_contributions = args.find(&:numeric?) || DEFAULT_MAX_CONTRIBUTIONS
13
+
14
+ raise(BadUrlError, "Try passing the '-t' option") unless yaml['meta']['site']
15
+
16
+ @output_file = 'User:' + ENV['WIKI_USER'] + '.contributions.' + yaml['meta']['site']
17
+
18
+ @params.merge!('ucuser' => ENV['WIKI_USER'], 'uclimit' => max_contributions, 'format' => Wikian::RESPONSE_FORMAT)
19
+
20
+ @query = @params.to_query
21
+
22
+ @api_url = URI("https://#{yaml['meta']['site']}/w/api.php?#{query}")
23
+ rescue => e
24
+ puts "#{e.class} in #{__FILE__}. #{e.message}"
25
+ exit
26
+ end
27
+
28
+ def template
29
+ <<~eos
30
+ meta:
31
+ http_method: get
32
+ site: en.wikipedia.org
33
+ headers:
34
+ #accept-encoding: gzip
35
+ user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
36
+ api:
37
+ action:
38
+ - query
39
+ list:
40
+ - usercontribs
41
+ ucprop:
42
+ - title
43
+ - comment
44
+ - timestamp
45
+ - sizediff
46
+ eos
47
+ end
48
+ end
49
+ end
@@ -2,7 +2,6 @@ class Wikian
2
2
  class WikianGetError < StandardError; end
3
3
  class ExtractWikiError < WikianGetError; end
4
4
  class ArgumentRequiredError < WikianGetError; end
5
- class BadUrlError < WikianGetError; end
6
5
 
7
6
  class Get < Subcommand
8
7
  attr_accessor :title
@@ -14,7 +13,7 @@ class Wikian
14
13
 
15
14
  url = URI(args.find{|arg| arg =~ URI.regexp})
16
15
 
17
- raise BadUrlError unless url
16
+ raise BadUrlError unless url.path
18
17
 
19
18
  @title = File.basename(url.path)
20
19
 
@@ -26,7 +25,7 @@ class Wikian
26
25
 
27
26
  @api_url = URI("https://#{url.host}/w/api.php?#{query}")
28
27
  rescue => e
29
- puts "#{e.class} #{e.message} in #{__FILE__}"
28
+ puts "#{e.class} in #{__FILE__}. #{e.message}"
30
29
  exit
31
30
  end
32
31
 
@@ -57,7 +56,6 @@ class Wikian
57
56
 
58
57
  # this is ugly, but Wikipedia is inconsistent in their JSON value for 'pages'. Sometimes it's a hash, sometimes it's an array.
59
58
  if pages.respond_to? :keys
60
- byebug
61
59
  create_wiki.call(pages.values.first['title'], pages.values.first['revisions'])
62
60
  else
63
61
  pages.each do |page|
@@ -68,7 +66,7 @@ class Wikian
68
66
  rescue => e
69
67
  puts "An error occurred while extracting the wikitext",
70
68
  "Try using a new config file by pasing the '-t' option.",
71
- "Or pass '-d' option for debugging"
69
+ "Or pass the '-d' option for debugging"
72
70
  exit
73
71
  end
74
72
 
@@ -84,15 +82,13 @@ class Wikian
84
82
  - revisions
85
83
  rvprop:
86
84
  - content
87
- #rvsection:
88
- # - 0
89
- # - 2
85
+ #rvsection: # get specific sections
86
+ # - 0
87
+ # - 2
90
88
  rvslots:
91
89
  - main
92
90
  formatversion:
93
91
  - 2
94
- format:
95
- - json
96
92
  eos
97
93
  end
98
94
  end
@@ -0,0 +1,24 @@
1
+ class Array
2
+ # return true if `self` and `elms` have any element in common
3
+ def have?(elms)
4
+ (self & elms).length > 0 ? true : false
5
+ end
6
+ alias_method :has?, :have?
7
+ end
8
+
9
+ class Hash
10
+ # return a query string representation of a hash
11
+ def to_query
12
+ URI.decode(URI.encode_www_form(self))
13
+ end
14
+ end
15
+
16
+ class String
17
+ # check is a string is numeric
18
+ #
19
+ # usage: 'asdf'.numeric? # => false
20
+ # '5.1'.numeric? # => true
21
+ def numeric?
22
+ Float(self) != nil rescue false
23
+ end
24
+ end
@@ -23,7 +23,7 @@ class Wikian
23
23
 
24
24
  @debug = (args & %w(-d --debug)).length > 0 ? true : false
25
25
  rescue => e
26
- puts "#{e.class} in #{__FILE__}"
26
+ puts "#{e.class} in #{__FILE__}. #{e.message}"
27
27
  exit
28
28
  end
29
29
 
@@ -107,12 +107,15 @@ class Wikian
107
107
  # pass the wikitext in request body
108
108
  @body_text = wikitext
109
109
  end
110
- if args.include?('-c')
110
+ if args.have?(%w(-c --captcha))
111
111
  params['captchaid'], params['captchaword'] = args[args.index('-c')+1].split(':')
112
112
  end
113
- if args.include?('-m')
113
+ if args.have?(%w(-m --message))
114
114
  params['summary'] = args[args.index('-m')+1]
115
115
  end
116
+ if args.have?(%w(-s --section))
117
+ params['section'] = args[args.index('-s')+1]
118
+ end
116
119
  @query = URI.encode_www_form(params)
117
120
  end
118
121
 
@@ -14,7 +14,7 @@ class Wikian
14
14
 
15
15
  @api_url = URI("https://#{yaml['meta']['site']}/w/api.php?#{query}")
16
16
  rescue => e
17
- puts "#{e.class} #{e.message} in #{__FILE__}"
17
+ puts "#{e.class} in #{__FILE__}. #{e.message}"
18
18
  exit
19
19
  end
20
20
 
@@ -30,10 +30,8 @@ class Wikian
30
30
  - query
31
31
  list:
32
32
  - search
33
- srsearch:
34
- - Craig Noone
35
- format:
36
- - json
33
+ srsearch: # text to search for. You can use multiple
34
+ - Mr. Fixit
37
35
  eos
38
36
  end
39
37
  end
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env -S ruby -W0
2
2
  class Wikian
3
+ class WikianSubcommandError < StandardError; end
4
+ class MissingConfigFileError < WikianSubcommandError; end
5
+ class BadUrlError < WikianSubcommandError; end
6
+
3
7
  # class to be inherited by other Wikian classes
4
8
  class Subcommand
5
9
  attr_accessor :args, :res, :yaml, :query, :title, :api_url, :debug, :output_file
@@ -15,10 +19,14 @@ class Wikian
15
19
 
16
20
  @debug = (args & %w(-d --debug)).length > 0 ? true : false
17
21
 
22
+ raise MissingConfigFileError unless File.exist?(Wikian::CONFIG_FILE)
18
23
  @yaml=YAML.load(File.open(Wikian::CONFIG_FILE))
19
24
 
20
25
  # some params like 'titles' can contain multiple entries joined by '|'. More info in Wikipedia API docs
21
26
  @params = Hash[yaml['api'].keys.zip(yaml['api'].values.map{|arr| arr.join("|")})]
27
+ rescue MissingConfigFileError => e
28
+ puts "#{e.class} try passing the '-t' option to generate #{Wikian::CONFIG_FILE} in #{__FILE__}"
29
+ exit
22
30
  end
23
31
 
24
32
  def make_template
@@ -60,7 +68,7 @@ class Wikian
60
68
 
61
69
  write_response
62
70
  rescue => e
63
- puts "#{e.class} #{e.message} in #{__FILE__}"
71
+ puts "#{e.class} in #{__FILE__}. #{e.message}"
64
72
  exit
65
73
  end
66
74
 
@@ -1,3 +1,3 @@
1
1
  class Wikian
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = %q{Get and edit wikipedia articles}
10
10
  spec.description = %q{Get and edit wikipedia articles}
11
- #spec.homepage = "TODO: Put your gem's website or public repo URL here."
11
+ spec.homepage = "https://sergioro.mx/posts/wikian"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
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.1.3
4
+ version: 0.1.8
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 00:00:00.000000000 Z
11
+ date: 2020-09-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Get and edit wikipedia articles
14
14
  email:
@@ -32,13 +32,15 @@ files:
32
32
  - exe/wikian
33
33
  - lib/.gitignore
34
34
  - lib/wikian.rb
35
+ - lib/wikian/contributions.rb
35
36
  - lib/wikian/get.rb
37
+ - lib/wikian/monkeypatches.rb
36
38
  - lib/wikian/post.rb
37
39
  - lib/wikian/search.rb
38
40
  - lib/wikian/subcommand.rb
39
41
  - lib/wikian/version.rb
40
42
  - wikian.gemspec
41
- homepage:
43
+ homepage: https://sergioro.mx/posts/wikian
42
44
  licenses:
43
45
  - MIT
44
46
  metadata: {}