User:Habst/getWaMedalTable.js

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 = `` console.log(nations, athletes, out); return out; });