Module:RecentMusicVideos

local p = {}

videos = {}

OUTPUT_PATTERN = [=[ video1 description1 video2 description2 video3 description3 video4 description4 video5 description5 video6 description6 ]=]

function GetVideos(content) for artist, id, title in content:gmatch "%[%[(.-)%]%]%s-|%s-%[https?://m?w?w?w?%.?youtube%.com/watch%?v=(.-)%s(.-)%]" do		table.insert(videos, {id=id, artist=artist, title=title}) end end

function GetPage(frame, delay) local month_delay = delay .. " month" local curr_year = frame:callParserFunction{name="#time", args={"Y", month_delay}} local curr_month = frame:callParserFunction{name="#time", args={"F", month_delay}} local page_link = "Template:" .. curr_year .. " Music Videos/" .. curr_month return mw.title.new(page_link):getContent end

function BuildOutput(frame) local content = nil local 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 = GetPage(frame, delay) if(content ~= nil) then GetVideos(content) --if number of entries of current month is less than 6, --also load previous month before the current month if(#videos <= 6) then videos = {} content = GetPage(frame, delay - 1) GetVideos(content) content = GetPage(frame, delay) GetVideos(content) end else delay = delay - 1 end end

local output = OUTPUT_PATTERN local WIDTH = "220" local HEIGHT = "150" for i = 0,6,1 do local id = videos[#videos - i].id		local artist = videos[#videos - i].artist local title = videos[#videos - i].title local link = "https://www.youtube.com/watch?v=" .. id		local video = frame:preprocess("") local description = "" .. artist .. " - [" .. link .. " " .. title .. "]"		output = output:gsub("video" .. (i + 1), video) output = output:gsub("description" .. (i + 1), description) end return output end

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

return p