forked from theopolisme/theobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtafi_tagger.py
executable file
·132 lines (114 loc) · 5.62 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
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
#! /usr/bin/env python
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
def static_list():
"""A little function per request to generate a static
page of this week's TAFI nominations.
"""
final_contents = "=== Week {{subst:CURRENTWEEK}}'s TAFIs ===\n<sup>Last updated ~~~~~</sup>\n"
#http://en.wikipedia.org//w/api.php?action=query&prop=extracts&format=json&explaintext=&titles=Template%3ATAFI%2FBlurb%2Fthisweek
x = site.api(action='query',prop='extracts',explaintext='y',titles='Template:TAFI/Blurb/thisweek')
for key,contents in x['query']['pages'].items():
contents = contents['extract']
for line in contents.splitlines():
page = line.strip()
final_contents += "\n* [[" + page + "]]"
pg = site.Pages['Template:TAFI/Blurb/static']
pg.save(final_contents,summary='[[WP:BOT|Bot]]: Updating static TAFI listings')
def complete_static_list():
"""A function per request to generate a static
page of *all* TAFI nominations on the schedule page.
"""
final_contents = "== List of TAFIs ==\n<sup>Last updated ~~~~~</sup>__TOC__"
contents = site.Pages["Wikipedia:Today's articles for improvement/Schedule/real"].edit()
sections = re.findall(r"""(\[\[[^\n]*?)(?:;<big>|$)""",contents,flags=re.U | re.DOTALL)
for section in sections:
week = re.findall(r"""\|Week (.*?)\]\]""",section,flags=re.U)[0]
final_contents += "\n\n=== Week {0} ===".format(week)
articles = re.findall(r"""\{\{TAFI nom.*?\|.*?article=(.*?)\|""",section,flags=re.U | re.DOTALL)
for article in articles:
final_contents += "\n*[[" + article + "]]"
pg = site.Pages["Wikipedia:Today's articles for improvement/Schedule/static"]
pg.save(final_contents,summary='[[WP:BOT|Bot]]: Updating static TAFI listings')
def remove_old_week_from_schedule():
"""Removes the previous, now completed week from the schedule
and moves it to archive."""
try:
schedule = site.Pages["Wikipedia:Today's articles for improvement/Schedule/real"]
text = schedule.edit()
old_schedule_contents = re.findall("""(;<big>\[\[Wikipedia:Today's articles for improvement/""" + str(now.year) + r"/" + str((now.isocalendar()[1])-1) + r"""\|.*?\s*);<big>\[\[""", text, flags = re.DOTALL)[0]
new_text = text.replace(old_schedule_contents, "\n")
schedule.save(new_text,summary="[[WP:BOT|Bot]]: Removing completed week from schedule - week {0}.".format(str((now.isocalendar()[1])-1)))
schedule_archive = site.Pages["Wikipedia:Today's articles for improvement/Archives/{0} schedule".format(str(now.year))]
arch_text = schedule_archive.edit() + "\n\n" + old_schedule_contents.strip()
schedule_archive.save(arch_text,summary="[[WP:BOT|Bot]]: Moving completed week to archive - week {0}.".format(str((now.isocalendar()[1])-1)))
except IndexError:
print "Oh, drat, someone already archived the old week. No harm done!"
# 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.
global now
now = datetime.datetime.now()
# Runs the static_list() function per request
static_list()
# Runs the complete_static_list() function per request
complete_static_list()
# Removes the completed week from the schedule and moves it to archive
remove_old_week_from_schedule()
# 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 = page.edit()
if text[0] == "{" and text[2] != "I":
text = re.sub(r"}}", "}}\n{{TAFI}}", text, 1, flags = re.U)
else:
text = "{{TAFI}}\n" + text
print text[0:100] # debugging - shows the changes
print "Saving page " + pagen.encode("ascii", "replace") + " - added TAFI template."
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\}\}\n", "", 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()
try:
x = re.findall("\{\{Former TAFI\}\}", tt, flags=re.IGNORECASE)[0]
print "Talk page already tagged; skipping."
except:
tt = "{{Former TAFI|date=" + start_date.strftime('%B %d, %Y') + "}}\n" + tt
talk.save(tt,summary="Tagging page with {{[[Template:Former TAFI|Former TAFI]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/tafi|disable]])")