Module:GetData

From Buddha-Nature

Documentation for this module may be created at Module:GetData/doc

local p = {}

function p.remoteGetCategory(frame)

    local category = frame.args.category or "Terms"
    local source = frame.args.source or "commons"
    local limit = frame.args.limit or "1000"

    local nocache = frame.args.nocache

    local queryPart = "[[Category:" .. category .. "]]|limit=" .. limit
    local encodedQuery = mw.uri.encode(queryPart)
    local queryUrl = "https://" .. source .. ".tsadra.org/api.php?action=ask&api_version=3&format=json&query=" .. encodedQuery
    if nocache then
        queryUrl = queryUrl .. "&nocache=" .. os.time()
    end

    local data, errors = mw.ext.externalData.getExternalData(queryUrl)

    if errors or not data then
        return 'fetch error'
    end

    local titles = {}
    for i = 1, data.count do
        local row = data[i]
        if type(row) == 'table' and row.fulltext then
            table.insert(titles, row.fulltext)
        end
    end
    return table.concat(titles, ";")

end

return p