-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnew_links.py
67 lines (61 loc) · 2.03 KB
/
new_links.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
from mwrogue.esports_client import EsportsClient
from mwcleric.auth_credentials import AuthCredentials
import mwparserfromhell, re
credentials = AuthCredentials(user_file="bot")
site = EsportsClient('lol', credentials=credentials) # set wiki
summary = 'Changing links/display to be just 1 field, with link only' # Set summary
limit = -1
startat_page = None
print(startat_page)
# startat_page = 'Challengers Korea/2017 Season/Spring Season/Scoreboards/Week 3'
this_template = site.client.pages['Module:Scoreboard'] # Set template
pages = this_template.embeddedin()
#pages = [site.pages['Data:Challengers Korea/2019 Season/Spring Season']]
def links_to_display(template):
if not template.has('name'):
return
name = template.get('name').value.strip()
if '{{!}}' in name:
template.add('name', name.split('{{!}}')[0])
name = template.get('name').value.strip()
if not template.has('link'):
template.add('link', name, before='name')
template.remove('name')
return
display_str = template.get('name').value.strip()
link_str = template.get('link').value.strip()
displays = display_str.split(',')
links = link_str.split(',')
new = []
for i, v in enumerate(displays):
if i < len(links) and links[i] != '':
uc_display = v[0].upper() + v[1:]
regex = r'^' + re.escape(uc_display)
link = re.sub(regex, v, links[i])
new.append(link)
continue
new.append(v)
template.add('link', ','.join(new))
template.remove('name')
passed_startat = False if startat_page else True
lmt = 0
for page in pages:
if lmt == limit:
break
if startat_page and page.name == startat_page:
passed_startat = True
if not passed_startat:
print("Skipping page %s" % page.name)
continue
lmt += 1
text = page.text()
wikitext = mwparserfromhell.parse(text)
for template in wikitext.filter_templates():
if template.name.matches(['MatchRecapS8/Player']):
links_to_display(template)
newtext = str(wikitext)
if text != newtext:
print('Saving page %s...' % page.name)
page.save(newtext, summary=summary)
else:
print('Skipping page %s...' % page.name)