-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotdrotate.py
199 lines (141 loc) · 6.3 KB
/
potdrotate.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
# vim: set fileencoding=utf-8 :
import pywikibot, mwparserfromhell
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
from itertools import groupby
from operator import itemgetter
import re
import cleandate
from cleanwikitext import simplify_wikisyntax
def dagen(today):
"""
The first POTD that will be copied over is today's picture.
>>> next(dagen(date(2016, 10, 20)))
datetime.date(2016, 10, 20)
The POTD furthest in the future that is to be copied over is as follows:
>>> [i for i in dagen(date(2016, 10, 20))][-1]
datetime.date(2016, 11, 18)
The number of days in January exceeds the number of days in February by a few,
this is a test to make sure that doesn't cause a conflict:
>>> [i for i in dagen(date(2016, 1, 31))][-1]
datetime.date(2016, 2, 27)
"""
d1 = today
d2 = today + relativedelta(days=-2, months=+1)
delta = d2 - d1
for i in range(delta.days + 1):
yield d1 + timedelta(days=i)
def potdArtikel(site, day):
name = potdArtikelnaam(day)
page = pywikibot.Page(site, name)
return potdDescription(page.text)
def potdDescription(text):
"""
>>> potdDescription('')
>>> potdDescription('{{Potd description|1=Een beschrijving.|2=nl|3=2016|4=10|5=07}}')
'Een beschrijving.'
"""
if text == '':
return None
return argumentFromTemplate(text, 'Potd description', '1')
def argumentFromTemplate(text, template, argument):
wikicode = mwparserfromhell.parse(text)
templates = wikicode.filter_templates()
description = [x for x in templates if x.name.matches(template)]
if description == None or description == []:
return None
sjabloon = description[0]
if not sjabloon.has_param(argument):
return None
return sjabloon.get(argument).value
def filename_from_potd_template(text):
"""
Newline in the comment:
>>> filename_from_potd_template("{{Potd filename|1= Hohenloher Freilandmuseum - Baugruppe Mühlental - Mahlmühle aus Weipertshofen - Flur - blaue Wand mit Feuerlöscher (2).jpg"+chr(10)+"<!--DON'T EDIT BELOW THIS LINE. IT FILLS OUT THE REST FOR YOU. "+chr(10)+"-->|2=2022|3=08|4=31}}")
'Hohenloher Freilandmuseum - Baugruppe Mühlental - Mahlmühle aus Weipertshofen - Flur - blaue Wand mit Feuerlöscher (2).jpg'
>>> filename_from_potd_template("{{Potd filename|1= Verschiedenfarbige Schwertlilie (Iris versicolor)-20200603-RM-100257.jpg"+chr(10)+"<!--DON'T EDIT BELOW THIS LINE. IT FILLS OUT THE REST FOR YOU. -->"+chr(10)+"|2=2022|3=08|4=28}}")
'Verschiedenfarbige Schwertlilie (Iris versicolor)-20200603-RM-100257.jpg'
If the template only contains a file name, don't attempt to decode parameters, but
only strip superfluous whitespace: (case taken from Template:Potd/2021-06-28)
>>> filename_from_potd_template("Zürich view Quaibrücke 20200702.jpg" + chr(10))
'Zürich view Quaibrücke 20200702.jpg'
Cyrillic character test:
>>> filename_from_potd_template("{{Potd filename|Тавче Гравче.jpg|2021|07|28}}")
'Тавче Гравче.jpg'
"""
# It's a mwparser object, make it pure text so it's accepted by re, among other reasons
value = str(text)
if '{{' in value:
value = str(argumentFromTemplate(text, 'Potd filename', '1'))
return re.compile('<!--.*?-->', re.DOTALL).sub('', value).strip()
def potdBestandsnaam(site, day):
name = potdBestandsnaamartikelnaam(day)
page = pywikibot.Page(site, name)
return filename_from_potd_template(page.text)
def potdArtikelnaam(day):
"""
>>> potdArtikelnaam(date(2016, 4, 1))
'Template:Potd/2016-04-01 (nl)'
"""
return "Template:Potd/%04d-%02d-%02d (nl)" % (day.year, day.month, day.day)
def potdBestandsnaamartikelnaam(day):
"""
>>> potdBestandsnaamartikelnaam(date(2016, 4, 1))
'Template:Potd/2016-04-01'
"""
return "Template:Potd/%04d-%02d-%02d" % (day.year, day.month, day.day)
def main(*args):
local_args = pywikibot.handle_args(args)
commons = pywikibot.Site(code="commons", fam="commons")
site = pywikibot.Site(code="nl", fam="wikipedia")
editSummary = 'Robot: Bijwerken afbeelding van de dag (%s, zie [[Gebruiker:Frank Geerlings/Toelichting/Bijwerken afbeelding van de dag|toelichting]])'
today = date.today()
metbeschrijvingen = map(lambda x: (x, x.day, potdArtikel(commons, x)), dagen(today))
bron = [a + (None,) if a[2] == None else a + (potdBestandsnaam(commons, a[0]),) for a in metbeschrijvingen if a[2]]
descriptionPage = pywikibot.Page(site, 'Sjabloon:Hoofdpagina - afbeelding van de dag - onderschrift/data')
descriptionText, updatedDays = getD(bron, descriptionPage)
descriptionChanged = descriptionPage.text != descriptionText
filePage = pywikibot.Page(site, 'Sjabloon:Hoofdpagina - afbeelding van de dag/data')
fileText = getFiletext(bron, filePage)
fileChanged = filePage.text != fileText
if fileChanged or descriptionChanged:
# Alleen allebei tegelijk opslaan, als de een dan ook de ander
descriptionPage.text = descriptionText
descriptionPage.save(editSummary % cleandate.readable_dates(updatedDays), minor=False)
filePage.text = fileText
filePage.save(editSummary % cleandate.readable_dates(updatedDays), minor=False)
if today in updatedDays:
refreshHomepage(site)
def refreshHomepage(site):
page = pywikibot.Page(site, 'Hoofdpagina')
site.purgepages([page])
def D(datum, beschrijving):
return u"{{Potd description|1=%s|2=nl|3=%04d|4=%02d|5=%02d}}\n " % (simplify_wikisyntax(beschrijving), datum.year, datum.month, datum.day)
def getD(bron, page):
wikicode = mwparserfromhell.parse(page.text)
switch = wikicode.filter_templates()[0]
updatedDays = []
for datum, dag, beschrijving, bestandsnaam in bron:
existing = switch.get('<!--%02d-->%d' % (dag, dag))
newValue = D(datum, beschrijving)
if existing.value == newValue:
continue
existing.value = newValue
updatedDays.append(datum)
newText = wikicode
return (newText, updatedDays)
def getFiletext(bron, filePage):
wikicode = mwparserfromhell.parse(filePage.text)
templates = wikicode.filter_templates()
multiview = [x for x in templates if x.name.matches('Multiview')][0]
for datum, dag, beschrijving, bestandsnaam in bron:
multiview.get(dag).value = multiviewRegel(dag, bestandsnaam)
newText = wikicode
return newText
def multiviewRegel(dag, bestandsnaam):
return u"<!--%02d-->[[Image:%s|245x180px|{{Hoofdpagina - afbeelding van de dag - onderschrift}}]]\n " % (dag, bestandsnaam)
if __name__ == "__main__":
try:
main()
except Exception:
pywikibot.error("Fatal error:", exc_info=True)