playlist/: Change license header
[libquvi-scripts.git] / share / playlist / soundcloud.lua
blob26ca86281e0a052342484d2acee05c0eff469ec6
1 -- libquvi-scripts
2 -- Copyright (C) 2012 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 C = require 'quvi/const'
40 local o = { [C.qfo_type] = C.qft_playlist }
41 local p = quvi.fetch(qargs.input_url, o)
43 qargs.thumb_url = p:match('.+content="(.-)"%s+property="og:image"') or ''
44 qargs.title = p:match('.+content="(.-)"%s+property="og:title"') or ''
46 local m = 'class="info">.-href="(.-)"'
47 .. '.-class="set%-track%-title">(.-)<'
48 .. '.-class="time">(.-)<'
50 qargs.media = {}
52 for u,t,d in p:gmatch(m) do
53 local m,s = d:match('(%d+)%.(%d+)')
54 local r = {
55 duration_ms = ((tonumber(m or '0')*60) + tonumber(s or '0')) *1000,
56 url = "http://soundcloud" ..u,
57 title = t
59 table.insert(qargs.media, r)
60 end
62 return qargs
63 end
66 -- Utility functions
69 function Soundcloud.can_parse_url(qargs)
70 local U = require 'socket.url'
71 local t = U.parse(qargs.input_url)
72 if t and t.scheme and t.scheme:lower():match('^https?$')
73 and t.host and t.host:lower():match('soundcloud%.com$')
74 and t.path and t.path:lower():match('/sets/[%w-_]+/')
75 then
76 return true
77 else
78 return false
79 end
80 end
82 -- vim: set ts=2 sw=2 tw=72 expandtab: