forked from theopolisme/theobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtafi_tagger.py
64 lines (56 loc) · 2.36 KB
/
tafi_tagger.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
import mwclient
import datetime
import re
from theobot import bot
from theobot import password
# CC-BY-SA Theopolisme
# Task 7 on [[User:Theo's Little Bot]]
def sokay(donenow):
"""Simple function to check checkpage.
This function calls a sub-function
of the theobot.bot module, checkpage().
"""
if donenow % 5 == 0:
if bot.checkpage("User:Theo's Little Bot/disable/tafi") == True:
return True
else:
return False
else:
return True
# Logs in to the site.
site = mwclient.Site('en.wikipedia.org')
site.login(password.username, password.password)
# Sets the timestamp from which we derive week numbers.
now = datetime.datetime.now()
# This loop adds the tags to the new week's articles.
for i in range(1,11):
if sokay(i+4) == True:
stringy = "Wikipedia:Today's articles for improvement/" + str(now.year) + "/" + str(now.isocalendar()[1]) + "/" + str(i)
editme = site.Pages[stringy].edit()
pagen = re.findall("\[\[(.*?)\]\]", editme)[0]
page = site.Pages[pagen]
try:
x = re.findall("\{\{TAFI\}\}", page.edit(), flags=re.IGNORECASE)[0]
print "Page already tagged; skipping."
except:
text = u"{{TAFI}}\n" + page.edit()
print "Saving page " + pagen + " - added TAFI template."
print text
page.save(text,summary="Adding [[WP:TAFI|Today's articles for improvement]] tag ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/tafi|disable]])")
# This loop removes the tags from the old week's articles.
for i in range(1,11):
if sokay(i+4) == True:
stringy = "Wikipedia:Today's articles for improvement/" + str(now.year) + "/" + str((now.isocalendar()[1])-1) + "/" + str(i)
editme = site.Pages[stringy].edit()
pagen = re.findall("\[\[(.*?)\]\]", editme)[0]
page = site.Pages[pagen]
text = re.sub(r"\{\{TAFI\}\}", "", page.edit())
print "Saving page " + pagen + " - removed TAFI template."
page.save(text,summary="Removing [[WP:TAFI|Today's articles for improvement]] tag ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/tafi|disable]])")
# This next set of instructions is for tagging the article's talk page.
start_date = now + datetime.timedelta(-7)
talk = site.Pages["Talk:" + pagen]
tt = talk.edit()
tt = "{{Former TAFI|date=" + start_date.strftime('%B %d, %Y') + "}}\n" + tt
print tt
talk.save(tt,summary="Tagging page with {{[[Template:Former TAFI|Former TAFI]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/tafi|disable]])")