Jump to content

User:Habst/getProgression.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// double check for HJ e.h. men/148793 ?
window.authorization??=new DOMParser().parseFromString(await (await fetch('/beta')).text(), 'text/html').querySelector('script:not([src])').innerText.split("'")[1]
pret=window.location.href.split('/').slice(0, 3).join('/');
id = window.location.href.split('/').slice(-2).join('/');
evt = 'Heptathlon';
out = `{| class="wikitable sortable"
|+ ${evt} progression
|-
! # !! Mark !!class=unsortable| {{Abbr|Pl.|Place}} !! Competition !! Venue !! Date !!class=unsortable| {{Abbr|Ref.|References}}
`;
num=1;
function nth(n){return n ? ["st","nd","rd"][((n+90)%100-10)%10-1]||"th" : ''}
window.seasons ??= (await (await fetch(pret+`/api/athletes/${id}/seasons`, {
  headers: { authorization }
})).json()).data.map(s => +s.value).sort((a, b) => a - b);
window.results ??= {};
for (const season of seasons) {
  const data = window.results[season] ??= await (await fetch(pret+`/api/athletes/${id}/season/${season}`, {
    headers: { authorization }
  })).json();
  const results = data.templates?.[0].divs[0].tables.flatMap(table => table.body.map(res => ({...res, title: table.title}))).sort((a, b) => new Date(a.date) - new Date(b.date)) ?? [];
  for (const res of results) {
    if (res.personalBest !== 'PB') continue;
    if (res.title !== evt) continue;
    const dt = new Date(res.date);
    const mon = dt.toLocaleString('en-US', { month: 'short' });
    let dts = `{{dts|${dt.getDate()} ${mon} ${dt.getFullYear()}}}`;
    const roundChar = ['h', 's', 'r', 'q'].find(c => res.pos?.includes(c));
    const roundNum = res.pos?.split(roundChar).at(-1);
    if (res.competitionLong?.includes(', '))
      res.competitionLong = res.competitionLong.split(', ')[0];
    if (res.competition === 'NC' && !res.pos.includes('-')) {
      res.competitionLong = `[[${res.venueCountry} Athletics Championships]]`;
    }
    if (/1.th|(^|[^1])(1st|2nd|3rd|[^1-3]th)/.test(res.competitionLong?.split(' ')[0])) {
      res.competitionLong = res.competitionLong.split(' ').slice(1).join(' ');
      dts = `[[${dt.getFullYear()} ${res.competitionLong}|${dts}]]`;
      res.competitionLong = `[[${res.competitionLong}]]`;
    }
    if (res.venueCountry === 'USA') {
      res.venueCountryFull = res.venue?.split(' ').at(-1);
      res.venue = res.venue?.split(' ').slice(0, -1).join(' ');
    } else {
      res.venueCountryFull ??= data.countries[res.venueCountry].full;
    }
    res.venue = res.venue.split('/')[0];
    const isTrack = evt.split(' ')[0].endsWith('m') || evt.split(' ')[0].endsWith('mH');
    const result = res.result.endsWith('A') ? res.result.slice(0, -1) : res.result;
    const suffix = isTrack ? '' : evt.includes('athlon') ? ' pts' : ' m';
    out += `|-\n|align=center| ${num++} || ${result}${suffix}${res.wind ? ` {{wind|${res.wind}}}` : res.indoor ? ` {{AthAbbr|sh}}` : ''} || ${{1:'{{Gold1}}',2:'{{Silver2}}',3:'{{Bronze3}}'}[res.pos1] ?? (res.pos1 + nth(+res.pos1))}${roundChar ? ` {{sma|(${{h:'Heat',s:'Semifinal',r:'Round',q:'Qualification'}[roundChar]}${roundNum ? ' ' + roundNum : ''})}}` : ''} || ${res.competitionLong ?? res.competition ?? ''} || [[${res.venue}, ${res.venueCountryFull}]] ||align=right| ${dts} ||<ref>{{Tilr|${res.competitionId}}}</ref>\n`
  }
}
out += '|}';
console.log(out);