Module:VideoMetaData

From Angelina Jordan Wiki
Revision as of 11:54, 27 July 2024 by Most2dot0 (talk | contribs)
The code for this module can be found below its documentation. You can edit the code, edit the documentation, or purge the cache.

Work in Progress!!

This module is supposed to provide access to video meta data, that is provided with and inherent to the video. I.e., this should all be automatically collectable, and should contain no opinion of the wiki contributors.

The idea is, that when an URL is given, this meta data can be provided in addition. For this, and because it can grow quite large, it should give efficient access to the data.

Examples

Associative Access

Based on Open Graph meta data contained in URL pages, stored in a JSON file:

Script error: The function "generateWikitableByURLs" does not exist.


Link with title form meta data

Script error: The function "link" does not exist. Script error: The function "link" does not exist.

Complete listing

This is based on a 2nd set of functions, that loops over the JSON data.

Lua error at line 34: attempt to index field 'Metadata' (a nil value).

Code

[Edit module code]


local p = {}

-- Function to convert JSON data to a Lua table
function p.jsonToTable(jsonPage)
    -- Check if jsonPage is a valid string
    if type(jsonPage) ~= 'string' then
        return nil, "Invalid argument: JSON page name must be a string."
    end
    
    -- Try to load the page content
    local title = mw.title.new(jsonPage)
    if not title then
        return nil, "Failed to create title object for page: " .. jsonPage
    end

    local content = title:getContent()
    if not content then
        return nil, "Failed to load content for page: " .. jsonPage
    end

    -- Try to decode the JSON content
    local data = mw.text.jsonDecode(content)
    if type(data) ~= 'table' then
        return nil, "Invalid JSON data in page: " .. jsonPage
    end

    -- Initialize a Lua table to store the processed data
    local videosTable = {}

    -- Loop over the JSON data and copy the entries to the Lua table
    for url, video in pairs(data) do
        table.insert(videosTable, {
            url = url,
            title = video.Metadata["title"],
            description = video.Metadata["description"]
        })
    end

    return videosTable, nil
end

-- Function to generate a wikitable from the filtered content
function p.generateWikitable(frame)
    local jsonPage = frame.args[1]
    if not jsonPage then
        return "Error: No JSON page specified."
    end

    local videosTable, err = p.jsonToTable(jsonPage)

    if not videosTable then
        return "Wikitable from JSON data error: " .. err
    end

    local result = {"{| class=\"wikitable\"\n! URL\n! Title\n! Description\n"}
    
    for i, video in ipairs(videosTable) do
        table.insert(result, string.format("|-\n| %s\n| %s\n| %s\n", video.url, video.title, video.description))
    end
    
    table.insert(result, "|}")
    return table.concat(result, "\n")
end

return p