forked from ONI-Wiki-zh/BotNotIncluded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_add_footer.py
53 lines (43 loc) · 1.52 KB
/
bot_add_footer.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
from typing import Callable, List
import pywikibot
import pywikibot.textlib as textlib
def bot_add_footer(site: pywikibot.Site,
categories: List[str],
page_cond: Callable[[pywikibot.Page], bool],
footer: str,
summary: str):
for cat in categories:
print(f"Iterating category '{cat}'")
for p in site.categorymembers(pywikibot.Category(site, cat), content=True):
if not page_cond(p):
print(f"Skip page '{p}'")
continue
p.text = textlib.add_text(p.text, footer)
p.save(summary)
if __name__ == '__main__':
footer_template = "Elements"
oni_en = pywikibot.Site("en", "oni")
cats = [
"Gas",
"Liquid",
"Solid",
]
def page_filter(page: pywikibot.Page):
footer_temp = pywikibot.Page(oni_en, f"Template:{footer_template}")
ps = oni_en.categorymembers(pywikibot.Category(oni_en, "Solid"), content=True)
# category pag
if page.is_categorypage():
return False
# redirect page
if page.isRedirectPage():
return False
# non-english pages
if "/" in page.title():
return False
# pages already have this template
if footer_temp in page.templates():
return False
return page.exists()
bot_add_footer(oni_en, cats, page_filter,
"{{Elements}}",
"Add elements footer with pywikibot script")