soundcloud.lua 3.57 KB
Newer Older
csun's avatar
csun committed
1
2
3
--[[
 $Id$

4
 Copyright © 2012, 2015 the VideoLAN team
csun's avatar
csun committed
5
6

 Authors: Cheng Sun <chengsun9atgmail.com>
7
          Pierre Ynard
csun's avatar
csun committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

 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.
--]]

-- Probe function.
function probe()
26
27
    local path = vlc.path
    path = path:gsub("^www%.", "")
Pierre Ynard's avatar
Pierre Ynard committed
28
    return ( vlc.access == "http" or vlc.access == "https" )
29
        and string.match( path, "^soundcloud%.com/.+/.+" )
csun's avatar
csun committed
30
31
end

32
33
34
35
36
37
38
39
40
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

csun's avatar
csun committed
41
42
-- Parse function.
function parse()
43
44
45
46
    while true do
        line = vlc.readline()
        if not line then break end

47
48
49
50
51
52
53
54
        -- Parameters for API call
        if not track then
            track = string.match( line, "soundcloud:tracks:(%d+)" )
        end

        -- For private tracks
        if not secret then
            secret = string.match( line, "[\"']secret_token[\"'] *: *[\"'](.-)[\"']" )
55
56
        end

57
        -- Metadata
58
59
60
61
        if not name then
            name = string.match( line, "[\"']title[\"'] *: *\"(.-[^\\])\"" )
            if name then
                name = fix_quotes( name )
csun's avatar
csun committed
62
            end
63
64
65
66
67
68
69
70
71
72
73
74
75
        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 )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
76
            end
csun's avatar
csun committed
77
        end
78
79
80
81

        if not arturl then
            arturl = string.match( line, "[\"']artwork_url[\"'] *: *[\"'](.-)[\"']" )
        end
csun's avatar
csun committed
82
    end
83

84
85
    if track then
        -- API magic
86
        local client_id = "2t9loNQH90kzJcsFCODdigxfp325aq4z"
87
88
        -- app_version is not required by the API but we send it anyway
        -- to remain unconspicuous
89
        local app_version = "1489155300"
90

91
        local api = vlc.stream( vlc.access.."://api.soundcloud.com/i1/tracks/"..track.."/streams?client_id="..client_id.."&app_version="..app_version..( secret and "&secret_token="..secret or "" ) )
92
93
94
95
96
97
98
99
100
101
102
103

        if api then
            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

104
105
106
107
108
109
    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 } }
csun's avatar
csun committed
110
end