Widget: DCTList: Difference between revisions
From LINKS Community Center
Eschmidt (talk | contribs) No edit summary |
Eschmidt (talk | contribs) No edit summary |
||
Line 5: | Line 5: | ||
<script> | <script> | ||
/** | |||
* @param {string[]} imageTitles - Array of image titles, e.g, ['File:Image1.png', 'File:Image2.png'] | |||
*/ | |||
async function getImages(imageTitles) { | |||
const imageUrlQuery = '/api.php?action=query&format=json&prop=imageinfo&iiprop=url&titles=' + encodeURIComponent(imageTitles.join('|')); | |||
const imageUrlResponse = await fetch(imageUrlQuery).then(response => response.json()); | |||
return Object.getOwnPropertyNames(imageUrlResponse.query.pages).map(pageKey => ({ | |||
/** @type {string} */ | |||
title: imageUrlResponse.query.pages[pageKey].title, | |||
/** @type {string} */ | |||
url: imageUrlResponse.query.pages[pageKey].imageinfo[0].url | |||
})); | |||
} | |||
async function getSources() { | |||
const sourceQuery = '/api.php?action=ask&format=json&query=' + encodeURIComponent('[[Category:Social media platform]]|?IMAGE'); | |||
const sourceResponse = await fetch(sourceQuery).then(response => response.json()); | |||
const sources = Object.getOwnPropertyNames(sourceResponse.query.results).map(platformName => ({ | |||
name: platformName, | |||
/** @type {string} */ | |||
image: sourceResponse.query.results[platformName].printouts.IMAGE[0].fulltext.replace('PAGENAME:', '') | |||
})); | |||
const imageData = await getImages(sources.map(source => source.image)); | |||
imageData.forEach(img => sources[sources.findIndex(source => source.image === img.title)].image = img.url); | |||
console.log('Sources:', sources); | |||
return sources; | |||
} | |||
async function getDcts() { | |||
const dctQuery = '[[Category:Disaster Community Technology]]' + | |||
'|?Image' + | |||
'|?Data Sources'; | |||
const dctQueryUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(dctQuery); | |||
const dctResponse = await fetch(dctQueryUrl).then(response => response.json()); | |||
console.log('Response:', dctResponse); | |||
const dctList = Object.getOwnPropertyNames(dctResponse.query.results).map(dctKey => { | |||
const dct = {}; | |||
dct.name = dctKey; | |||
dct.url = dctResponse.query.results[dctKey].fullurl; | |||
dct.dataSources = dctResponse.query.results[dctKey].printouts['Data Sources'].map(src => src.fulltext); | |||
dct.logo = dctResponse.query.results[dctKey].printouts['Image'][0] ? dctResponse.query.results[dctKey].printouts['Image'][0].fulltext : void 0; | |||
return dct; | |||
}) | |||
console.log('DCT:', dctList); | |||
const logoData = await getImages(dctList.map(dct => dct.logo).filter(logo => !!logo)); | |||
console.log('images', logoData); | |||
// table.on("tableBuilt", () => { | |||
// }); | |||
return dctList; | |||
} | |||
// const table = new Tabulator("#dct-tabulator", { | |||
} | // data: dctList, | ||
// autoColumns: true, | |||
// }); | |||
Promise.all([getSources(), getDcts()]).then(data => { | |||
console.log('OUTPUT', data); | |||
}) | |||
</script> | </script> | ||
<div id="tabulator"></div> | <div id="dct-tabulator"></div> | ||
</includeonly> | </includeonly> |
Revision as of 13:12, 7 June 2022
Development verstion of the DCT List.
Not ready for production!