2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 -- NOTE: Vimeo is picky about the user-agent string.
26 local Vimeo
= {} -- Utility functions unique to this script.
28 -- Identify the media script.
30 local C
= require
'quvi/const'
32 r
.categories
= C
.proto_http
33 local U
= require
'quvi/util'
34 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/%d+$"})
38 -- Query available formats.
39 function query_formats(self
)
40 local config
= Vimeo
.get_config(self
)
41 local formats
= Vimeo
.iter_formats(self
, config
)
44 for _
,v
in pairs(formats
) do
45 table.insert(t
, Vimeo
.to_s(v
))
49 self
.formats
= table.concat(t
, "|")
56 self
.host_id
= "vimeo"
58 local c
= Vimeo
.get_config(self
)
60 local s
= c
:match('"title":(.-),') or error("no match: media title")
61 local U
= require
'quvi/util'
62 self
.title
= U
.slash_unescape(s
):gsub('^"',''):gsub('"$','')
64 self
.duration
= (tonumber(c
:match('"duration":(%d+)')) or 0) * 1000
66 local s
= c
:match('"thumbnail":"(.-)"') or ''
68 self
.thumbnail_url
= U
.slash_unescape(s
)
71 local formats
= Vimeo
.iter_formats(self
, c
)
72 local format = U
.choose_format(self
, formats
,
76 or error("unable to choose format")
77 self
.url
= {format.url
or error("no match: media stream 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 self
.id
= self
.page_url
:match('vimeo.com/(%d+)')
95 or error("no match: media ID")
97 local c_url
= "http://vimeo.com/" .. self
.id
98 local c
= quvi
.fetch(c_url
, {fetch_type
='config'})
100 if c
:match('<error>') then
101 local s
= c
:match('<message>(.-)[\n<]')
102 error( (not s
) and "no match: error message" or s
)
108 function Vimeo
.iter_formats(self
, config
)
110 local qualities
= config
:match('"qualities":%[(.-)%]')
111 or error('no match: qualities')
112 for q
in qualities
:gmatch('"(.-)"') do
113 Vimeo
.add_format(self
, config
, t
, q
)
118 function Vimeo
.add_format(self
, config
, t
, quality
)
119 table.insert(t
, {quality
=quality
,
120 url
=Vimeo
.to_url(self
, config
, quality
)})
123 function Vimeo
.choose_best(t
) -- First 'hd', then 'sd' and 'mobile' last.
124 for _
,v
in pairs(t
) do
125 local f
= Vimeo
.to_s(v
)
126 for _
,q
in pairs({'hd','sd','mobile'}) do
127 if f
== q
then return v
end
130 return Vimeo
.choose_default(t
)
133 function Vimeo
.choose_default(t
)
134 for _
,v
in pairs(t
) do
135 if Vimeo
.to_s(v
) == 'sd' then return v
end -- Default to 'sd'.
137 return t
[1] -- Or whatever is the first.
140 function Vimeo
.to_url(self
, config
, quality
)
141 local sign
= config
:match('"signature":"(.-)"')
142 or error("no match: request signature")
144 local exp = config
:match('"timestamp":(%d+)')
145 or error("no match: request timestamp")
147 local s
= "http://player.vimeo.com/play_redirect?clip_id=%s"
148 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
150 return string.format(s
, self
.id
, sign
, exp, quality
)
153 function Vimeo
.to_s(t
)
154 return string.format("%s", t
.quality
)
157 -- vim: set ts=4 sw=4 tw=72 expandtab: