/
piglobot.rb
executable file
·79 lines (65 loc) · 2.16 KB
/
piglobot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
72
73
74
75
76
77
78
79
# encoding: utf-8
=begin
Copyright (c) 2007-2012 by Piglop
This file is part of Piglobot.
Piglobot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Piglobot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Piglobot. If not, see <http://www.gnu.org/licenses/>.
=end
require "rubygems"
require "bundler/setup"
require "mediawiki_api"
class Piglobot
attr_reader :api
def initialize
password = File.read(File.expand_path('../password', __FILE__)).strip
@api = MediawikiApi::Client.new "https://fr.wikipedia.org/w/api.php"
@api.log_in("Piglobot", password)
end
MONTHS = %w(janvier février mars avril mai juin
juillet août septembre octobre novembre décembre)
def format_date(time)
day = time.day
month = MONTHS[time.month - 1]
year = time.year
"{{date|#{day}|#{month}|#{year}}}"
end
def admin_names
admins = []
query = {"augroup" => "sysop", "aulimit" => 5000}
continue = {"continue" => ""}
loop do
response = @api.list(:allusers, query.merge(continue))
admins += response.data.map { |u| u["name"] }
continue = response["continue"]
break unless continue
end
admins
end
def last_contribution_time(user)
contribution = @api.list(:usercontribs, "ucuser" => user, "uclimit" => 1).data.first
if contribution
DateTime.parse(contribution["timestamp"])
else
nil
end
end
def last_log_event_time(user)
contribution = @api.list(:logevents, "leuser" => user, "lelimit" => 1).data.first
if contribution
DateTime.parse(contribution["timestamp"])
else
nil
end
end
def edit(page, text, summary)
@api.edit(title: page, text: text, summary: summary)
end
end