Module:VideoMetaData

From Angelina Jordan Wiki
Revision as of 11:03, 7 October 2025 by Most2dot0 (talk | contribs) (ChatGPT rewrite for current JSON data format)
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:

Error: No URLs specified.


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.

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

Code

[Edit module code]


local p = {}

-- Load JSON content from a page
function p.loadJsonContent(jsonPage)
	if type(jsonPage) ~= 'string' then
		return nil, "Invalid argument: JSON page name must be a string."
	end

	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

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

	return data, nil
end

-- Extract metadata for a list of URLs from an array-based JSON
function p.extractMetadata(data, urls)
	local videosTable = {}

	for _, url in ipairs(urls) do
		local found = false

		for _, entry in ipairs(data) do
			if entry.url == url then
				videosTable[url] = {
					url = entry.url,
					title = entry.title or "No title",
					description = entry.description or "No description",
					channelName = entry.channelName or "Unknown"
				}
				found = true
				break
			end
		end

		if not found then
			videosTable[url] = {
				url = url,
				title = "No title",
				description = "No description",
				channelName = "Unknown"
			}
		end
	end

	return videosTable
end

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

	local urls = {}
	for i = 2, #frame.args do
		local u = frame.args[i]
		if type(u) == 'string' and u ~= '' then
			table.insert(urls, u)
		end
	end
	if #urls == 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! Channel\n"}
	for _, url in ipairs(urls) do
		local v = videosTable[url]
		table.insert(result, string.format("|-\n| %s\n| %s\n| %s\n| %s\n", v.url, v.title, v.description, v.channelName))
	end
	table.insert(result, "|}")

	return table.concat(result, "\n")
end

-- Simple test function
function p.test(frame)
	local jsonPage = frame.args[1]
	local data, err = p.loadJsonContent(jsonPage)
	if not data then
		return err
	end
	local first = data[1]
	if not first then
		return "No entries found."
	end
	return string.format("First entry: %s — %s", first.title or "No title", first.channelName or "Unknown")
end

return p