-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedals.py
129 lines (94 loc) · 3.57 KB
/
medals.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import requests, json, pywikibot, re, os, datetime, mwparserfromhell
from bs4 import BeautifulSoup
#os.chdir(r'projects/sportsTables')
templateCheck = "medals table"
enwp = pywikibot.Site("en", "wikipedia")
lvwp = pywikibot.Site("lv", "wikipedia")
configuration = {
"Fake komentārs": "STOP un botflag der 'true' vai 'false' (bez pēdiņām)",
"STOP": False,
"botflag": True,
"remove": [
"event",
"team",
"show_limit"
],
"articles": {
"2020 Summer Olympics medal table": "2020. gada vasaras olimpisko spēļu medaļu tabula"
},
"prop_values": {
"remaining_link": "Pārējās valstis"
},
"repls": {
"2020 Summer Olympics medal table": "2020. gada olimpisko spēļu medaļu kopvērtējums",
"Host nation (Japan)": "Mājinieki (Japāna)",
"[[2020 Summer Olympics medal table#Medal table|Remaining NOCs]]": "Pārējās valstis",
"[[2020. gada olimpisko spēļu medaļu kopvērtējums#Medal table|Remaining NOCs]]": "Pārējās valstis",
"[[2020. gada olimpisko spēļu medaļu kopvērtējums#Medal table|Remaining NOCs]]": "Pārējās valstis",
"[[2020. gada olimpisko spēļu medaļu kopvērtējums#Medal table|Remaining Teams]]": "Pārējās valstis",
"[[2020 Summer Olympics medal table#Medal table|Remaining Teams]]": "Pārējās valstis",
"flagIOCteam": ""
}
}
mapper = configuration['articles']
botflag = configuration['botflag'] if 'botflag' in configuration else True
removeparams = configuration['remove']#note_res_KE varbūt arī nevajag ņemt ārā
paramsToReplace = configuration['prop_values']#note_res_KE varbūt arī nevajag ņemt ārā
site = pywikibot.Site('lv', 'wikipedia')
dict = {}
reslist = []
today = datetime.date.today()
def groupchange(enarticle):
#enarticle = "Template:2016–17 Premier League table"
enpage = pywikibot.Page(enwp,enarticle)
entext = enpage.get()
enwikicode = mwparserfromhell.parse(entext)
templates = enwikicode.filter_templates()
for tpl in templates:
if tpl.name.lower().strip() in templateCheck:
#pywikibot.output(tpl)
for param in removeparams:
if tpl.has(param):
tpl.remove(param)
for param in paramsToReplace:
if tpl.has(param):
val = paramsToReplace.get(param)
tpl.remove(param)
tpl.add(param, val)
newtext = str(tpl)
for replace in configuration["repls"]:
thisrepl = configuration["repls"][replace]
newtext = newtext.replace(replace,thisrepl)
return newtext
def getCurrent(lvtext):
currText = re.search('<!-- TABLE START -->\n(.*)\n<!-- TABLE END -->',lvtext, flags=re.DOTALL)
if currText:
return currText.group(1)
return False
def doreplace(text,pretext,header,footer):
newtext = re.sub(header + '.*' + footer, header + '\n' + pretext + '\n' + footer, text, flags=re.DOTALL)
#pywikibot.output(newtext)
return newtext
#
def main():
for article in mapper:
pywikibot.output("Working on %s" % article)
lvtitle = mapper[article]
page = pywikibot.Page(site,lvtitle)
text = page.get()
if 'nobots' in text: return
lvwikitable = getCurrent(text)
enwikitable = article
#if not lvwikitable: return 0#paziņot man
returnedtext = groupchange(enwikitable)
change = len(returnedtext) - len(lvwikitable)
print(change)
useBotFlag = False if change > 500 else True
if returnedtext!=lvwikitable:
tableInserted = doreplace(text,returnedtext,'<!-- TABLE START -->','<!-- TABLE END -->')
page.text = tableInserted
page.save(summary='Bots: atjaunināta tabula', botflag=useBotFlag)
else:
print('no changes')
#
main()