media/: Use socket.url
[libquvi-scripts.git] / share / media / soundcloud.lua
blob379bcabdace58bb04966712e5ef44d40ab87c3b1
1 -- libquvi-scripts
2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library 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 GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
22 local Soundcloud = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = Soundcloud.can_parse_url(qargs),
28 domains = table.concat({'soundcloud.com'}, ',')
30 end
32 -- Parse media properties.
33 function parse(qargs)
34 Soundcloud.normalize(qargs)
36 local p = quvi.fetch(qargs.input_url)
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);
49 return qargs
50 end
53 -- Utility functions
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$')
62 and t.path and (
63 t.path:lower():match('^/.+/.+$')
64 or t.path:lower():match('/player%.swf$')
66 then
67 return true
68 else
69 return false
70 end
71 end
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',
79 U.unescape(url))
81 qargs.input_url = quvi.fetch(u):match('href=\\"(.-)\\"')
82 or error('no match: media URL')
83 end
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)}
90 end
92 -- vim: set ts=2 sw=2 tw=72 expandtab: