FIX: Potential "attempt to index ... (a nil value)"
[libquvi-scripts.git] / share / lua / website / vimeo.lua
blobb8e74ffe4f259c8102836817fe33eb56495c6f64
2 -- libquvi-scripts
3 -- Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <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 -- w/ HD: <http://vimeo.com/1485507>
24 -- no HD: <http://vimeo.com/10772672>
26 local Vimeo = {} -- Utility functions unique to this script.
28 -- Identify the script.
29 function ident(self)
30 package.path = self.script_dir .. '/?.lua'
31 local C = require 'quvi/const'
32 local r = {}
33 r.domain = "vimeo%.com"
34 r.formats = "default|best"
35 r.categories = C.proto_http
36 local U = require 'quvi/util'
37 r.handles = U.handles(self.page_url, {r.domain}, {"/%d+$"})
38 return r
39 end
41 -- Query available formats.
42 function query_formats(self)
43 local config = Vimeo.get_config(self)
44 local formats = Vimeo.iter_formats(self, config)
46 local t = {}
47 for _,v in pairs(formats) do
48 table.insert(t, Vimeo.to_s(v))
49 end
51 table.sort(t)
52 self.formats = table.concat(t, "|")
54 return self
55 end
57 -- Parse media URL.
58 function parse(self)
59 self.host_id = "vimeo"
60 local config = Vimeo.get_config(self)
62 local _,_,s = config:find("<caption>(.-)</")
63 self.title = s or error("no match: media title")
65 local _,_,s = config:find('<duration>(%d+)')
66 self.duration = (tonumber(s) or 0) * 1000 -- to msec
68 local _,_,s = config:find('<thumbnail>(.-)<')
69 self.thumbnail_url = s or ''
71 local formats = Vimeo.iter_formats(self, config)
72 local U = require 'quvi/util'
73 local format = U.choose_format(self, formats,
74 Vimeo.choose_best,
75 Vimeo.choose_default,
76 Vimeo.to_s)
77 or error("unable to choose format")
78 self.url = {format.url or error("no match: media url")}
79 return self
80 end
83 -- Utility functions
86 function Vimeo.normalize(url)
87 url = url:gsub("player.", "") -- player.vimeo.com
88 url = url:gsub("/video/", "/") -- player.vimeo.com
89 return url
90 end
92 function Vimeo.get_config(self)
93 self.page_url = Vimeo.normalize(self.page_url)
95 local _,_,s = self.page_url:find('vimeo.com/(%d+)')
96 self.id = s or error("no match: media id")
98 local config_url = "http://vimeo.com/moogaloop/load/clip:" .. self.id
99 local config = quvi.fetch(config_url, {fetch_type = 'config'})
101 if config:find('<error>') then
102 local _,_,s = config:find('<message>(.-)[\n<]')
103 error( (not s) and "no match: error message" or s )
106 return config
109 function Vimeo.iter_formats(self, config)
110 local _,_,s = config:find('<isHD>(%d+)')
111 local isHD = tonumber(s) or 0
113 local t = {}
114 Vimeo.add_format(self, config, t, 'sd')
115 if isHD == 1 then
116 Vimeo.add_format(self, config, t, 'hd')
119 return t
122 function Vimeo.add_format(self, config, t, quality)
123 table.insert(t,
124 {quality=quality,
125 url=Vimeo.to_url(self, config, quality)})
128 function Vimeo.choose_best(formats) -- Last is 'best'
129 local r
130 for _,v in pairs(formats) do r = v end
131 return r
134 function Vimeo.choose_default(formats) -- First is 'default'
135 for _,v in pairs(formats) do return v end
138 function Vimeo.to_url(self, config, quality)
139 local _,_,s = config:find("<request_signature>(.-)</")
140 local sign = s or error("no match: request signature")
142 local _,_,s = config:find("<request_signature_expires>(.-)</")
143 local exp = s or error("no match: request signature expires")
145 local fmt_s = "http://vimeo.com/moogaloop/play/clip:%s/%s/%s/?q=%s"
147 return string.format(fmt_s, self.id, sign, exp, quality)
150 function Vimeo.to_s(t)
151 return string.format("%s", t.quality)
154 -- vim: set ts=4 sw=4 tw=72 expandtab: