-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciteavnotes.py
More file actions
101 lines (87 loc) · 3.43 KB
/
Copy pathciteavnotes.py
File metadata and controls
101 lines (87 loc) · 3.43 KB
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
from __future__ import annotations
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
AutomaticTWSummaryBot,
ConfigParserBot,
ExistingPageBot,
SingleSiteBot,
)
import wikitextparser as wtp
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
class BasicBot(
SingleSiteBot,
ConfigParserBot,
ExistingPageBot,
AutomaticTWSummaryBot,
):
use_redirects = False
summary_key = 'basic-changing'
update_options = {
'replace': False,
'summary': "[[ВП:ЗДБ#Заміна параметрів в Cite AV media notes|Заміна параметрів у шаблоні Cite AV media notes]]",
'text': 'Test',
'top': False,
}
def treat_page(self) -> None:
text = self.current_page.text
parsed = wtp.parse(text)
changed = False
for tpl in parsed.templates:
tpl_name = tpl.name.strip().replace('_', ' ').lower()
if tpl_name in ['cite av media notes', 'cite album-notes']:
# Collect old values
albumlink = tpl.get_arg('albumlink')
artist = tpl.get_arg('artist')
bandname = tpl.get_arg('bandname')
notestitle = tpl.get_arg('notestitle')
publisherid = tpl.get_arg('publisherid')
old_tpl = tpl.string
# Merge artist and bandname for 'others'
others_vals = []
if artist:
others_vals.append(artist.value)
tpl.del_arg('artist')
if bandname:
others_vals.append(bandname.value)
tpl.del_arg('bandname')
if others_vals:
tpl.set_arg('others', '; '.join(others_vals))
if albumlink:
tpl.set_arg('title-link', albumlink.value)
tpl.del_arg('albumlink')
if notestitle:
tpl.set_arg('chapter', notestitle.value)
tpl.del_arg('notestitle')
if publisherid:
tpl.set_arg('id', publisherid.value)
tpl.del_arg('publisherid')
changed = True
#print(old_tpl)
#print(tpl.string)
#input()
text.replace(old_tpl, tpl.string)
if changed:
self.put_current(str(parsed), summary=self.opt.summary)
else:
pywikibot.output(f"No changes needed on page: {self.current_page.title()}")
def main(*args: str) -> None:
options = {}
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
local_args = gen_factory.handle_args(local_args)
for arg in local_args:
arg, _, value = arg.partition(':')
opt = arg[1:]
if opt in ('summary', 'text'):
if not value:
options[opt] = pywikibot.input('Please enter a value for ' + arg)
else:
options[opt] = value
else:
options[opt] = True
gen = gen_factory.getCombinedGenerator(preload=True)
if not pywikibot.bot.suggest_help(missing_generator=not gen):
BasicBot(generator=gen, **options).run()
if __name__ == '__main__':
main()