Copyright © 2012, 2015, 2019-2020 the VideoLAN team
Authors: Cheng Sun <chengsun9atgmail.com>
Pierre Ynard
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
function probe()
local path = vlc.path
path = path:gsub("^www%.", "")
return ( vlc.access == "http" or vlc.access == "https" )
and string.match( path, "^soundcloud%.com/.+/.+" )
end
function fix_quotes( value )
if string.match( value, "^\"" ) then
return ""
end
return string.gsub( value, "\\\"", "\"" )
end
function extract_magic( url )
local s = vlc.stream( url )
if not s then
return nil
end
local line = s:read( 4096*1024 )
if not line then
return nil
end
local client_id = string.match( line, '[{,]client_id:"(%w+)"[},]' )
if client_id then
vlc.msg.dbg( "Found soundcloud API magic" )
return client_id
end
return nil
end
function parse()
while true do
local line = vlc.readline()
if not line then break end
if not stream then
stream = string.match( line, '"url":"([^"]-/stream/progressive[^"]-)"' )
end
if not client_id then
local script = string.match( line, '<script( .-)>' )
if script then
local src = string.match( script, ' src="(.-)"' )
if src then
client_id = extract_magic( src )
end
end
end
if not name then
name = string.match( line, "[\"']title[\"'] *: *\"(.-[^\\])\"" )
if name then
name = fix_quotes( name )
end
end
if not description then
description = string.match( line, "[\"']artwork_url[\"'] *:.-[\"']description[\"'] *: *\"(.-[^\\])\"" )
if description then
description = fix_quotes( description )
end
end
if not artist then
artist = string.match( line, "[\"']username[\"'] *: *\"(.-[^\\])\"" )
if artist then
artist = fix_quotes( artist )
end
end
if not arturl then
arturl = string.match( line, "[\"']artwork_url[\"'] *: *[\"'](.-)[\"']" )
end
end
if stream then
if client_id then
stream = stream..( string.match( stream, "?" ) and "&" or "?" ).."client_id="..client_id
end
local api = vlc.stream( stream )
if api then
local streams = api:readline()
path = string.match( streams, '"url":"(.-)"' )
end
end
if not path then
vlc.msg.err( "Couldn't extract soundcloud audio URL, please check for updates to this script" )
return { }
end
return { { path = path, name = name, description = description, artist = artist, arturl = arturl } }
end