Module:RecentMusicVideos

local p = {}

I = {} A = {} T = {}

n_items = 0

function DataInsert(id, artist, title) table.insert(I, id) table.insert(A, artist) table.insert(T, title) n_items = n_items + 1 end

function RemoveData I = {} A = {} T = {} n_items = 0 end

function ParseContent(content) for artist, id, title in content:gmatch "%[%[(.-)%]%]%s-|%s-%" do		DataInsert(id, artist, title) end end

pattern = [[{| style="text-align:center;" align="center"
 * video1
 * video2
 * video3
 * description1
 * description2
 * description3
 * video4
 * video5
 * video6
 * description4
 * description5
 * description6
 * }]]
 * description5
 * description6
 * }]]

function GetContent(frame, delay) local monthdelay = delay .. " month" local currYear = frame:callParserFunction{name="#time", args={"Y", monthdelay}} local currMonth = frame:callParserFunction{name="#time", args={"F", monthdelay}} local pagelink = "Template:" .. currYear .. " Music Videos/" .. currMonth local page = mw.title.new(pagelink) return page:getContent end

function BuildOutput(frame) content = nil delay = 0 --making sure the page exists --if it doesn't, pick the previous month until a page is found while(content == nil) do		content = GetContent(frame, delay) if(content ~= nil) then ParseContent(content) --if number of entries of current month is less than 6, --also load previous month before the current month if(n_items < 6) then RemoveData content = GetContent(frame, delay - 1) ParseContent(content) content = GetContent(frame, delay) ParseContent(content) end else delay = delay - 1 end end

output = pattern for i = 0,6,1 do id = I[n_items - i]		width = "220" height = "150" artist = A[n_items - i]		title = T[n_items - i]		video = frame:expandTemplate{ title = "YoutubePlayer", args = {id=id, width=width, height=height} }		description = "" .. artist .. " - [https://www.youtube.com/watch?v=" .. id .. " " .. title .. "]"		videoplaceholder = "video" .. (i + 1) descriptionplaceholder = "description" .. (i + 1) output = output:gsub(videoplaceholder, video) output = output:gsub(descriptionplaceholder, description) end return output end

function p.Main(frame) return BuildOutput(frame) end

return p