local CATEGORY_NAME = "Advanced Simplicity"
function ulx.votesong( calling_ply, string )
v = v + 1
PrintMessage( HUD_PRINTTALK, "A vote has been started to play the song," .. data.title .. ". Type !votesong" .. string .. ", to vote!" )
if v == 3 then
game.ConsoleCommand( "ulx youtube " .. string .. "" )
ulx.fancyLogAdmin( "Console has passed the vote to play the song " .. data.title .. "" )
v = 0
if ply:IsUserGroup( "superadmin" ) then
hook.Add( "VetoSong", "VetoMusic", function( calling_ply, text )
text = string.lower( text )
if ( text == "!vetosong" ) then
v = 0
return ""
end
end
end )
end
end
local function videoDetails( VideoID, callback )
local URL - ()
url[ 1 ] =
"https://youtube.com/oembed"
url[ 2 ] =
"?format=json"
url[ 3 ] =
"&url=http://youtube.com/watch?v="
url[ 4 ] =
url = table.concat( url )
http:Fetch( url, function (body, length, headers, code)
callback(util.JSONToTable(body))
end )
end
videoDetails("Y61jFaKRTrI", function(data)
print(data.title)
end)
if SERVER then
util.AddNetworkString("PlayYouTubeAudio")
end
local function APIRequest(videoID, callback)
local url = {}
url[1] = "https://youtubeinmp3.com/fetch/?format=JSON&video="
url[2] = "https://www.youtube.com/watch?v="
url[3] = videoID
url = table.concat(url)
http.Fetch(url, function(body, len, headers, code)
if code ~= 200 then
return callback(false, "API request failed")
end
local json = util.JSONToTable(body)
if not json then
local shortenerUrl = "http://tny.im/yourls-api.php"
local shortenerParams = {
action = "shorturl",
url = url,
format = "json"
}
http.Post(shortenerUrl, shortenerParams, function(body, len, headers, code)
return callback(false, "Go to " .. util.JSONToTable(body).shorturl .. ", click \"Convert & Download MP3\" then try requesting this song again.")
end)
elseif json.error then
return callback(false, json.error)
else
return callback(true, json)
end
end)
end
if CLIENT then
local station
local function play(url)
if IsValid(station) then station:Stop() end
sound.PlayURL(url, "", function(_station)
station = _station
if IsValid(station) then
station:Play()
end
end)
end
net.Receive("PlayYouTubeAudio", function()
APIRequest(net.ReadString(), function(ok, data)
if not ok then
print(data)
return false
end
play(data.link)
end)
end)
end
function ulx.youtube(calling_ply, videoID)
if string.len(videoID) ~= 11 then
ULib.tsayError(calling_ply, "Invalid video ID. Example: !youtube Y6ljFaKRTrI")
return false
end
APIRequest(videoID, function(ok, data)
if not ok then
ULib.tsayError(calling_ply, data)
return false
end
net.Start("PlayYouTubeAudio")
net.WriteString(videoID)
net.Broadcast()
ulx.fancyLogAdmin(calling_ply, "#A played song: #s", data.title)
end)
end
local youtube = ulx.command(CATEGORY_NAME, "ulx youtube", ulx.youtube, "!youtube")
youtube:addParam{type=ULib.cmds.StringArg, hint="YouTube video ID", ULib.cmds.takeRestOfLine}
youtube:defaultAccess(ULib.ACCESS_ADMIN)
youtube:help("Plays audio from a YouTube video.")