-
Notifications
You must be signed in to change notification settings - Fork 8
/
reggaeton_tagger.py
executable file
·106 lines (90 loc) · 3.11 KB
/
reggaeton_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
#! /usr/bin/env python
import mwclient
import mwparserfromhell
import sys
from theobot import bot
from theobot import password
from theobot import lists
# CC-BY-SA Theopolisme
# Task 5 on [[User:Theo's Little Bot]]
# Defining list.
pages = []
def sokay(donenow):
"""This function calls a subfunction
of the theobot module, checkpage().
"""
if donenow % 5 == 0:
if bot.checkpage("User:Theo's Little Bot/disable/reggaeton") == True:
return True
else:
return False
else:
return True
def cats_recursive(category):
"""Recursively goes through
categories. Almost TOO
straightforward.
"""
for item in category:
if "Category" in str(item):
cats_recursive(item)
else:
x = item.page_title
pages.append(x)
def editor(text):
"""This function does the bulk of the
work. Requires one parameter, text.
"""
code = mwparserfromhell.parse(text)
# This is used to check if we need to add the template.
has_been_tagged = False
for template in code.filter_templates():
if template.name in ('WikiProject Latin music', 'Latin music'):
has_been_tagged = True
if template.name in ('WikiProject Latin music', 'Latin music') and not template.has_param("reggaeton"):
template.add("reggaeton", "yes")
print "Reggaeton value added."
if template.name in lists.bannershell_redirects:
x = template.get(1).value
for template in x.filter_templates():
if template.name in ('WikiProject Latin music', 'Latin music'):
has_been_tagged = True
if template.name in ('WikiProject Latin music', 'Latin music') and not template.has_param("reggaeton"):
template.add("reggaeton", "yes")
print "Reggaeton value added."
text = unicode(code)
if has_been_tagged == False:
text = "{{WikiProject Latin music|class=|importance=|reggaeton=yes}}\n" + text
return text
def main():
global site
site = mwclient.Site('en.wikipedia.org')
site.login(password.username, password.password)
print "Getting category contents...this could take a while."
zam = mwclient.listing.Category(site, 'Category:Reggaeton')
cats_recursive(zam)
print "Working on " + str(len(pages)) + " pages."
donenow = 5
for page in pages:
if sokay(donenow) == True:
talk = u'Talk:' + page
print "Working on " + talk.encode('ascii', 'ignore')
page = site.Pages[talk]
text = page.edit()
y = editor(text)
try:
page.save(y, summary = "Adding reggaeton parameter to {{[[Template:WikiProject Latin music|WikiProject Latin music]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/reggaeton|disable]])")
print talk.encode('ascii', 'ignore') + " saved."
except AttributeError:
print "Page save error; retrying."
try:
page.save(y, summary = "Adding reggaeton parameter to {{[[Template:WikiProject Latin music|WikiProject Latin music]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/reggaeton|disable]])")
print talk.encode('ascii', 'ignore') + " saved."
except AttributeError:
print "Page skipped due to unknown error."
donenow = donenow + 1
elif sokay(donenow) == False:
print "Aw, snap, we were disabled. Quitting in 3...2...1..."
sys.exit()
if __name__ == '__main__':
main()