Jump to content

User:Habst/getPetscan.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.
// https://petscan.wmflabs.org/?wikidata_label_language=&source_combination=&output_compatability=catscan&edits%5Bbots%5D=both&format=html&common_wiki_other=&show_disambiguation_pages=both&links_to_no=&search_max_results=500&max_sitelink_count=&cb_labels_no_l=1&before=&links_to_all=&sitelinks_no=&templates_no=&labels_no=&templates_any=&negcats=Terry+Fox&show_redirects=both&depth=6&subpage_filter=either&links_to_any=&page_image=any&templates_yes=&show_soft_redirects=both&outlinks_any=&labels_any=&interface_language=en&project=wikipedia&langs_labels_yes=&sparql=&ns%5B0%5D=1&cb_labels_any_l=1&larger=&search_wiki=&edits%5Bflagged%5D=both&output_limit=&cb_labels_yes_l=1&categories=2023+deaths%0D%0ASport+of+athletics+people&outlinks_no=&language=en&smaller=&langs_labels_any=
// todo: handle death precision to month, Javelin Throw (old), Decathlon (52-61) ?
getWd = async q => {
  return (await (await fetch(`https://www.wikidata.org/w/api.php?`+new URLSearchParams({action:'wbgetentities',format:'json',ids:q}))).json()).entities[q];
}
getWa = async id => {
  return (await (await fetch("https://tppcjbjqljd4tj7my77t466hn4.appsync-api.eu-west-1.amazonaws.com/graphql", {
    headers: { "x-api-key": "da2-fulcgci74bbslidixnmkcr6sxu" },
    body: JSON.stringify({
      operationName: "GetSingleCompetitorAllTimePersonalTop10",
      variables: { allTimePersonalTop10Discipline: null, id },
      query: `
query GetSingleCompetitorAllTimePersonalTop10($id: Int, $urlSlug: String, $allTimePersonalTop10Discipline: Int) {
  getSingleCompetitorAllTimePersonalTop10(id: $id, urlSlug: $urlSlug, allTimePersonalTop10Discipline: $allTimePersonalTop10Discipline) {
    results { discipline date result score }
  }
}`
    }),
    method: "POST",
  })).json()).data.getSingleCompetitorAllTimePersonalTop10.results;
}
window.qs ??= {};
window.was ??= {};
(async () => {
window.out = '';
for (const { q, title } of pet['*'][0].a['*']) {
  qs[q] ??= await getWd(q);
  const nats = ['P1532', 'P27'];
  const P_OCCUPATION = 'P106';
  const P_BIRTH = 'P569';
  const P_DEATH = 'P570';
  const P_IOC = 'P984';
  const P_WAID = 'P1146';
  const Q_ATHCOMP = 'Q11513337';
  let nat;
  console.log(title, q);
  for (const natP of nats) {
    for (const natQ of qs[q].claims[natP]?.map(c => c.mainsnak.datavalue.value.id) ?? []) {
      const natQ = qs[q].claims[natP]?.[0].mainsnak.datavalue.value.id;
      if (!natQ) continue;
      qs[natQ] ??= await getWd(natQ);
      if (qs[natQ].claims[P_IOC]) {
        nat = qs[natQ].claims[P_IOC][0].mainsnak.datavalue.value;
        break;
      }
    }
    if (nat) break;
  }
  let occ;
  const waid = qs[q].claims[P_WAID]?.find(c => c.rank !== 'deprecated')?.mainsnak.datavalue.value;
  if (waid) {
    was[waid] ??= await getWa(waid);
    occ = was[waid][0]?.discipline.replace('Mile', 'Mile run');
  }
  if (!occ) {
    const occQ = qs[q].claims[P_OCCUPATION].map(oc => oc.mainsnak.datavalue.value.id).find(id => id !== Q_ATHCOMP) ?? Q_ATHCOMP;
    qs[occQ] ??= await getWd(occQ);
    const occLabel = qs[occQ].labels.en.value.replace('sprinter', 'sprint (running)|Sprinter');
    occ = occLabel[0].toUpperCase() + occLabel.slice(1);
  }
  for (bd of [P_BIRTH, P_DEATH]) {
    qs[q].claims[bd][0].value = qs[q].claims[bd][0].mainsnak.datavalue.value;
    qs[q].claims[bd][0].value.time = qs[q].claims[bd][0].value.time.replace('-00-00T', '-01-01T').replace('-00T', '-01T');
  }
  const birth = new Date(qs[q].claims[P_BIRTH][0].value.time.slice(1));
  birth.setTime(birth.getTime() + (5*60*60*1000));
  const isLowPrecision = qs[q].claims[P_BIRTH][0].value.precision <= 10 || qs[q].claims[P_DEATH][0].value.precision <= 10;
  const death = new Date(qs[q].claims[P_DEATH][0].value.time.slice(1));
  death.setTime(death.getTime() + (5*60*60*1000));
  const age = new Date(death - birth).getFullYear() - 1970;
  const row = `| [[${title.replaceAll('_', ' ')}${title.includes('(')?'|':''}]] || {{flagg|cncie|${nat}}} || [[${occ}]] || ${isLowPrecision ? `${age}-${age+1}` : age} || {{dts|${death.getDate()} ${death.toLocaleString('default', { month: 'long' })}}}`;
  out += row + '\n|-\n';
  console.log(row);
}
return out;
})()