Module:RecentMusicVideos

local p = {}

videos = {}

OUTPUT_PATTERN = [=[ mobilevideo1 description1 mobilevideo2 description2 mobilevideo3 description3 mobilevideo4 description4 mobilevideo5 description5 mobilevideo6 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 DESKTOP_WIDTH = "220" local DESKTOP_HEIGHT = "150" local MOBILE_WIDTH = "300" local MOBILE_HEIGHT = "180" 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 desktop_video = frame:preprocess("") local mobile_video = frame:preprocess("") local description = "" .. artist .. " - [" .. link .. " " .. title .. "]"		output = output:gsub("desktopvideo" .. (i + 1), desktop_video) output = output:gsub("mobilevideo" .. (i + 1), mobile_video) output = output:gsub("description" .. (i + 1), description) end return output end

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

return p