User:Habst/getProgression.js

// 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

#Markclass=unsortable| {{Abbr|Pl.|Place}}CompetitionVenueDateclass=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 = `${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}{{Tilr|${res.competitionId}}}\n`

}

}

out += '

';

console.log(out);