-
Notifications
You must be signed in to change notification settings - Fork 8
/
latin_music.py
executable file
·120 lines (102 loc) · 3.39 KB
/
latin_music.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
#! /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 11 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/latin music") == True:
return True
else:
return False
else:
return True
def cats_recursive(category):
"""Recursively goes through
categories. Almost TOO
straightforward.
"""
for item in category:
item = unicode(item)
if "Category:" in item:
cats_recursive(mwclient.listing.Category(site, item))
else:
pages.append(item)
def editor(text):
"""This function does the bulk of the
work. Requires one parameter, text.
"""
code = mwparserfromhell.parse(text)
already_tagged = False
for template in code.filter_templates():
if template.name == "WikiProject Latin music":
already_tagged = True
if template.name in ('WikiProject Latin America', 'WPLAMERICA', 'WP Latin America') and template.has_param("music"):
try:
music_imp = template.get('music-importance').value
template.remove("music-importance")
template.add("importance",music_imp)
except ValueError:
pass
template.name = "WikiProject Latin music"
template.remove("music")
if template.name in lists.bannershell_redirects:
x = template.get(1).value
for template in x.filter_templates():
if template.name in ('WikiProject Latin America', 'WPLAMERICA', 'WP Latin America') and template.has_param("music"):
try:
music_imp = template.get('music-importance').value
template.remove("music-importance")
template.add("importance",music_imp)
except ValueError:
pass
template.name = "WikiProject Latin music"
template.remove("music")
if already_tagged != True:
text = unicode(code)
else:
print "Skipping page since already tagged with WikiProject Latin music."
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:WikiProject Latin American music articles')
cats_recursive(zam)
print "Working on " + str(len(pages)) + " pages."
donenow = 5
for page in pages:
if sokay(donenow) == True:
talk = page
print "Working on " + talk.encode('UTF-8')
page = site.Pages[talk]
text = page.edit()
y = editor(text)
try:
#print y.encode('UTF-8')
page.save(y, summary = "Converting to {{[[Template:WikiProject Latin music|WikiProject Latin music]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/latin music|disable]])")
print talk.encode('UTF-8') + " saved."
except AttributeError:
print "Page save error; retrying."
try:
#print y.encode('UTF-8')
page.save(y, summary = "Converting to {{[[Template:WikiProject Latin music|WikiProject Latin music]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/latin music|disable]])")
print talk.encode('UTF-8') + " 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()