This repository has been archived by the owner on Apr 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
clean_sandbox.py
210 lines (195 loc) · 9.44 KB
/
clean_sandbox.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
# -*- coding: utf-8 -*-
"""
This bot cleans a sandbox by replacing the current contents with predefined
text.
This script understands the following command-line arguments:
-hours:# Use this parameter if to make the script repeat itself
after # hours. Hours can be defined as a decimal. 0.01
hours are 36 seconds; 0.1 are 6 minutes.
-delay:# Use this parameter for a wait time after the last edit
was made. If no parameter is given it takes it from
hours and limits it between 5 and 15 minutes.
The minimum delay time is 5 minutes.
"""
#
# (C) Leonardo Gregianin, 2006
# (C) Wikipedian, 2006-2007
# (C) Andre Engels, 2007
# (C) Siebrand Mazeland, 2007
# (C) xqt, 2009
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import time
import wikipedia as pywikibot
content = {
'als':u'{{subst:/Vorlage}}',
'ar': u'{{من فضلك اترك هذا السطر ولا تعدله (عنوان ساحة التجربة)}}\n<!-- مرحبا! خذ راحتك في تجربة مهارتك في التنسيق والتحرير أسفل هذا السطر. هذه الصفحة لتجارب التعديل ، سيتم تفريغ هذه الصفحة كل 6 ساعات. -->',
'bar':u'{{Bitte erst NACH dieser Zeile schreiben! (Begrüßungskasten)}}\r\n',
'cs': u'{{subst:/uhrabat}}',
'da': u'{{subst:Sandkasse tekst}}',
'de': u'{{Bitte erst NACH dieser Zeile schreiben! (Begrüßungskasten)}}\r\n',
'en': u'{{Please leave this line alone (sandbox heading)}}\n<!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->',
'fa': u'{{subst:User:Amirobot/sandbox}}',
'fi': u'{{subst:Hiekka}}',
'he': u'{{ארגז חול}}\n<!-- נא לערוך מתחת לשורה זו בלבד, תודה. -->',
'id': u'{{Bakpasir}}\n<!-- Uji coba dilakukan di baris di bawah ini -->',
'it': u'{{sandbox}}<!-- Scrivi SOTTO questa riga senza cancellarla. Grazie. -->',
'ja': u'{{subst:サンドボックス}}',
'ko': u'{{연습장 안내문}}',
'ksh':u'{{subst:/Schablon}}',
'nds':u'{{subst:/Vörlaag}}',
'nl': u'{{subst:Wikipedia:Zandbak/schoon zand}}',
'no': u'{{Sandkasse}}\n<!-- VENNLIGST EKSPERIMENTER NEDENFOR DENNE SKJULTE TEKSTLINJEN! SANDKASSEMALEN {{Sandkasse}} SKAL IKKE FJERNES! -->}}',
'nn': u'{{sandkasse}}\n<!-- Ver snill og IKKJE FJERN DENNE LINA OG LINA OVER ({{sandkasse}}) Nedanføre kan du derimot ha det artig og prøve deg fram! Lykke til! :-) -->',
'pl': u'{{Prosimy - NIE ZMIENIAJ, NIE KASUJ, NIE PRZENOŚ tej linijki - pisz niżej}}',
'pt': u'<!--não apague esta linha-->{{página de testes}}<!--não apagar-->\r\n',
'commons': u'{{Sandbox}}\n<!-- Please edit only below this line. -->',
'ru': u'{{/Пишите ниже}}\n<!-- Не удаляйте, пожалуйста, эту строку, тестируйте ниже -->',
'sr': u'{{песак}}\n<!-- Молимо, испробавајте испод ове линије. Хвала. -->',
'sv': u'{{subst:Sandlådan}}',
'th': u'{{กระบะทราย}}\n<!-- กรุณาอย่าแก้ไขบรรทัดนี้ ขอบคุณครับ/ค่ะ -- Please leave this line as they are. Thank you! -->',
'zh': u'{{subst:User:Sz-iwbot/sandbox}}\r\n',
}
msg = {
'als':u'Bötli: Sandchaschte iigebnet.',
'ar': u'روبوت: هذه الصفحة سيتم تفريغها تلقائيا',
'bar':u'Bot: Spielwiesn gmaht.',
'cs': u'Uhrabání pískoviště',
'da': u'Bot: Nyt sand (fra[[Skabelon:Sandkasse tekst]])',
'de': u'Bot: Setze Spielwiese zurück.',
'en': u'Robot: Automatically cleaned',
'fa': u'ربات:صفحه به طور خودکار تميز شد',
'fi': u'Botti siivosi hiekkalaatikon.',
'he': u'בוט: דף זה ינוקה אוטומטית.',
'id': u'Bot: Tata ulang',
'it': u'Bot: pulitura sandbox',
'ja': u'ロボットによる: 砂場ならし',
'ko': u'로봇: 연습장 비움',
'ksh':u'Bot: allt Zeush fott gedunn.',
'nds':u'Bot: Speelwisch leddig maakt.',
'nl': u'Bot: automatisch voorzien van schoon zand.',
'no': u'bot: Rydder sandkassa.',
'pl': u'Robot czyści brudnopis',
'pt': u'Bot: Limpeza da página de testes',
'commons': u'Bot: This page will automatically be cleaned.',
'ru': u'Бот: очистка песочницы',
'sr': u'Чишћење песка',
'sv': u'Robot krattar sandlådan.',
'th': u'โรบอต: กำลังจะเก็บกวาดหน้านี้โดยอัตโนมัติ',
'zh': u'Bot: 本页被自动清理',
}
sandboxTitle = {
'als':u'Project:Sandchaschte',
'ar': u'Project:ساحة التجربة',
'bar':u'Project:Spielwiese',
'cs': u'Project:Pískoviště',
'da': u'Project:Sandkassen',
'de': u'Project:Spielwiese',
'en': u'Project:Sandbox',
'fa': u'Project:صفحه تمرین',
'fr': u'Project:Bac à sable',
'fi': u'Project:Hiekkalaatikko',
'he': u'Project:ארגז חול',
'id': u'Project:Bak pasir',
'it': u'Project:Pagina delle prove',
'ja': u'Project:サンドボックス',
'ko': u'Project:연습장',
'ksh':u'Project:Shpillplaz',
'nds':u'Project:Speelwisch',
'nl': u'Project:Zandbak',
'no': u'Project:Sandkasse',
'pl': u'Project:Brudnopis',
'pt': u'Project:Página de testes',
'commons': u'Project:Sandbox',
'ru': u'Project:Песочница',
'sr': u'Project:Песак',
'sv': u'Project:Sandlådan',
'th': u'Project:ทดลองเขียน',
'zh': u'Project:沙盒',
}
class SandboxBot:
def __init__(self, hours, no_repeat, delay):
self.hours = hours
self.no_repeat = no_repeat
if delay == None:
self.delay = min(15, max(5, int(self.hours *60)))
else:
self.delay = max(5, delay)
def run(self):
def minutesDiff(time1, time2):
if type(time1) is long:
time1 = str(time1)
if type(time2) is long:
time2 = str(time2)
t1 = (((int(time1[0:4])*12+int(time1[4:6]))*30+int(time1[6:8]))*24+int(time1[8:10])*60)+int(time1[10:12])
t2 = (((int(time2[0:4])*12+int(time2[4:6]))*30+int(time2[6:8]))*24+int(time2[8:10])*60)+int(time2[10:12])
return abs(t2-t1)
mySite = pywikibot.getSite()
while True:
wait = False
now = time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime())
localSandboxTitle = pywikibot.translate(mySite, sandboxTitle)
if type(localSandboxTitle) is list:
titles = localSandboxTitle
else:
titles = [localSandboxTitle,]
for title in titles:
sandboxPage = pywikibot.Page(mySite, title)
try:
text = sandboxPage.get()
translatedContent = pywikibot.translate(mySite, content)
translatedMsg = pywikibot.translate(mySite, msg)
subst = 'subst:' in translatedContent
if text.strip() == translatedContent.strip():
pywikibot.output(u'The sandbox is still clean, no change necessary.')
elif subst and sandboxPage.userName() == mySite.loggedInAs():
pywikibot.output(u'The sandbox might be clean, no change necessary.')
elif text.find(translatedContent.strip()) <> 0 and not subst:
sandboxPage.put(translatedContent, translatedMsg)
pywikibot.output(u'Standard content was changed, sandbox cleaned.')
else:
diff = minutesDiff(sandboxPage.editTime(), time.strftime("%Y%m%d%H%M%S", time.gmtime()))
#Is the last edit more than 5 minutes ago?
if diff >= self.delay:
sandboxPage.put(translatedContent, translatedMsg)
else: #wait for the rest
pywikibot.output(u'Sleeping for %d minutes.' % (self.delay-diff))
time.sleep((self.delay-diff)*60)
wait = True
except pywikibot.EditConflict:
pywikibot.output(u'*** Loading again because of edit conflict.\n')
if self.no_repeat:
pywikibot.output(u'\nDone.')
return
elif not wait:
if self.hours < 1.0:
pywikibot.output('\nSleeping %s minutes, now %s' % ((self.hours*60), now) )
else:
pywikibot.output('\nSleeping %s hours, now %s' % (self.hours, now) )
time.sleep(self.hours * 60 * 60)
def main():
hours = 1
delay = None
no_repeat = True
for arg in pywikibot.handleArgs():
if arg.startswith('-hours:'):
hours = float(arg[7:])
no_repeat = False
elif arg.startswith('-delay:'):
delay = int(arg[7:])
else:
pywikibot.showHelp('clean_sandbox')
return
bot = SandboxBot(hours, no_repeat, delay)
try:
bot.run()
except KeyboardInterrupt:
pywikibot.output('\nQuitting program...')
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()