-
Notifications
You must be signed in to change notification settings - Fork 8
/
ga_tagger.py
executable file
·128 lines (107 loc) · 3.43 KB
/
ga_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
import mwclient
import re
import settings
import sys
# CC-BY-SA Theopolisme
def cats_recursive(category):
"""Recursively goes through
categories. Almost TOO
straightforward.
Note that category must be
a STRING.
"""
pages = []
category = mwclient.listing.Category(site, category)
print category
for item in category:
if "Category:" in unicode(item):
cats_recursive(item)
else:
x = item.strip_namespace(item.name)
print x.encode('UTF-8', 'ignore')
pages.append(x)
return pages
def tagged_already(page):
"""Returns TRUE if page has been
tagged with {{Good article}} or
one of its redirects.
"""
check_page()
page = site.Pages[page]
text = page.edit()
regexp1 = re.compile(r'\{\{([Gg]ood [Aa]rticle|GA [Aa]rticle)\}\}')
if regexp1.search(text) is not None:
return True
else:
return False
def process_current_gas():
"""For all current GAs, makes sure
topicon is on article and, if not,
adds it.
"""
pages = cats_recursive('Category:Wikipedia good articles')
for talkpage in pages:
page = talkpage
print "Working on: " + page.encode('UTF-8', 'ignore')
if tagged_already(page) == False:
pagee = site.Pages[page]
text = pagee.edit()
text = "{{Good article}}\n" + text
pagee.save(text,summary="[[User:RileyBot|Bot]] trial: Adding {{[[Template:Good article|Good article]]}} topicon) ([[User:RileyBot/12|Task 12]] - [[User:RileyBot/Stop/12|disable]]")
global counts
counts['current-added'] = counts['current-added'] + 1
counts['total_edits'] = counts['total_edits'] + 1
else:
print "Page already tagged."
def ga_on_talk(page):
"""Returns TRUE if talk page
matches GA regex.
"""
page = "Talk:" + page
page = site.Pages[page]
text = page.edit()
regexp2 = re.compile(r'|(\s*)class(\s*)=(\s*)GA')
if regexp2.search(text) is not None:
return True
else:
return False
def process_delisted_gas():
"""Removes topicon from delisted
or former GA nominees.
"""
pages = cats_recursive('Category:Delisted good articles')
pages += cats_recursive('Category:Former good article nominees')
for page in pages:
print "Working on: " + page.encode('UTF-8', 'ignore')
if tagged_already(page) == True and ga_on_talk(page) == False:
pagee = site.Pages[page]
text = pagee.edit()
text_diff = pagee.edit()
text = re.sub(r'\{\{([Gg]ood [Aa]rticle|GA [Aa]rticle)\}\}', '', text)
if text != text_diff:
pagee.save(text,summary="[[User:RileyBot|Bot]] trial: Removing {{[[Template:Good article|Good article]]}} topicon) ([[User:RileyBot/12|Task 12]] - [[User:RileyBot/Stop/12|disable]]")
global counts
counts['former-removed'] = counts['former-removed'] + 1
counts['total_edits'] = counts['total_edits'] + 1
def check_page():
stop_page = site.Pages['User:RileyBot/Stop/12']
stop_page_text = stop_page.edit()
if stop_page_text.lower() != u'enable':
print "Check page disabled"
sys.exit(0)
def main():
"""This defines and fills a global
variable for the site, and then runs.
"""
print "Logging in as " + settings.username + "..."
global site
site = mwclient.Site('en.wikipedia.org')
site.login(settings.username, settings.password)
check_page()
global counts
counts = {'total_edits': 0, 'current-added': 0, 'former-removed': 0}
process_current_gas()
process_delisted_gas()
print "TOTAL EDITS MADE: " + counts['total_edits'] + "\nADDED: " + counts['current-added'] + "\nREMOVED: " + counts['former-removed']
if __name__ == '__main__':
main()