User:Habst/getWaTour.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.
// <nowiki>
tourType = 'continental-tour';
year = '2024';
ids = {
  'cross-country-tour': {
    '2023/24': {id: 3664, Gold: 3983, Silver: 3984, Bronze: 3985},
  },
  'label-road-races': {
    2024: {id:3775,Platinum:3722,Gold:3659,Elite:3944,Label:3947},
  },
  'combined-events-tour': {
    '2023/24': {id:3767,Gold:3987,Silver:3988,Bronze:3989},
  },
  'race-walking-tour': {
    '2023/24': {id:3692,Gold:3991,Silver:3992,Bronze:3993},
  },
  'continental-tour': {
    2024: {id:3773,Gold:3781,Silver:3782,Bronze:3783,Challenger:4049},
  },
}
meets = await (await fetch("https://tppcjbjqljd4tj7my77t466hn4.appsync-api.eu-west-1.amazonaws.com/graphql", {
    "credentials": "omit",
    "headers": {
        "x-api-key": "da2-fulcgci74bbslidixnmkcr6sxu",
    },
    "body": JSON.stringify({
	"operationName": "getMinisiteCalendarEvents",
	"query": `query getMinisiteCalendarEvents($competitionGroupId: Int, $competitionSubgroupId: Int, $season: String) {
  getMinisiteCalendarEvents(competitionGroupId: $competitionGroupId, competitionSubgroupId: $competitionSubgroupId, season: $season) {
    options {
      seasons {
        id
        name
        __typename
      }
      competitionSubgroups {
        id
        name
        count
        __typename
      }
      __typename
    }
    parameters {
      season
      competitionGroupId
      competitionSubgroupId
      __typename
    }
    results {
      id
      iaafId
      hasResults
      hasStartlist
      hasApiResults
      disciplines
      rankingCategory
      competitionSubgroup
      undeterminedCompetitionPeriod {
        status
        label
        remark
        __typename
      }
      name
      venue
      country
      startDate
      endDate
      dateRange
      __typename
    }
    __typename
  }
}`,
	"variables": {
		"competitionGroupId": ids[tourType][year].id,
		"competitionSubgroupId": 0,
		"season": year,
	}
    }),
    "method": "POST",
})).json();
console.log(Object.keys(ids[tourType][year]).filter(k => k !== 'id').map(tier => {
  const mtgs = meets.data.getMinisiteCalendarEvents.results.filter(m => m.competitionSubgroup === tier);
  return `| colspan="4" align="center" bgcolor="${{
  Challenger:'white',
  Label:'white',
  Elite:'silver',
  Platinum:'blue',
  }[tier] ?? tier.toLowerCase()}" | '''${tier}''' (${mtgs.length})<ref>{{cite web |title=World Athletics ${tourType} ${tier} {{!}} World Athletics |url=https://worldathletics.org/competitions/world-athletics-${tourType}/calendar-results?season=${year}&competitionSubgroupId=${ids[tourType][year][tier]} |website=worldathletics.org |access-date=${new Date().toLocaleDateString('sv')}}}</ref>\n|-\n` + mtgs.map(m => {
  const words = m.name.split(' ').filter((w, i) => {
    if (+w === new Date(m.startDate).getFullYear()) return false;
    if (w.match(/^[0-9]+(st|nd|rd|th)$/)) return false;
    if (w.match(/^[0-9]+(\.|°|º|\.º)$/)) return false;
    if (w.match(/^[0-9]+ème$/)) return false;
    if (i === 0 && w.match(/^[IVXL]+$/)) return false;
    return true;
  });
  // if (words.join(' ') === 'Meeting de Paris') words.push('Indoor');
  let name = words.join(' ');
  name = {
    Tallinn: 'Combined Events Tour Tallinn',
    ISTAF: 'ISTAF Berlin',
    'The Hero': 'The Hero!',
    'Capital Classic': 'Team Ledger Harcourts Capital Classic',
    "Sky's the Limit": "Sky's the Limit Meeting",
    'Gouden Spike': 'Gouden Spike Meeting',
    'Cork City Sports': 'Cork City Sports International Athletics',
  }[name] ?? name;
  let city = m.venue.split(', ').slice(1).join(', ').split(' (')[0];
  if (!city || (m.country === 'USA' && city.split(' ').length <= 1)) city = m.venue.split(' (')[0];
  city = {
    Montreuil: 'Montreuil, Seine-Saint-Denis',
    Halle: 'Halle (Saale)',
    Duszniki: 'Duszniki, Greater Poland Voivodeship',
    Cork: 'Cork (city)',
    Windsor: 'Windsor, Ontario',
  }[city] ?? city;
  return `| {{dts|abbr=on|${m.startDate}|format=dm}} || ${['Gold', 'Silver', 'Bronze', 'Challenger', 'Platinum', 'Elite', 'Label'].includes(tier) ? `[[${name}]]` : name} || [[${city}, {{subst:#invoke:Country alias|main|alias=${m.country}}}|${city}]] || {{flagg|cncie|${m.country}}}`
}).join('\n|-\n')
}).join('\n|-\n'));
// </nowiki>