media/: Use socket.url
[libquvi-scripts.git] / share / media / vimeo.lua
blob183591da3a0d69ef96a2a8a323f0253010f6f77b
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 return {
31 can_parse_url = Vimeo.can_parse_url(qargs),
32 domains = table.concat({'vimeo.com'}, ',')
34 end
36 -- Parse media stream URL.
37 function parse(qargs)
38 Vimeo.normalize(qargs)
40 local p = quvi.fetch(qargs.input_url)
41 local U = require 'quvi/util'
43 qargs.id = qargs.input_url:match('/(%d+)$') or error('no match: media ID')
44 qargs.duration_ms =(tonumber(p:match('"duration":(%d+)')) or 0) * 1000
45 qargs.thumb_url = U.slash_unescape(p:match('"thumbnail":"(.-)"') or '')
47 local s = p:match('"title":(.-),') or ''
48 qargs.title = U.slash_unescape(s):gsub('^"',''):gsub('"$','')
50 qargs.streams = Vimeo.iter_streams(qargs, p)
52 return qargs
53 end
56 -- Utility functions
59 function Vimeo.can_parse_url(qargs)
60 Vimeo.normalize(qargs)
61 local U = require 'socket.url'
62 local t = U.parse(qargs.input_url)
63 if t and t.scheme and t.scheme:lower():match('^http$')
64 and t.host and t.host:lower():match('vimeo%.com$')
65 and t.path and t.path:lower():match('^/%d+$')
66 then
67 return true
68 else
69 return false
70 end
71 end
73 function Vimeo.iter_streams(qargs, page)
74 local p = page:match('"profiles":{(.-)},') or error('no match: profiles')
76 local rs = page:match('"signature":"(.-)"')
77 or error('no match: request signature')
79 local rt = page:match('"timestamp":(%d+)')
80 or error('no match: request timestamp')
82 local f = "http://player.vimeo.com/play_redirect?clip_id=%s"
83 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
85 local S = require 'quvi/stream'
86 local r = {}
88 for e,a in p:gmatch('"(.-)":{(.-)}') do -- For each profile.
89 for q in a:gmatch('"(.-)":%d+') do -- For each quality in the profile.
90 local u = string.format(f, qargs.id, rs, rt, q)
91 local t = S.stream_new(u)
92 t.video.encoding = string.lower(e or '')
93 t.id = Vimeo.to_id(t, q)
94 table.insert(r, t)
95 end
96 end
98 if #r >1 then
99 Vimeo.ch_best(r)
102 return r
105 function Vimeo.normalize(qargs)
106 qargs_input_url = qargs.input_url:gsub("player.", "")
107 qargs.input_url = qargs.input_url:gsub("/video/", "/")
110 function Vimeo.ch_best(t)
111 t[1].flags.best = true -- Should be the 'hd'.
114 -- Return an ID for a stream.
115 function Vimeo.to_id(t, quality)
116 return string.format("%s_%s", quality, t.video.encoding)
119 -- vim: set ts=2 sw=2 tw=72 expandtab: