模組:沙盒/Pseudo Classes/Template:YouTube

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p = {}
local ent = mw.wikibase.getEntity()
local url = require('Module:URL')._url
local getArgs = require('Module:Arguments').getArgs

local txt = 'YouTube上的'
local uri = 'https://www.youtube.com/'

function p.channel(id, name)
	local typ = 'channel/'
	if id and ent == nil then return end
	if type(id) == 'string' then
		if id.match('^UC[-_0-9A-Za-z]{21}[AQgw]$') == nil then typ = 'user/' end
	else
		id = ent:formatPropertyValues('P2397').value
	end
	return uri..typ..id, txt..(name or '')..'頻-{}-道'
end

function p.show()
	return
end

function p.playlist(id, name)
	if type(id) ~= 'string' or id.match('^EL[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]||PL[0-9A-Za-z_-]{16}|(?:UU|UC|PL|LL|EL)[0-9A-Za-z_-]{21}[AQgw]|PL[0-9A-Za-z_-]{32}|(?:OLAK5uy_|RDCLAK5uy_)[0-9A-Za-z_-]{32}[048AEIMQUYcgkosw]$') then error() end
	return uri..'playlist?list='..id, (name or '')..'播放列表'
end

function p.watch(id, name)
	if type(id) ~= 'string' or id.match('^[-_0-9A-Za-z]{11}$') then error() end
	return uri..'watch?v='..id, (name or '')..'影片'
end

function p.main(frame)
	local args = getArgs(frame, {
		parentOnly = true
	})

	return url(p.channel(args.channel, args[1]))
end

return p