Module:VideoMetaData
From Angelina Jordan Wiki
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:
| URL | Title | Description |
|---|---|---|
| https://www.youtube.com/watch?v=hyyOyEoO06w | No title | No description |
| https://www.youtube.com/watch?v=9GxgnNdxz9w | No title | No description |
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.
| URL | Title | Description |
|---|
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 key, value in pairs(data) do
-- Check if the entry contains metadata
if type(value) == 'table' and value.Metadata then
table.insert(videosTable, {
url = value.Metadata["url"] or "No URL",
title = value.Metadata["title"] or "No title",
description = value.Metadata["description"] or "No description"
})
end
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
-- new, faster version for specific URLs below
-- Function to load JSON content from a page
function p.loadJsonContent(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
return data, nil
end
-- Function to extract metadata from JSON data for specified URLs
function p.extractMetadata(data, urls)
-- Initialize a Lua table to store the processed data
local videosTable = {}
-- Loop over the specified URLs and copy the entries to the Lua table
for _, url in ipairs(urls) do
local video = data[url]
-- Check if the entry contains metadata
if type(video) == 'table' and video.Metadata then
video.Metadata.url = url -- Include URL in the metadata
videosTable[url] = video.Metadata
else
-- If the URL does not have metadata, add a placeholder entry
videosTable[url] = {
url = url,
title = "No title",
description = "No description"
}
end
end
return videosTable
end
-- Function to generate a wikitable from the filtered content
function p.generateWikitableByURLs(frame)
local jsonPage = frame.args[1]
if not jsonPage then
return "Error: No JSON page specified."
end
-- Extract the URLs provided as additional arguments
local urls = {}
local urlCount = 0
for i, url in ipairs(frame.args) do
if i > 1 and type(url) == 'string' then -- Skip the first argument (jsonPage)
table.insert(urls, url)
urlCount = urlCount + 1
end
end
if urlCount == 0 then
return "Error: No URLs specified."
end
local data, err = p.loadJsonContent(jsonPage)
if not data then
return "Wikitable from JSON data error: " .. err
end
local videosTable = p.extractMetadata(data, urls)
local result = {"{| class=\"wikitable\"\n! URL\n! Title\n! Description\n"}
for _, url in ipairs(urls) do
local video = videosTable[url]
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