-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBana.py
executable file
·308 lines (259 loc) · 11 KB
/
Bana.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding: utf-8 -*-
# MIT License
# Copyright (C) 2013-2014 sitic
import pywikibot
import mwparserfromhell as mwparser
import re
import dateutil.parser as dateparser
import dateutil.relativedelta as datedelta
import datetime
import locale
import redis
import sys
from Bali import AdtMain
discPageTitle = u'Wikipedia Diskussion:Hauptseite/Artikel des Tages/Vorschläge'
erledigtTemplate = (u'{{Erledigt|1= Gestriger AdT-Abschnitt, Baustein'
u' wurde [[WP:Bot|automatisch]] gesetzt. ~~~~}}\n\n')
erledigtComment = u'Bot: /* {section} */{andere} als erledigt markiert'
re_adt1 = re.compile(r'\[\[(?P<adt>[^\|\]]*)\|?[^\]]*?\]\]')
re_adt2 = re.compile(r'\:\s*(?P<adt>[^\[\|\]]*?)\s*==\n')
redisServer = 'tools-redis'
redisPort = 6379
redisDB = 9
rand_str = 'bceL8omhRhUIkx4KhGWPC6TLmq5IixQD7o5BId3x' # openssl rand -base64 30
class AdT_Verwaltung():
def __init__(self, do_hinweis):
self.dry = False # debug switch
self.do_hinweis = do_hinweis
self.site = pywikibot.Site()
self.site.login()
self.red = redis.StrictRedis(host=redisServer, port=redisPort,
db=redisDB)
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
self.today = datetime.date.today()
self.dayName = self.today.strftime('%A').decode('utf-8')
self.monthName = self.today.strftime('%B').decode('utf-8')
self.year = self.today.year
# 31.12.2013
self.adtDate = self.today.strftime('%d.%m.%Y').decode('utf-8')
# 31. Dezember 2013
self.snapDate = self.__format_date(self.today)
self.props = []
self.erl_props = []
self.dates = []
self.sections = []
pywikibot.output(u'\n\ninit complete: ' +
datetime.datetime.now()
.strftime('%d. %B %Y, %H:%M:%S').decode('utf-8'))
main_adt = AdtMain()
try:
main_adt.add_template()
except Exception as inst:
pywikibot.error(u'ERROR: ' + str(type(inst)))
pywikibot.error(inst)
try:
self.adt_disc()
except Exception as inst:
pywikibot.error(u'ERROR: ' + str(type(inst)))
pywikibot.error(inst)
try:
if self.do_hinweis:
self.add_templates()
else:
self.cleanup_templates()
except Exception as inst:
pywikibot.error(u'ERROR: ' + str(type(inst)))
pywikibot.error(inst)
def adt_disc(self): # NOQA
discPage = pywikibot.Page(self.site, discPageTitle)
section_count = 0
line_count = -1
header_line = None
sectionname = None
modsections = []
date = self.today + datedelta.relativedelta(years=1000)
line_list = discPage.text.splitlines(True)
for text_line in line_list:
line_count += 1
s = re.match(r'==\s*(?P<sectionname>[^=]+?)\s*==\n', text_line)
if s: # found section
# check previous section for AdT and erle
if header_line is not None:
lines = line_list[header_line:line_count]
# adt = self.__find_adt(lines)
if date.date() <= self.today and section_count < 6:
try:
self.__cleanup(lines, sectionname, date)
except Exception as inst:
pywikibot.error(u'ERROR: ' + str(type(inst)))
pywikibot.error(inst)
if not self.__erle_exists(lines):
line_list[line_count-1] += erledigtTemplate
modsections.append(sectionname)
else:
self.check_template(lines, sectionname, date)
section_count += 1
sectionname = s.group('sectionname')
code = mwparser.parse(sectionname)
sectionname = code.strip_code(normalize=True, collapse=True)
header_line = line_count
d = re.search(r'\d{1,2}\.\d{1,2}\.\d{2,4}\s?:', sectionname)
if d:
date = dateparser.parse(d.group()[:-1], dayfirst=True)
else:
date = self.today + datedelta.relativedelta(years=1000)
if self.do_hinweis:
# don't do erle
return
pywikibot.output(u'WD:AdT: Abschnitt(e) ' + unicode(modsections) +
u' als erledigt markiert')
if len(modsections) == 1:
lead_section = modsections[0]
comment = erledigtComment.format(section=lead_section, andere=u'')
elif len(modsections) > 1:
andere = u' sowie'
for i in range(1, len(modsections)):
section = modsections[i]
if i > 1:
andere += u','
andere += u' [[#' + unicode(section) + ']]'
lead_section = modsections[0]
comment = erledigtComment.format(section=lead_section,
andere=andere)
if len(modsections) != 0:
discPage.text = u''.join(line_list)
if not self.dry:
discPage.save(comment=comment, botflag=True, minor=True)
def __find_adt(self, line_list):
lines = u''.join(line_list)
code = mwparser.parse(lines)
for template in code.filter_templates(recursive=False):
if template.name.matches((u'AdT-Vorschlag', u'AdT-Vorschlag\n')):
l = re.search(r'\s*(?P<adt>.*)\s*\n?',
unicode(template.get(u'LEMMA').value))
d = re.search(r'\d{1,2}\.\d{1,2}\.\d{2,4}',
unicode(template.get(u'DATUM').value))
date = None
adtTitle = None
if d:
date = dateparser.parse(d.group(), dayfirst=True).date() #NOQA
if l:
adtTitle = l.group('adt').strip()
pywikibot.output(u'AdT Vorschlag: ' + adtTitle)
else:
pywikibot.error(u'Konnte AdT nicht finden in Abschnitt: ' +
lines)
return adtTitle
def __erle_exists(self, line_list):
code = mwparser.parse(u''.join(line_list))
for template in code.filter_templates(recursive=False):
if template.name.matches("Erledigt"):
return True
return False
def __cleanup(self, lines, sectionname, date):
adt = self.__find_adt(lines)
if adt is not None:
self.erl_props.append(adt)
def check_template(self, lines, sectionname, date):
if date == self.today + datedelta.relativedelta(years=1000):
date = None
adt = self.__find_adt(lines)
if adt is not None:
self.dates.append(date)
self.props.append(adt)
self.sections.append(sectionname)
def cleanup_templates(self):
for adt in self.erl_props:
if adt in self.props:
# mehrmals für AdT vorgeschlagen
continue
page = pywikibot.Page(self.site, adt, ns=1)
if not page.exists():
pywikibot.error(u'ERROR: disc for AdT-Vorschlag ' + adt
+ u' does not exist!')
return
oldtext = page.text
code = mwparser.parse(page.text)
for template in code.filter_templates(recursive=False):
if template.name.matches("AdT-Vorschlag Hinweis"):
code.remove(template)
pywikibot.output(adt +
u': {{AdT-Vorschlag Hinweis}} '
u'gefunden, entfernt')
page.text = unicode(code)
if page.text == oldtext:
continue
page.text = page.text.lstrip(u'\n')
pywikibot.showDiff(oldtext, page.text)
comment = u'Bot: [[Vorlage:AdT-Vorschlag Hinweis]] entfernt'
if not self.dry:
page.save(comment=comment, botflag=True, minor=True)
def add_templates(self): # NOQA
for adt, section, date in zip(self.props, self.sections, self.dates):
page = pywikibot.Page(self.site, adt, ns=1)
if not page.exists():
pywikibot.error(u'ERROR: disc for AdT-Vorschlag ' + adt
+ u' does not exist!')
return
oldtext = page.text
code = mwparser.parse(page.text)
found = False
for template in code.filter_templates(recursive=False):
if template.name.matches("AdT-Vorschlag Hinweis"):
found = True
if not template.has(u'Abschnitt'):
template.add(u'Abschnitt', section)
if template.has(u'Datum'):
tdate = self.__date_parser(template.get(u'Datum').value)
if dateparser.parse(tdate, dayfirst=True) <= date:
continue
else:
template.get(u'Datum').value = self.__format_date(
date)
template.get(u'Abschnitt').value = section
page.text = unicode(code)
if not found:
page.text = (u'{{AdT-Vorschlag Hinweis' +
self.__format_tempdate(date) +
u' | Abschnitt = ' + section + u'}}\n' +
page.text)
if page.text == oldtext:
continue
comment = (u'Bot: Dieser Artikel wurde für den ' +
self.__format_date(date) +
u' zum Artikel des Tages vorgeschlagen ([[WD:AdT#' +
section + u'|Diskussion]])')
pywikibot.showDiff(oldtext, page.text)
if not self.dry:
page.save(comment=comment, botflag=False, minor=False)
def __format_date(self, date):
return date.strftime('%-d. %B %Y').decode('utf-8')
def __format_tempdate(self, date):
if date is not None:
return u'| Datum = ' + self.__format_date(date)
else:
return u''
def __date_parser(self, date):
date = unicode(date)
dictionary = {
u'Januar': 'January',
u'Februar': 'February',
u'März': 'March',
u'Mai': 'May',
u'Juni': 'June',
u'Juli': 'July',
u'Oktober': 'October',
u'Dezember': 'December'
}
for lang, en in dictionary.items():
date = date.replace(lang, en)
return date
if __name__ == "__main__":
try:
if len(sys.argv) < 2:
AdT_Verwaltung(False)
else:
AdT_Verwaltung(True)
finally:
pywikibot.stopme()