User:Habst/getWaMedalTable.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Habst/getWaMedalTable. |
window.data ??= {};
window.cache ??= {};
medals = {};
nations = new Set();
athletes = new Set();
(async () => {
for (const day of [1,2,4,5])
window.data[day]??=await (await fetch("https://wpgiegzkbrhj5mlsdxnipboepm.appsync-api.eu-west-1.amazonaws.com/graphql", {
"headers": {
"x-api-key": "da2-juounigq4vhkvg5ac47mezxqge" // note API key is intentionally public
},
"body": JSON.stringify({
"operationName": "getCalendarCompetitionResults",
"variables": {
"competitionId": 7200585,
"day": day,
"eventId": null
},
"query": `query getCalendarCompetitionResults($competitionId: Int, $day: Int, $eventId: Int) {
getCalendarCompetitionResults(competitionId: $competitionId, day: $day, eventId: $eventId) {
competition {
dateRange
endDate
name
rankingCategory
startDate
venue
__typename
}
eventTitles {
rankingCategory
eventTitle
events {
event
eventId
gender
isRelay
perResultWind
withWind
summary {
competitor {
teamMembers {
id
name
iaafId
urlSlug
__typename
}
id
name
iaafId
urlSlug
birthDate
__typename
}
mark
nationality
placeInRace
placeInRound
points
raceNumber
records
wind
__typename
}
races {
date
day
race
raceId
raceNumber
results {
competitor {
teamMembers {
id
name
iaafId
urlSlug
__typename
}
id
name
iaafId
urlSlug
birthDate
hasProfile
__typename
}
mark
nationality
place
points
qualified
records
wind
remark
details {
event
eventId
raceNumber
mark
wind
placeInRound
placeInRace
points
overallPoints
placeInRoundByPoints
overallPlaceByPoints
__typename
}
__typename
}
startList {
competitor {
birthDate
country
id
name
urlSlug
__typename
}
order
pb
sb
bib
__typename
}
wind
__typename
}
__typename
}
__typename
}
options {
days {
date
day
__typename
}
events {
gender
id
name
combined
__typename
}
__typename
}
parameters {
competitionId
day
eventId
__typename
}
__typename
}
}`
}),
"method": "POST",
})).json();
mark2secs=(mark, isField = false)=>{
const parts = mark.split(':');
let ret;
if (parts.length === 1) ret = +mark;
else if (parts.length === 2) ret = +parts[0] * 60 + +parts[1];
else ret = +parts[0] * 60 * 60 + +parts[1] * 60 + +parts[2];
if (Number.isNaN(ret)) return isField ? -Infinity : Infinity;
return ret;
}
const combinedData = Object.values(data).reduce((acc, dayData) => {
for (let eventTitle of dayData.data.getCalendarCompetitionResults.eventTitles) {
eventTitle = structuredClone(eventTitle);
const foundEventTitle = acc.data.getCalendarCompetitionResults.eventTitles.find(et => et.eventTitle === eventTitle.eventTitle);
if (foundEventTitle) {
const oldEvts = [...eventTitle.events];
for (const evt of oldEvts) {
const foundEvt = foundEventTitle.events.find(e2 => e2.event === evt.event);
if (foundEvt) foundEvt.races.push(...evt.races);
else foundEventTitle.events.push(evt);
}
}
else acc.data.getCalendarCompetitionResults.eventTitles.push(eventTitle);
}
return acc;
}, { data: { getCalendarCompetitionResults: { eventTitles: [] } } });
for (const eventTitle of combinedData.data.getCalendarCompetitionResults.eventTitles) {
if (!['World Athletics Indoor Tour', 'Indoor Meeting', 'Diamond Discipline', 'Promotional Events', 'National Events', 'U18 Events', 'Regional Races', null].includes(eventTitle.eventTitle)) continue;
for (const evt of eventTitle.events) {
let etHasEvents = true;
const isField = ['jump', 'throw', 'vault', 'discus', 'put'].some(s => evt.event.toLowerCase().includes(s));
const stages = Object.values(evt.races.reduce((acc, r) => {
acc[r.race] ??= [];
acc[r.race].push(r);
return acc;
}, {}));
for (const stage of stages) {
const isLastStage = stages.indexOf(stage) === stages.length - 1;
const isFinal = stage[0].race === 'Final';
const isMulti = stage.length > 1;
const results = stage.flatMap(race => race.results.map(res => ({...res, raceNumber: race.raceNumber, wind: res.wind ?? race.wind})))//.sort((a, b) => isField ? mark2secs(b.mark, true) - mark2secs(a.mark, true) : mark2secs(a.mark) - mark2secs(b.mark));
for (const result of results) {
nations.add(result.nationality);
const id = +result.competitor.urlSlug?.split('-').at(-1).replace(/^0/, '');
if (id) athletes.add(id);
else {
for (const tm of result.competitor.teamMembers) athletes.add(+tm.id);
}
const pl = ['DNS', 'DNF', 'DQ', 'NM'].includes(result.mark) ? '' : +result.place.replace('.', '')//results.indexOf(result) + 1;
if (isFinal) {
const medal = pl === 1 ? 'gold' : pl === 2 ? 'silver' : pl === 3 ? 'bronze' : null;
if (medal) {
medals[result.nationality] ??= { gold: 0, silver: 0, bronze: 0 };
medals[result.nationality][medal]++;
}
}
}
}
}
}
const out = `{{Medals table
| host =
| flag_template =
| event =
| team =
${Object.entries(medals).map(([nat, meds]) => ` | gold_${nat} = ${meds.gold} | silver_${nat} = ${meds.silver} | bronze_${nat} = ${meds.bronze}`).join('\n')}
}}`
console.log(nations, athletes, out);
return out;
})();