-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtow.py
107 lines (92 loc) · 3.25 KB
/
tow.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
"""
Script updates sakha {{Нэдиэлэ тылбааһа}} template according to Translate of
the Week project.
Usage:
python tow.py
"""
import re
import pywikibot
META_TEMPLATE = "Template:TOWThisweek"
LOCAL_TEMPLATE = "Халыып:Нэдиэлэ тылбааһа"
ORIGINAL_ID = "original"
LOCAL_ID = "sakha"
ARCHIVE_PAGE = "Бикипиэдьийэ:Бырайыак:Тылбаас/Архыып"
ARCHIVE_ALL = True
ARCHIVE_LABEL = "<!-- NapalmBot: insert here -->"
ARCHIVE_DEFAULT = "???"
ARCHIVE_FORMAT = "|-\n| {local} || {original}\n"
DEFAULT_TEXT = "'''[[Халыып:Нэдиэлэ тылбааһа|Ыстатыйа сахалыы аатын суруй]]'''"
UPDATE_COMMENT = "Нэдиэлэ тылбааһын саҥардыы."
ARCHIVE_COMMENT = "Нэдиэлэ тылбааһын архыыптааһын."
def parse_meta_template():
"""Return (link, langcode, pagename) tuple."""
site = pywikibot.Site("meta", "meta")
template = pywikibot.Page(site, META_TEMPLATE)
match = re.search(r"\[\[:([A-Za-z\-]+):(.*?)\]\]", template.text)
return (match.group(0), match.group(1), match.group(2))
def get_sitelink(site, lang, name):
"""Return interwiki of [[:lang:name]] in current site."""
try:
page = pywikibot.Page(pywikibot.Site(lang), name)
result = pywikibot.ItemPage.fromPage(page).getSitelink(site)
except:
result = None
return result
def get_regexps():
"""
Return (original, local) re object tuple for matching links:
$1 — prefix,
$2 — link,
$3 — postfix.
"""
regexp = r"(<span id\s*=\s*\"{}\">)(.*?)(</span>)"
wrap = lambda x: re.compile(regexp.format(x))
return (wrap(ORIGINAL_ID), wrap(LOCAL_ID))
def archive(site, local, original):
"""Archive link if neccessary."""
if ARCHIVE_PAGE == "":
return
if local != DEFAULT_TEXT:
if not ARCHIVE_ALL:
match = re.match(r"\[\[(.*?)[\]|]", local)
if match is None:
return
try:
if pywikibot.Page(site, match.group(1)).exists():
return
except:
return
else:
local = ARCHIVE_DEFAULT
page = pywikibot.Page(site, ARCHIVE_PAGE)
text = page.text
pos = text.find(ARCHIVE_LABEL)
if pos == -1:
return
text = text[:pos] + ARCHIVE_FORMAT.format(local=local, original=original) + text[pos:]
page.text = text
page.save(ARCHIVE_COMMENT, minor=False)
def main():
"""Main script function."""
site = pywikibot.Site()
(interwiki, lang, name) = parse_meta_template()
local = get_sitelink(site, lang, name)
if local:
local = "[[{}]]".format(local)
else:
local = DEFAULT_TEXT
(interwiki_re, local_re) = get_regexps()
template = pywikibot.Page(site, LOCAL_TEMPLATE)
result = template.text
old_interwiki = interwiki_re.search(result).group(2)
old_local = local_re.search(result).group(2)
if interwiki == old_interwiki:
return
else:
archive(site, old_local, old_interwiki)
result = local_re.sub("\\1" + local + "\\3", result)
result = interwiki_re.sub("\\1" + interwiki + "\\3", result)
template.text = result
template.save(UPDATE_COMMENT, minor=False)
if __name__ == "__main__":
main()