Script to Fetch Wikipedia Summary from Keyword

Description

I wrote this little scriptlet for personal use and thought maybe someone else might find it useful.
It fetches the summary of a Wikipedia article in one cell based on a query in another using the public MediaWiki API.

example

code

I’m just starting out programming and I don’t know JS so there’s probably alot of bad practice. I wanted to write it in a more functional way but I couldn’t figure it out how when it’s asynchronous.
Feedback welcomed with a smile :smiley:

/* CONFIG */
let queryColumn = 'Query'
let resultColumn = 'Wiki'
let language = 'en'
let table = base.getTable('table');
let view = table.getView('view');


/* SCRIPT */

/* list of all records in view */
let result = await view.selectRecordsAsync();
for (let record of result.records) {
    let query = record.getCellValueAsString(queryColumn)
    let seachQuery = encodeURIComponent(query)
    /* skips wiki request if airtable record has no query */
    if (query == null) {continue}
    /* skips request if airtable record already has */
    if (record.getCellValue(resultColumn) == null) {
        let wikiURL = 
            'https://' + language +
            '.wikipedia.org/w/api.php?origin=*' + 
            /* specify content of wiki request (https://en.wikipedia.org/w/api.php?) */
            '&action=query&prop=extracts&titles=' + seachQuery + '&exintro' +
            '&format=json';
        let wikiResponse = await fetch(wikiURL, {mode:"cors", headers:{origin:"*"}});
        let wikiJSON = await wikiResponse.json();
        let wikiIntroHTML = wikiJSON.query.pages[Object.keys(wikiJSON.query.pages)].extract;
        try {
            /* removes everything but raw text */
            var wikiIntroText = wikiIntroHTML.replace(/\n|<.*?>/gm,''); 
        }
        catch(e) {
            console.error(
            `Wiki search for "${query}" returned an undefined object.
            This is likely because no such Wikipedia article exists,
            or because your query is not formatted correctly. (it is case sensitive)`
            )
        }
        await table.updateRecordAsync(record, {[resultColumn]: wikiIntroText,});
    }
}
output.markdown('# done')
7 Likes

Very cool, thanks for sharing!