share/Makefile.am: Reorganize blocks, comments
[libquvi-scripts.git] / share / lua / media / vimeo.lua
blob64904f680978a08ccef652c0973c25290b3665b7
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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
19 -- 02110-1301 USA
23 -- NOTE: Vimeo is picky about the user-agent string.
26 local Vimeo = {} -- Utility functions unique to this script.
28 -- Identify the media script.
29 function ident(qargs)
30 local A = require 'quvi/accepts'
31 local C = require 'quvi/const'
32 local r = {
33 accepts = A.accepts(qargs.input_url, {"vimeo%.com"}, {"/%d+$"}),
34 categories = C.qmspc_http
36 return r
37 end
39 -- Parse media stream URL.
40 function parse(qargs)
41 Vimeo.normalize(qargs)
43 local p = quvi.fetch(qargs.input_url)
44 local U = require 'quvi/util'
46 qargs.id = qargs.input_url:match('/(%d+)$') or error('no match: media ID')
47 qargs.duration_ms =(tonumber(p:match('"duration":(%d+)')) or 0) * 1000
48 qargs.thumb_url = U.slash_unescape(p:match('"thumbnail":"(.-)"') or '')
50 local s = p:match('"title":(.-),') or ''
51 qargs.title = U.slash_unescape(s):gsub('^"',''):gsub('"$','')
53 qargs.streams = Vimeo.iter_streams(qargs, p)
55 return qargs
56 end
59 -- Utility functions
62 function Vimeo.iter_streams(qargs, page)
63 local p = page:match('"profiles":{(.-)},') or error('no match: profiles')
65 local rs = page:match('"signature":"(.-)"')
66 or error('no match: request signature')
68 local rt = page:match('"timestamp":(%d+)')
69 or error('no match: request timestamp')
71 local f = "http://player.vimeo.com/play_redirect?clip_id=%s"
72 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
74 local S = require 'quvi/stream'
75 local r = {}
77 for e,a in p:gmatch('"(.-)":{(.-)}') do -- For each profile.
78 for q in a:gmatch('"(.-)":%d+') do -- For each quality in the profile.
79 local u = string.format(f, qargs.id, rs, rt, q)
80 local t = S.stream_new(u)
81 t.video.encoding = string.lower(e or '')
82 t.id = Vimeo.to_id(t, q)
83 table.insert(r, t)
84 end
85 end
87 if #r >1 then
88 Vimeo.ch_best(r)
89 end
91 return r
92 end
94 function Vimeo.normalize(qargs)
95 local u = qargs.input_url:gsub("player.", "") -- player.vimeo.com
96 qargs.input_url = u:gsub("/video/", "/")
97 end
99 function Vimeo.ch_best(t)
100 t[1].flags.best = true -- Should be the 'hd'.
103 -- Return an ID for a stream.
104 function Vimeo.to_id(t, quality)
105 return string.format("%s_%s", quality, t.video.encoding)
108 -- vim: set ts=2 sw=2 tw=72 expandtab: