-
Notifications
You must be signed in to change notification settings - Fork 8
/
protected_areas.py
executable file
·89 lines (71 loc) · 2.22 KB
/
protected_areas.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
#! /usr/bin/env python
import mwclient
import sys
from theobot import password
# CC-BY-SA Theopolisme
# Task 8 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/protected areas") == True:
return True
else:
return False
else:
return True
def pages_in_cat(cat):
cat = mwclient.listing.Category(site, cat)
x = []
for item in cat:
x.append(item.page_title)
return x
def generate_wikicode(year):
var = """{{Year nav topic3|""" + year + """|protected areas established}}
This is a list of [[protected area]]s established in """ + year + """.
{| class="wikitable sortable"
|-
! Name
! Country
! data-sort-type="numeric" |Area (ha)
|-"""
cats = pages_in_cat("Category:Protected areas established in " + year)
for cat in cats:
var = var + "\n| [[" + cat + """]] || ||\n|-"""
var = var + """\n|}
==See also==
*[[""" + year + """ in the environment]]
[[Category:Protected areas established in """ + year + """| ]]"""
return var
def main():
global site
site = mwclient.Site('en.wikipedia.org')
site.login(password.username, password.password)
years_to_do = []
for item in mwclient.listing.Category(site, 'Category:Protected areas by year of establishment'):
if "Category" in str(item):
try:
int(item.page_title.split()[-1])
year = item.page_title.split()[-1]
years_to_do.append(year)
except ValueError:
print "Item " + str(item.page_title.split()[-1]) + " skipped."
donenow = 5
for year in years_to_do:
if sokay(donenow) == True:
donenow += 1
pagename = "List of protected areas established in " + year
if site.Pages[pagename].edit() == '':
page_save_text = generate_wikicode(year)
page = site.Pages[pagename]
print "Saving page " + pagename + "."
page.save(text,summary="Creating per [[WP:CLT]] for [[:Category:Protected areas by year of establishment]] ([[WP:BOT|bot]] - [[User:Theo's Little Bot/disable/protected areas|disable]])")
else:
print "Page already created, skipping."
else:
print "Bot was disabled. Exiting."
sys.exit()
if __name__ == '__main__':
main()