User:Habst/roadTo.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/roadTo. |
let out = '{{mw-datatable}}\n==2024 Olympians==\n';
const eventId = '10229509';
const disciplineName = `Women's 100 Metres`;
window.cache ??= {};
window.q ??= {};
const getRoad = async (disciplineName, eventId) => {
return window.q[disciplineName] ??= await (await fetch("https://graphql-prod-4607.prod.aws.worldathletics.org/graphql", {
headers: { "x-api-key": "da2-xevhjbj7ybgcnjzrwiad2u6qqq" },
body: JSON.stringify({
operationName: "GetChampionshipQualifications",
variables: { competitionId: "7153115", eventId },
query: `query GetChampionshipQualifications($competitionId: Int!, $eventId: Int, $country: String, $qualificationType: String) {
getChampionshipQualifications(competitionId: $competitionId, eventId: $eventId, country: $country, qualificationType: $qualificationType) {
eventId
groupByCountry
entryNumber
entryStandard
alternativeEntryStandards {
entryStandard
event
__typename
}
disciplineName
maxCompetitorsByCoutnry
firstQualificationDay
lastQualificationDay
firstRankingDay
lastRankingDay
rankDate
numberOfCompetitorsQualifiedByEntryStandard
numberOfCompetitorsQualifiedByTopList
numberOfCompetitorsFilledUpByWorldRankings
numberOfCompetitorsQualifiedByUniversalityPlaces
numberOfCompetitorsQualifiedByDesignatedCompetition
qualifications {
qualifiedBy
qualified
qualificationPosition
countryPosition
name
urlSlug
iaafId
birthDate
competitorIaafId
result
wind
venue
date
countryCode
place
score
calculationId
label
__typename
}
events {
genderCode
eventId
disciplineName
__typename
}
countries {
shortname
name
__typename
}
qualificationTypes {
id
name
__typename
}
__typename
}
}`
}),
method: "POST",
})).json();
};
if (typeof nameFixer === 'undefined') {
const script = Object.assign(document.createElement('script'), { src: 'https://unpkg.com/name-fixer@1.0.0' });
document.body.appendChild(script);
await new Promise(res => script.addEventListener('load', res));
}
titleExists=async (name)=>{
const enLabelTitleMatch = await fetch(`https://xtools.wmcloud.org/api/page/articleinfo/en.wikipedia.org/${name.replace('|', '')}?format=json&uselang=en`);
return enLabelTitleMatch.status === 200;
}
getSuffix=async (name, evt, year) => {
const el = evt?.toLowerCase() ?? '';
const parens = evt?.includes('mH') || el.includes('metres hurdles') ? 'hurdler' : el.includes('high jump') ? 'high jumper' : el.includes('long jump') ? 'long jumper' : el.includes('pole vault') ? 'pole vaulter' : el.includes('triple jump') ? 'triple jumper' : el.includes('shot put') ? 'shot putter' : el.includes('discus') ? 'discus thrower' : el.includes('hammer') ? 'hammer thrower' : el.includes('javelin') ? 'javelin thrower' : el.includes('steeplchase') ? 'steeplechase runner' : el.includes('walk') ? 'racewalker' : el.includes('heptathlon') ? 'heptathlete' : el.includes('decathlon') ? 'decathlete' : ['60m', '60 m', '100m', '100 m', '200m', '200 m', '400m', '400 m'].some(d => el.includes(d)) ? 'sprinter' : 'runner';
name += ` (${parens})`;
if (await titleExists(name)) name = name.replace(`(${parens})`, `(${parens}, born ${year})`);
return name;
}
getTitle=async (id,name,evt,year)=>{
const words = name.split(' ');
const lnameStart = words.findIndex(w => w.toUpperCase() === w);
const fname = words.slice(0, lnameStart).join(' ');
const lname = words.slice(lnameStart).join(' ');
name = fname + ' ' + nameFixer.nameFixer(lname);
name = name.replace('LI', 'Li').replace('XI', 'Xi');
if (cache[id]) return cache[id];
const pages = await (await fetch('https://www.wikidata.org/w/api.php?' + new URLSearchParams({
action: 'query',
format: 'json',
list: 'search',
srsearch: `haswbstatement:P1146=${id}`,
}))).json();
const qid = pages.query.search[0]?.title;
if (qid) {
const entity = await (await fetch('https://www.wikidata.org/w/api.php?' + new URLSearchParams({
action: 'wbgetentities',
format: 'json',
ids: qid,
}))).json();
const sitelinks = entity.entities[qid].sitelinks;
const enTitle = sitelinks.enwiki?.title;
if (enTitle) {
cache[id] = `[[${enTitle}${enTitle.includes('(') ? '|' : ''}]]`;
return cache[id];
}
let enLabel = entity.entities[qid].labels.en?.value ?? name;
const enLabelNoParens = enLabel;
if (await titleExists(enLabel)) enLabel = await getSuffix(enLabel, evt, year);
const otherWikis = Object.keys(sitelinks).filter(key => !key.startsWith('commons') && key.endsWith('wiki'));
if (otherWikis.length) {
const positionals = otherWikis.map(ow => `|${ow.replace('wiki', '')}|${sitelinks[ow].title}`).join('');
cache[id] = `{{ill|${enLabel}${positionals}${enLabel.includes('(') ? `|lt=${enLabelNoParens}` : ''}}}`;
return cache[id];
}
cache[id] = `{{ill|${enLabel}|wd=${qid}|s=1${enLabel.includes('(') ? `|lt=${enLabelNoParens}` : ''}}}`;
return cache[id];
}
if (await titleExists(name)) name = await getSuffix(name, evt, year);
cache[id] = `[[${name}${name.includes('(') ? '|' : ''}]]`;
return cache[id];
}
await getRoad(disciplineName, eventId);
const ocq = q[disciplineName].data.getChampionshipQualifications;
for (const { disciplineName, eventId } of ocq.events) {
if (disciplineName.includes('Relay')) continue;
if (disciplineName.includes('Men')) continue;
await getRoad(disciplineName, eventId);
const cq = q[disciplineName].data.getChampionshipQualifications;
console.log(disciplineName);
const evt = disciplineName.split(' ').slice(1).join(' ');
out += `===${disciplineName}===\n{| class="wikitable sortable mw-datatable"
|+ ${disciplineName} (${cq.entryNumber} spots; standard ${cq.entryStandard})
! # !! Athlete !! Country !! WA !! Qualified By
|-\n`;
for (const aq of q[disciplineName].data.getChampionshipQualifications.qualifications) {
let id = aq.urlSlug.split('-').at(-1);
if (id.startsWith('0')) id = id.slice(1);
const yob = aq.birthDate.split(' ').at(-1);
const title = await getTitle(id, aq.name, evt, yob);
out += `| ${aq.qualificationPosition ?? 'Alt'} || ${title} || {{flagg|cncie|${aq.countryCode}}} || [https://worldathletics.org/athletes/_/${id}] || ${aq.qualifiedBy}\n|-\n`;
}
out += '|}\n\n';
}
console.log(out);