Widget: DCTList: Difference between revisions

From LINKS Community Center
Jump to: navigation, search
Eschmidt (talk | contribs)
No edit summary
Eschmidt (talk | contribs)
No edit summary
Line 14: Line 14:
     function getUrl(title) {
     function getUrl(title) {
         return title ? '/index.php/Special:FilePath/' + title : title;
         return title ? '/index.php/Special:FilePath/' + title : title;
    }
    /**
    * @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
        }));
     }
     }


Line 34: Line 20:
         const sourceResponse = await fetch(sourceQuery).then(response => response.json());
         const sourceResponse = await fetch(sourceQuery).then(response => response.json());


         const sources = Object.getOwnPropertyNames(sourceResponse.query.results).map(platformName => ({
        const results = sourceResponse.query.results;
         const sources = Object.getOwnPropertyNames(results).map(platformName => ({
             name: platformName,
             name: platformName,
            /** @type {string} */
             image: getUrl(results[platformName].printouts.IMAGE[0].fulltext.replace('PAGENAME:', ''))
             image: getUrl(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;
         return sources;
     }
     }
Line 53: Line 35:
         const dctQueryUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(dctQuery);
         const dctQueryUrl = '/api.php?action=ask&format=json&query=' + encodeURIComponent(dctQuery);
         const dctResponse = await fetch(dctQueryUrl).then(response => response.json());
         const dctResponse = await fetch(dctQueryUrl).then(response => response.json());
         console.log('DCT Response:', dctResponse);
         // console.log('DCT Response:', dctResponse);


         const dctList = Object.getOwnPropertyNames(dctResponse.query.results).map(dctKey => {
        const results = dctResponse.query.results;
         const dctList = Object.getOwnPropertyNames(results).map(dctKey => {
             const dct = {};
             const dct = {};
             dct.name = dctKey;
             dct.name = dctKey;
            /** @type {string} */
             dct.url = results[dctKey].fullurl;
             dct.url = dctResponse.query.results[dctKey].fullurl;
             dct.dataSources = results[dctKey].printouts['Data Sources'].map(src => src.fulltext);
            /** @type {string[]} */
             dct.logo = results[dctKey].printouts['Image'][0] ? getUrl(results[dctKey].printouts['Image'][0].fulltext) : void 0;
             dct.dataSources = dctResponse.query.results[dctKey].printouts['Data Sources'].map(src => src.fulltext);
            /** @type {string} */
             dct.logo = dctResponse.query.results[dctKey].printouts['Image'][0] ? dctResponse.query.results[dctKey].printouts['Image'][0].fulltext : void 0;
             return dct;
             return dct;
         })
         })


        const logoData = await getImages(dctList.map(dct => dct.logo).filter(logo => !!logo));
         return dctList;
         return dctList;
     }
     }

Revision as of 14:36, 7 June 2022

Development verstion of the DCT List.
Not ready for production!