-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDanu.py
executable file
·157 lines (134 loc) · 5.55 KB
/
Danu.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# MIT License
# Copyright (C) 2013-2014 sitic
import pywikibot
import pywikibot.date
import time
#what should be snapshoted?
mainPageTitle = {
'en' : u'Main Page',
'de' : u'Wikipedia:Hauptseite'
}
#will be added at the top of the snapshoted page
archivePageIntro = {
'en' : u'',
'de' : u'{{{{Wikipedia:Hauptseite/Archiv/Vorlage|Tag={day}|Monat={month}|Jahr={year}}}}}\n{{{{bots|denyscript=delinker}}}}\n'
}
#where to put the snapshoted page
archiveTitlePrefix = {
'en' : u'Wikipedia:Main Page history/',
'de' : u'Wikipedia:Hauptseite/Archiv/'
}
#script will generate pages archiveTitlePrefix + localize(dateformat)
dateformat = {
'en' : u'{year} {monthName} {day}',
'de' : u'{day}. {monthName} {year}'
}
#where to update the template
templateTitle = {
'en' : u'Template:Main Page history',
'de' : u'Wikipedia:Hauptseite/Archiv'
}
archiveComment = {
'en' : u'Bot: creating snapshot of the current [[Main Page]]',
'de' : u'[[WP:Bot|Bot]]: erstelle Abbild der heutigen [[Wikipedia:Hauptseite|Hauptseite]]'
}
newMonthComment = {
'en' : u'Bot: adding links to next month',
'de' : u'[[WP:Bot|Bot]]: Vorbereitung für den nächsten Monat'
}
class SnapMain():
def __init__(self):
self.site = pywikibot.Site()
self.site.login()
self.day = time.localtime().tm_mday
self.month = time.localtime().tm_mon
self.monthName = pywikibot.date.monthName(self.site.language(),
self.month)
if self.month == 12: # end of the year?
self.nextmonthName = pywikibot.date.monthName(self.site.language(),
1)
else:
self.nextmonthName = pywikibot.date.monthName(self.site.language(),
self.month + 1)
self.year = time.localtime().tm_year
self.nextyear = time.localtime().tm_year + 1
self.snap()
self.maxDays = pywikibot.date.getNumberOfDaysInMonth(
time.localtime().tm_mon)
if time.localtime().tm_mday == self.maxDays: #end of the month?
pass
#self.new_month()
def format_date(self, day, monthName, year):
"""
return a string with the formated date (e.g. u'31. Dezember 2013')
"""
l_dateformat = pywikibot.translate(self.site, dateformat,
fallback=False)
return l_dateformat.format(day=day, monthName=monthName, year=year)
def isleapyear(self, n):
if n % 400 == 0:
return True
if n % 100 == 0:
return False
if n % 4 == 0:
return True
else:
return False
def snap(self):
pywikibot.output(u'creating snapshot for today: ' +
self.format_date(self.day, self.monthName, self.year))
l_mainPageTitle = pywikibot.translate(self.site, mainPageTitle,
fallback=False)
l_archiveTitlePrefix = pywikibot.translate(self.site,
archiveTitlePrefix, fallback=False)
l_archivePageIntro = pywikibot.translate(self.site, archivePageIntro,
fallback=False)
l_archiveComment = pywikibot.translate(self.site, archiveComment,
fallback=False)
l_archivePageIntro = l_archivePageIntro.format(day=time.strftime('%d'),
month=time.strftime('%m'), year=self.year)
mainPage = pywikibot.Page(self.site, l_mainPageTitle)
date = self.format_date(self.day, self.monthName, self.year)
archivePage = pywikibot.Page(self.site, l_archiveTitlePrefix + date)
archivePage.text = pywikibot.textlib.removeLanguageLinks(mainPage.expand_text())
archivePage.text = pywikibot.textlib.removeCategoryLinks(archivePage.text)
archivePage.text = l_archivePageIntro + archivePage.text
archivePage.save(comment=l_archiveComment, botflag=True, minor=False, force=True)
def new_month(self):
pywikibot.output(u'new month, updating template')
l_archiveTitlePrefix = pywikibot.translate(self.site,
archiveTitlePrefix, fallback=False)
l_templateTitle = pywikibot.translate(self.site, templateTitle,
fallback=False)
l_newMonthComment = pywikibot.translate(self.site, newMonthComment,
fallback=False)
templatePage = pywikibot.Page(self.site, l_templateTitle)
if time.localtime().tm_mon == 12: # end of the year?
newYearTable = u'\n\n\n{{|\n|+ \'\'\'{year}\'\'\'\n|}}'
templatePage.text += newYearTable.format(year=self.nextyear)
self.year = self.nextyear
maxDays = 31 #January
else:
#check for leap year, getNumberOfDaysInMonth() does not do that
if self.isleapyear(self.year) and time.localtime().tm_mon == 1:
maxDays = 29
else:
maxDays = pywikibot.date.getNumberOfDaysInMonth(
time.localtime().tm_mon + 1)
if self.format_date(1, self.nextmonthName, self.year) in templatePage.text:
#template page is already up to date
return
templatePage.text = templatePage.text[:-1] + u'-\n| ' + self.nextmonthName
for i in range(1, maxDays + 1):
templatePage.text += u'|| [[' + l_archiveTitlePrefix +\
self.format_date(i, self.nextmonthName, self.year) +\
u'|' + i.__str__() + u']]'
templatePage.text += u'\n|}'
templatePage.save(comment=l_newMonthComment, botflag=True, minor=False)
if __name__ == "__main__":
try:
SnapMain()
finally:
pywikibot.stopme()