2 -- Copyright (C) 2012-2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local Soundcloud
= {} -- Utility functions unique to this script
24 -- Identify the media script.
27 can_parse_url
= Soundcloud
.can_parse_url(qargs
),
28 domains
= table.concat({'soundcloud.com'}, ',')
32 -- Parse media properties.
34 Soundcloud
.normalize(qargs
)
36 local p
= quvi
.http
.fetch(qargs
.input_url
).data
38 qargs
.thumb_url
= p
:match('.+content="(.-)"%s+property="og:image"') or ''
39 qargs
.title
= p
:match('.+content="(.-)"%s+property="og:title"') or ''
41 local m
= p
:match("window%.SC%.bufferTracks%.push(%(.-%);)")
42 or error("no match: metadata")
44 qargs
.duration_ms
= tonumber(m
:match('"duration":(%d-),')) or 0
45 qargs
.id
= m
:match('"uid":"(%w-)"') or ''
47 qargs
.streams
= Soundcloud
.iter_streams(m
);
56 function Soundcloud
.can_parse_url(qargs
)
57 Soundcloud
.normalize(qargs
)
58 local U
= require
'socket.url'
59 local t
= U
.parse(qargs
.input_url
)
60 if t
and t
.scheme
and t
.scheme
:lower():match('^http?$')
61 and t
.host
and t
.host
:lower():match('soundcloud%.com$')
63 t
.path
:lower():match('^/.+/.+$')
64 or t
.path
:lower():match('/player%.swf$')
73 function Soundcloud
.normalize(qargs
) -- "Normalize" an embedded URL
74 local url
= qargs
.input_url
:match('swf%?url=(.-)$')
75 if not url
then return end
77 local U
= require
'quvi/util'
78 local u
= string.format('http://soundcloud.com/oembed?url=%s&format=json',
81 qargs
.input_url
= quvi
.http
.fetch(u
).data
:match('href=\\"(.-)\\"')
82 or error('no match: media URL')
85 function Soundcloud
.iter_streams(p
)
86 local u
= p
:match('"streamUrl":"(.-)"')
87 or error("no match: media stream URL")
88 local S
= require
'quvi/stream'
89 return {S
.stream_new(u
)}
92 -- vim: set ts=2 sw=2 tw=72 expandtab: