2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program 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
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
22 -- NOTE: Vimeo is picky about the user-agent string.
25 local Vimeo
= {} -- Utility functions unique to this script.
27 -- Identify the media script.
30 can_parse_url
= Vimeo
.can_parse_url(qargs
),
31 domains
= table.concat({'vimeo.com'}, ',')
35 -- Parse media stream URL.
37 Vimeo
.normalize(qargs
)
39 qargs
.id
= qargs
.input_url
:match('/(%d+)$') or error('no match: media ID')
40 local c
= Vimeo
.config_new(qargs
)
42 local J
= require
'json'
45 qargs
.duration_ms
= tonumber(j
.video
.duration
or 0) * 1000
46 qargs
.streams
= Vimeo
.iter_streams(qargs
, j
)
48 qargs
.thumb_url
= Vimeo
.thumb_new(j
)
49 qargs
.title
= j
.video
.title
or ''
58 function Vimeo
.can_parse_url(qargs
)
59 Vimeo
.normalize(qargs
)
60 local U
= require
'socket.url'
61 local t
= U
.parse(qargs
.input_url
)
62 if t
and t
.scheme
and t
.scheme
:lower():match('^http$')
63 and t
.host
and t
.host
:lower():match('vimeo%.com$')
64 and t
.path
and t
.path
:lower():match('^/%d+$')
72 function Vimeo
.config_new(qargs
)
73 local U
= require
'socket.url'
74 local t
= U
.parse(qargs
.input_url
)
75 t
.host
= 'player.vimeo.com'
76 t
.path
= table.concat({'/video/', qargs
.id
})
77 local p
= quvi
.http
.fetch(U
.build(t
)).data
78 return p
:match('b=(.-);') or error('no match: b')
81 function Vimeo
.thumb_new(j
)
83 for _
,u
in pairs(j
.video
.thumbs
) do
89 function Vimeo
.normalize(qargs
)
90 qargs_input_url
= qargs
.input_url
:gsub("player%.", "")
91 qargs
.input_url
= qargs
.input_url
:gsub("/video/", "/")
94 function Vimeo
.iter_streams(qargs
, j
)
95 local S
= require
'quvi/stream'
96 local h
= j
.request
.files
.h264
99 for k
,v
in pairs(h
) do
100 local s
= S
.stream_new(v
.url
)
101 s
.container
= v
.url
:match('%.(%w+)%?') or ''
102 s
.video
.bitrate_kbit_s
= v
.bitrate
103 s
.video
.height
= v
.height
104 s
.video
.width
= v
.width
105 s
.id
= Vimeo
.to_id(s
,k
)
116 function Vimeo
.ch_best(S
, t
)
119 for _
,v
in pairs(t
) do
120 if v
.video
.height
> r
.video
.height
then
121 r
= S
.swap_best(r
, v
)
126 function Vimeo
.to_id(t
, quality
)
127 return string.format("%s_%s_%dk_%dp",
128 quality
, t
.container
, t
.video
.bitrate_kbit_s
, t
.video
.height
)
131 -- vim: set ts=2 sw=2 tw=72 expandtab: