Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / soundcloud.lua
blob33612303a6083bc033031668bc4fa327a9447c00
2 -- quvi
3 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
4 --
5 -- This file is part of quvi <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
23 local Soundcloud = {} -- Utility functions unique to this script
25 -- Identify the script.
26 function ident(self)
27 package.path = self.script_dir .. '/?.lua'
28 local C = require 'quvi/const'
29 local r = {}
30 r.domain = "soundcloud.com"
31 r.formats = "default"
32 r.categories = C.proto_http
33 local U = require 'quvi/util'
34 r.handles = U.handles(self.page_url,
35 {r.domain}, {"/.+/.+$", "/player.swf"})
36 return r
37 end
39 -- Query available formats.
40 function query_formats(self)
41 self.formats = 'default'
42 return self
43 end
45 -- Parse media URL.
46 function parse(self)
47 self.host_id = "soundcloud"
49 Soundcloud.normalize(self)
51 local page = quvi.fetch(self.page_url)
53 local _,_,s = page:find("window%.SC%.bufferTracks%.push(%(.-%);)")
54 local metadata = s or error("no match: metadata")
56 local _,_,s = metadata:find('"uid":"(%w-)"')
57 self.id = s or error("no match: media id")
59 local _,_,s = metadata:find('"title":"(.-)"')
60 local title = s or error("no match: media title")
61 -- Unescape the Unicode strings if any
62 -- the HTML will be unescaped by quvi itself
63 self.title = string.gsub(title, "\\u(%d+)",
64 function (h)
65 return string.char(tonumber(h, 16))
66 end)
68 local _,_,s = page:find('content="([:/%w%?%.-]-)" property="og:image"')
69 self.thumbnail_url = s or ""
71 local _,_,s = metadata:find('"duration":(%d-),')
72 self.duration = tonumber(s) or 0
74 local _,_,s = metadata:find('"streamUrl":"(.-)"')
75 self.url = { s } or error("no match: stream URL")
77 return self
78 end
81 -- Utility functions
84 function Soundcloud.normalize(self) -- "Normalize" an embedded URL
85 local url = self.page_url:match('swf%?url=(.-)$')
86 if not url then return end
88 local U = require 'quvi/util'
89 local oe_url = string.format(
90 'http://soundcloud.com/oembed?url=%s&format=json', U.unescape(url))
92 local s = quvi.fetch(oe_url):match('href=\\"(.-)\\"')
93 self.page_url = s or error('no match: page url')
94 end
96 -- vim: set ts=4 sw=4 tw=72 expandtab: