media/tagtele.lua: Relicense under AGPLv3+
[libquvi-scripts.git] / share / media / soundcloud.lua
blobacc42a25cf05e424aae72a69963d410e8fa65bb2
1 -- libquvi-scripts
2 -- Copyright (C) 2012-2013 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 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.
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.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);
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 -- Not a playlist URL.
64 not t.path:lower():match('^/.-/sets/[%w-_]+/$')
65 -- But has the following path properties.
66 and t.path:lower():match('^/.+/.+$')
68 then
69 return true
70 else
71 return false
72 end
73 end
75 function Soundcloud.normalize(qargs) -- "Normalize" an embedded URL
76 local url = qargs.input_url:match('swf%?url=(.-)$')
77 if not url then return end
79 local U = require 'quvi/util'
80 local u = string.format('http://soundcloud.com/oembed?url=%s&format=json',
81 U.unescape(url))
83 qargs.input_url = quvi.http.fetch(u).data:match('href=\\"(.-)\\"')
84 or error('no match: media URL')
85 end
87 function Soundcloud.iter_streams(p)
88 local u = p:match('"streamUrl":"(.-)"')
89 or error("no match: media stream URL")
90 local S = require 'quvi/stream'
91 return {S.stream_new(u)}
92 end
94 -- vim: set ts=2 sw=2 tw=72 expandtab: