/
recreation.py
263 lines (205 loc) · 7.67 KB
/
recreation.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# -*- encoding:utf-8 -*-
"""
Copyright (C) 2013 Xavier Combelle
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>
Ce bot permet de lister la liste des pages recrées
usage typique
python -m recreation.recreation --prefix 'Utilisateur:Xavier Combelle Bot/Journal des recréations'
ou
python -m recreation.recreation --prefix 'Utilisateur:Xavier Combelle Bot/Journal des recréations' --verbose
avec recreation.timestamp.txt un fichier contenant une seule ligne au format
2013-08-10
qui est la dernière date à laquelle le bot a été lancée
"""
from __future__ import unicode_literals, print_function
from datetime import date, timedelta, datetime
import argparse
import sys
import locale
import pywikibot
from pywikibot import Page,Site
from pywikibot.exceptions import NoPage
from pywikibot.data import api
def api_query(params):
"""make a query
arguments:
params -- dictionary of params to pass to wikipedia api
result:
dict as json result of api
"""
query = api.Request(site=pywikibot.Site(),**params )
datas = query.submit()
return datas
def deletelog(title):
"""
query the delete log for the page
arguments:
title -- title of the page for which the log is required
result:
either
- the last entry of delete log for the page
- None if there is no delete log
"""
params = {
'action': 'query',
'list': "logevents",
'letitle': title,
'leprop':'title|timestamp',
'leaction':"delete/delete",
'lelimit':'1'
}
datas = api_query(params)
dl = datas['query']['logevents']
if dl:
return dl[0]
def creation_log(start,end):
"""
query the recent change for creation between the timestamp in parameter
arguments:
start -- time stamp of first creation (format YYYY-MM-DDTHH:MM:SSZ)"
end -- time stamp of last creation (format YYYY-MM-DDTHH:MM:SSZ)"
result:
list of the creation events occuring between the timestamp
"""
params = {
'action': 'query',
'list': "recentchanges",
'rctype': "new",
'rcnamespace': '0',
'rcprop':'title|user|timestamp',
'rcstart':end,
'rcend':start,
'rclimit':'10',
}
all_done = False
while not all_done :
if all_done:
break
datas = api_query(params)
import pprint
if datas == []:
break
data = datas['query']["recentchanges"]
for d in data:
yield d
if 'query-continue' in datas:
if 'recentchanges' in datas['query-continue']:
params['rccontinue'] = datas['query-continue']['recentchanges']['rccontinue']
else:
all_done = True
def to_date(day):
"""
convert a day from python time format to timestamp
arguments:
day -- python time format day
result:
timestamp (format YYYY-MM-DDTHH:MM:SSZ)
"""
return day.strftime("%Y-%m-%dT00:00:00Z")
def from_date(timestamp):
"""
convert a timestamp to python time format
arguments:
timestamp -- string (format YYYY-MM-DDTHH:MM:SSZ)
result:
python time format
"""
return datetime.strptime(timestamp,"%Y-%m-%dT%H:%M:%SZ")
def format_date(day,skip_day=False):
"""
format day in french human readable format
arguments:
day -- python date format
skip_day -- if True format "janvier 2013"
result:
the date formated of king
either:
- "1 janvier 2013"
- "janvier 2013"
depending on skip_day parameter
"""
if skip_day:
format_string = "{month} {year}"
else:
format_string = "{day} {month} {year}"
return format_string.format(day=day.day,
month={1:"janvier",
2:"février",
3:"mars",
4:"avril",
5:"mai",
6:"juin",
7:"juillet",
8:"août",
9:"septembre",
10:"octobre",
11:"novembre",
12:"décembre"}[day.month],
year = day.year)
def wiki_param(param):
"""
format a parameter such as it is well behaved as a parameter in a template
"""
if "=" in param:
return "1="+param
else:
return param
ONE_DAY = timedelta(1)
def process(day):
"""
one day bot processing
arguments:
day -- python date format
"""
if params.verbose:
print("processing Journal des recréations ({day})".format(day=format_date(day)))
start = to_date(day)
end = to_date(day+ONE_DAY)
result = "\n== {} ==\n".format(format_date(day))
for i,page in enumerate(creation_log(start,end),1):
if params.verbose:
print (i,page["timestamp"])
dl = deletelog(page["title"])
if dl:
r = ("* {{{{a-court|{title}}}}} <small>([[{pas}|PàS]])</small> supprimé le {date} recréé par {{{{u|{user}}}}} \n"
.format(title = wiki_param(page["title"]) ,
pas = wiki_param("Discussion:"+page["title"]+"/Suppression"),
user = wiki_param(page["user"]),
date = format_date(from_date(dl["timestamp"]))))
if params.verbose:
print(r)
result += r
page = Page(Site(), params.prefix+"/"+format_date(day,skip_day=True))
try:
result = page.get()+result
except NoPage:
pass
page.put(result,comment="Journal des recréations ({day})".format(day=format_date(day)))
if __name__ == "__main__" :
parser = argparse.ArgumentParser(description='recreaction bot for french wikipedia.')
parser.add_argument('--verbose', help="enable verbose output", action='store_true')
parser.add_argument('--prefix',
help="prefix of the list page of recreation example: 'Utilisateur:Xavier Combelle Bot/journal des recréations'",
action='store',
required = True)
params = parser.parse_args()
if sys.version_info < (3, 0):
params.prefix = params.prefix.decode(locale.getpreferredencoding())
Site().forceLogin()
end = datetime.today() - ONE_DAY
with open("recreation.timestamp.txt") as ts:
start = datetime.strptime(ts.read().strip(),"%Y-%m-%d")+ONE_DAY
while start <= end:
process(start)
with open("recreation.timestamp.txt","w") as ts:
print(start.strftime("%Y-%m-%d"),file=ts)
start+=ONE_DAY