soundcloud.lua: rewrite for changes

Complete rewrite due to website changes.

Based on reverse engineering by Daniel Ekmann, thanks!
This commit is contained in:
Pierre Ynard 2015-10-17 00:22:22 +02:00
parent d1daa6eac8
commit 98d7a343e3
1 changed files with 68 additions and 22 deletions

View File

@ -1,9 +1,10 @@
--[[ --[[
$Id$ $Id$
Copyright © 2012 the VideoLAN team Copyright © 2012, 2015 the VideoLAN team
Authors: Cheng Sun <chengsun9atgmail.com> Authors: Cheng Sun <chengsun9atgmail.com>
Pierre Ynard
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -26,30 +27,75 @@ function probe()
and string.match( vlc.path, "soundcloud%.com/.+/.+" ) and string.match( vlc.path, "soundcloud%.com/.+/.+" )
end end
function fix_quotes( value )
if string.match( value, "^\"" ) then
return "" -- field was really empty string
end
-- TODO: handle escaped backslashes and others
return string.gsub( value, "\\\"", "\"" )
end
-- Parse function. -- Parse function.
function parse() function parse()
if string.match ( vlc.path, "soundcloud%.com" ) then while true do
arturl = nil line = vlc.readline()
while true do if not line then break end
line = vlc.readline()
if not line then break end if not path then
if string.match( line, "window%.SC%.bufferTracks%.push" ) then local track = string.match( line, "soundcloud:tracks:(%d+)" )
-- all the data is nicely stored on this one line if track then
_,_,uid,token,name = string.find (line, -- API magic
"window%.SC%.bufferTracks%.push.*" .. local client_id = "02gUJC0hH2ct1EGOcYXQIzRFU91c72Ea"
"\"uid\":\"([^\"]*)\".*" .. -- app_version is not required by the API but we send it
"\"token\":\"([^\"]*)\".*" .. -- anyway to remain unconspicuous
"\"title\":\"([^\"]*)\"") local app_version = "a089efd"
-- we only want the first one of these lines
break local api = vlc.stream( "https://api.soundcloud.com/i1/tracks/"..track.."/streams?client_id="..client_id.."&app_version="..app_version )
end if not api then
-- try to get the art url break
if string.match( line, "artwork--download--link" ) then end
_,_,arturl = string.find( line, " href=\"(.*)\" " )
local streams = api:readline() -- data is on one line only
-- For now only quality available is 128 kbps (http_mp3_128_url)
path = string.match( streams, "[\"']http_mp3_%d+_url[\"'] *: *[\"'](.-)[\"']" )
if path then
-- FIXME: do this properly
path = string.gsub( path, "\\u0026", "&" )
end
end end
end end
path = "http://media.soundcloud.com/stream/"..uid.."?stream_token="..token
return { { path = path; name = name; arturl = arturl } } 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 end
return {}
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 end