3 -- Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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
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.
30 package
.path
= self
.script_dir
.. '/?.lua'
31 local C
= require
'quvi/const'
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+$"})
41 -- Query available formats.
42 function query_formats(self
)
43 local config
= Vimeo
.get_config(self
)
44 local formats
= Vimeo
.iter_formats(self
, config
)
47 for _
,v
in pairs(formats
) do
48 table.insert(t
, Vimeo
.to_s(v
))
52 self
.formats
= table.concat(t
, "|")
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
,
77 or error("no match: media url")}
85 function Vimeo
.normalize(url
)
86 url
= url
:gsub("player.", "") -- player.vimeo.com
87 url
= url
:gsub("/video/", "/") -- player.vimeo.com
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
)
108 function Vimeo
.iter_formats(self
, config
)
109 local _
,_
,s
= config
:find('<isHD>(%d+)')
110 local isHD
= tonumber(s
) or 0
113 Vimeo
.add_format(self
, config
, t
, 'sd')
115 Vimeo
.add_format(self
, config
, t
, 'hd')
121 function Vimeo
.add_format(self
, config
, t
, quality
)
124 url
=Vimeo
.to_url(self
, config
, quality
)})
127 function Vimeo
.choose_best(formats
) -- Last is 'best'
129 for _
,v
in pairs(formats
) do r
= v
end
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: