forked from theopolisme/theobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
australia_roads.py
120 lines (102 loc) · 3.66 KB
/
australia_roads.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
import mwclient
import mwparserfromhell
import sys
import re
from theobot import bot
from theobot import password
# CC-BY-SA Theopolisme
# Task 4 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/australia roads") == True:
return True
else:
return False
else:
return True
def cats_recursive(category):
"""Recursively goes through
categories. Almost TOO
straightforward.
"""
str = 'Jubilee 150 Walkway'
for item in category:
if item.find(str) != -1:
pass
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_compare = mwparserfromhell.parse(text)
code = mwparserfromhell.parse(text)
for template in code.filter_templates():
if template.name in ('WP Australia', 'WP Australian music', 'WPAUS', 'WPAUSTRALIA', 'WPAustralia', 'WikiProject Australia') and not template.has_param("road"):
template.add("road", "yes")
print "Road value added."
if template.name in ('WP Australia', 'WP Australian music', 'WPAUS', 'WPAUSTRALIA', 'WPAustralia', 'WikiProject Australia') and not template.has_param("road-importance"):
try:
xyz = template.get("importance").value
template.add("road-importance", xyz)
print "Road-importance value added."
except:
print "No importance to add."
if template.name == 'WikiProjectBannerShell':
x = template.get(1).value
for template in x.filter_templates():
if template.name in ('WP Australia', 'WP Australian music', 'WPAUS', 'WPAUSTRALIA', 'WPAustralia', 'WikiProject Australia') and not template.has_param("road"):
template.add("road", "yes")
print "Road value added."
if template.name in ('WP Australia', 'WP Australian music', 'WPAUS', 'WPAUSTRALIA', 'WPAustralia', 'WikiProject Australia') and not template.has_param("road-importance"):
try:
xyz = template.get("importance").value
template.add("road-importance", xyz)
print "Road-importance value added."
except:
print "No importance to add."
text = unicode(code)
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:Roads in Australia')
cats_recursive(zam)
print "Working on " + str(len(pages)) + " pages."
donenow = 5
y = pages.index('Thomas Price')
y = y-5
del pages[:y]
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 road parameter to {{[[Template:WikiProject Australia|WikiProject Australia]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/australia_roads|disable]])")
print talk.encode('ascii', 'ignore') + " saved."
except AttributeError:
print "Page save error; retrying."
try:
page.save(y, summary = "Adding road parameter to {{[[Template:WikiProject Australia|WikiProject Australia]]}} ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/australia_roads|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()