3 -- Copyright (C) 2010-2012 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
24 -- NOTE: Vimeo is picky about the user-agent string.
27 local Vimeo
= {} -- Utility functions unique to this script.
29 -- Identify the script.
31 package
.path
= self
.script_dir
.. '/?.lua'
32 local C
= require
'quvi/const'
34 r
.domain
= "vimeo%.com"
35 r
.formats
= "default|best"
36 r
.categories
= C
.proto_http
37 local U
= require
'quvi/util'
38 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/%d+$"})
42 -- Query available formats.
43 function query_formats(self
)
44 local config
= Vimeo
.get_config(self
)
45 local formats
= Vimeo
.iter_formats(self
, config
)
48 for _
,v
in pairs(formats
) do
49 table.insert(t
, Vimeo
.to_s(v
))
53 self
.formats
= table.concat(t
, "|")
60 self
.host_id
= "vimeo"
62 local c
= Vimeo
.get_config(self
)
64 local s
= c
:match('"title":(.-),') or error("no match: media title")
65 local U
= require
'quvi/util'
66 self
.title
= U
.slash_unescape(s
):gsub('^"',''):gsub('"$','')
68 self
.duration
= (tonumber(c
:match('"duration":(%d+)')) or 0) * 1000
70 local s
= c
:match('"thumbnail":"(.-)"') or ''
72 self
.thumbnail_url
= U
.slash_unescape(s
)
75 local formats
= Vimeo
.iter_formats(self
, c
)
76 local format = U
.choose_format(self
, formats
,
80 or error("unable to choose format")
81 self
.url
= {format.url
or error("no match: media stream URL")}
89 function Vimeo
.normalize(url
)
90 url
= url
:gsub("player.", "") -- player.vimeo.com
91 url
= url
:gsub("/video/", "/") -- player.vimeo.com
95 function Vimeo
.get_config(self
)
96 self
.page_url
= Vimeo
.normalize(self
.page_url
)
98 self
.id
= self
.page_url
:match('vimeo.com/(%d+)')
99 or error("no match: media ID")
101 local c_url
= "player.vimeo.com/config/" .. self
.id
102 local c
= quvi
.fetch(c_url
, {fetch_type
='config'})
104 if c
:match('<error>') then
105 local s
= c
:match('<message>(.-)[\n<]')
106 error( (not s
) and "no match: error message" or s
)
112 function Vimeo
.iter_formats(self
, config
)
114 local qualities
= config
:match('"qualities":%[(.-)%]')
115 or error('no match: qualities')
116 for q
in qualities
:gmatch('"(.-)"') do
117 Vimeo
.add_format(self
, config
, t
, q
)
122 function Vimeo
.add_format(self
, config
, t
, quality
)
123 table.insert(t
, {quality
=quality
,
124 url
=Vimeo
.to_url(self
, config
, quality
)})
127 function Vimeo
.choose_best(t
) -- First 'hd', then 'sd' and 'mobile' last.
128 for _
,v
in pairs(t
) do
129 local f
= Vimeo
.to_s(v
)
130 for _
,q
in pairs({'hd','sd','mobile'}) do
131 if f
== q
then return v
end
134 return Vimeo
.choose_default(t
)
137 function Vimeo
.choose_default(t
)
138 for _
,v
in pairs(t
) do
139 if Vimeo
.to_s(v
) == 'sd' then return v
end -- Default to 'sd'.
141 return t
[1] -- Or whatever is the first.
144 function Vimeo
.to_url(self
, config
, quality
)
145 local sign
= config
:match('"signature":"(.-)"')
146 or error("no match: request signature")
148 local exp = config
:match('"timestamp":(%d+)')
149 or error("no match: request timestamp")
151 local s
= "http://player.vimeo.com/play_redirect?clip_id=%s"
152 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
154 return string.format(s
, self
.id
, sign
, exp, quality
)
157 function Vimeo
.to_s(t
)
158 return string.format("%s", t
.quality
)
161 -- vim: set ts=4 sw=4 tw=72 expandtab: