Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / playlist / soundcloud.lua
blob48fc86aac19404dfd39db2167f31922e32806dc5
1 -- libquvi-scripts
2 -- Copyright (C) 2012-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local Soundcloud = {} -- Utility functions unique to this script
23 -- Identify the playlist script.
24 function ident(qargs)
25 return {
26 domains = table.concat({'soundcloud.com'}, ','),
27 can_parse_url = Soundcloud.can_parse_url(qargs)
29 end
31 -- Parse playlist properties.
32 function parse(qargs)
34 qargs.id, s = qargs.input_url:match('/([%w-_]+)/sets/([%w-_]+)/')
35 if qargs.id and s then
36 qargs.id = qargs.id .."_".. s
37 end
39 local p = quvi.http.fetch(qargs.input_url).data
41 qargs.thumb_url = p:match('.+content="(.-)"%s+property="og:image"') or ''
42 qargs.title = p:match('.+content="(.-)"%s+property="og:title"') or ''
44 local m = 'class="info">.-href="(.-)"'
45 .. '.-class="set%-track%-title".->(.-)<'
46 .. '.-class="time">(.-)<'
48 qargs.media = {}
50 for u,t,d in p:gmatch(m) do
51 local m,s = d:match('(%d+)%.(%d+)')
52 local r = {
53 duration_ms = ((tonumber(m or '0')*60) + tonumber(s or '0')) *1000,
54 url = "http://soundcloud.com" ..u,
55 title = t
57 table.insert(qargs.media, r)
58 end
60 return qargs
61 end
64 -- Utility functions
67 function Soundcloud.can_parse_url(qargs)
68 local U = require 'socket.url'
69 local t = U.parse(qargs.input_url)
70 if t and t.scheme and t.scheme:lower():match('^https?$')
71 and t.host and t.host:lower():match('soundcloud%.com$')
72 and t.path and t.path:lower():match('^/.-/sets/[%w-_]+/$')
73 then
74 return true
75 else
76 return false
77 end
78 end
80 -- vim: set ts=2 sw=2 tw=72 expandtab: