Module:Media

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

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

local tables = require('Module:Tables')
local smwjson = require('Module:SMW JSON')
local p = {}

function is_participant(person, participants)
	if #participants == 0 then
		return false
	end
	for _,participant in ipairs(participants) do
		if participant ~= nil then
			participant = participant:lower()
			if participant:find(person..']]', 1, true) or participant:find(person..'|', 1, true) then
				return true
			end
		end
	end
	return false
end

function media_type(t)
	if t == nil then
		return 'Unknown'
	else
		t = t:lower()
		if t == 'stream' then
			return '[[Livestream]]'
		elseif t == 'video' then
			return '[[Video]]'
		elseif t == 'podcast' then
			return '[[Podcast]]'
		end
	end
	return 'Unknown'
end

function p.main(frame)
	local args = frame:getParent().args
	local person = (args[1] or ''):lower()
	
	local data = mw.smw.ask{
		"[[Category:Livestreams]] OR [[Category:Videos]] OR [[Category:Podcasts]]",
		"?Media JSON",
		limit = 4000
	}
	data = smwjson.parse(data, "Media JSON")
	local ret = mw.html.create('table')
		:addClass('wikitable sortable autosort=3,d')
		
	local header = {
		"Media",
		"Type",
		"Date"
	}
	tables._row(ret:tag('tr'), header, true)
	
	for _,line in ipairs(data) do
		local participants = line.participants or ''
		local participants_parts = {}
		for i in string.gmatch(participants, '([^,]+)') do
			table.insert(participants_parts, i)
		end
		if is_participant(person, participants_parts) then
			local row = {
				line._page,
				media_type(line.type),
				line.date
			}
			tables._row(ret:tag('tr'), row, false)
		end
	end
	return ret
end

return p