Split from git://repo.or.cz/quvi.git
[libquvi-scripts.git] / share / lua / website / vimeo.lua
blobf05d65bb6348d0b8a176f57c3f13b57f3c6f2fef
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 self.url = {U.choose_format(self, formats,
74 Vimeo.choose_best,
75 Vimeo.choose_default,
76 Vimeo.to_s).url
77 or error("no match: media url")}
78 return self
79 end
82 -- Utility functions
85 function Vimeo.normalize(url)
86 url = url:gsub("player.", "") -- player.vimeo.com
87 url = url:gsub("/video/", "/") -- player.vimeo.com
88 return url
89 end
91 function Vimeo.get_config(self)
92 self.page_url = Vimeo.normalize(self.page_url)
94 local _,_,s = self.page_url:find('vimeo.com/(%d+)')
95 self.id = s or error("no match: media id")
97 local config_url = "http://vimeo.com/moogaloop/load/clip:" .. self.id
98 local config = quvi.fetch(config_url, {fetch_type = 'config'})
100 if config:find('<error>') then
101 local _,_,s = config:find('<message>(.-)[\n<]')
102 error( (not s) and "no match: error message" or s )
105 return config
108 function Vimeo.iter_formats(self, config)
109 local _,_,s = config:find('<isHD>(%d+)')
110 local isHD = tonumber(s) or 0
112 local t = {}
113 Vimeo.add_format(self, config, t, 'sd')
114 if isHD == 1 then
115 Vimeo.add_format(self, config, t, 'hd')
118 return t
121 function Vimeo.add_format(self, config, t, quality)
122 table.insert(t,
123 {quality=quality,
124 url=Vimeo.to_url(self, config, quality)})
127 function Vimeo.choose_best(formats) -- Last is 'best'
128 local r
129 for _,v in pairs(formats) do r = v end
130 return r
133 function Vimeo.choose_default(formats) -- First is 'default'
134 for _,v in pairs(formats) do return v end
137 function Vimeo.to_url(self, config, quality)
138 local _,_,s = config:find("<request_signature>(.-)</")
139 local sign = s or error("no match: request signature")
141 local _,_,s = config:find("<request_signature_expires>(.-)</")
142 local exp = s or error("no match: request signature expires")
144 local fmt_s = "http://vimeo.com/moogaloop/play/clip:%s/%s/%s/?q=%s"
146 return string.format(fmt_s, self.id, sign, exp, quality)
149 function Vimeo.to_s(t)
150 return string.format("%s", t.quality)
153 -- vim: set ts=4 sw=4 tw=72 expandtab: