1-- libquvi-scripts v0.9.20131130
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.
10--
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.
15--
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/>.
19--
20
21local Soundcloud = {} -- Utility functions unique to this script
22
23-- Identify the playlist script.
24function ident(qargs)
25  return {
26    domains = table.concat({'soundcloud.com'}, ','),
27    can_parse_url = Soundcloud.can_parse_url(qargs)
28  }
29end
30
31-- Parse playlist properties.
32function parse(qargs)
33
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
38
39  local p = quvi.http.fetch(qargs.input_url).data
40
41  qargs.thumb_url = p:match('.+content="(.-)"%s+property="og:image"') or ''
42  qargs.title = p:match('.+content="(.-)"%s+property="og:title"') or ''
43
44  local m = 'class="info">.-href="(.-)"'
45         .. '.-class="set%-track%-title".->(.-)<'
46         .. '.-class="time">(.-)<'
47
48  qargs.media = {}
49
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
56    }
57    table.insert(qargs.media, r)
58  end
59
60  return qargs
61end
62
63--
64-- Utility functions
65--
66
67function 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
78end
79
80-- vim: set ts=2 sw=2 tw=72 expandtab:
81