media/sevenload.lua: Rewrite parse function
[libquvi-scripts.git] / share / media / vimeo.lua
blob849a71cdc72b94fceb668a33613d6bc99d3bb712
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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.
28 function ident(qargs)
29 return {
30 can_parse_url = Vimeo.can_parse_url(qargs),
31 domains = table.concat({'vimeo.com'}, ',')
33 end
35 -- Parse media stream URL.
36 function parse(qargs)
37 Vimeo.normalize(qargs)
39 local p = quvi.http.fetch(qargs.input_url).data
40 local U = require 'quvi/util'
42 qargs.id = qargs.input_url:match('/(%d+)$') or error('no match: media ID')
43 qargs.duration_ms =(tonumber(p:match('"duration":(%d+)')) or 0) * 1000
44 qargs.thumb_url = U.slash_unescape(p:match('"thumbnail":"(.-)"') or '')
46 local s = p:match('"title":(.-),') or ''
47 qargs.title = U.slash_unescape(s):gsub('^"',''):gsub('"$','')
49 qargs.streams = Vimeo.iter_streams(qargs, p)
51 return qargs
52 end
55 -- Utility functions
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+$')
65 then
66 return true
67 else
68 return false
69 end
70 end
72 function Vimeo.iter_streams(qargs, page)
73 local p = page:match('"profiles":{(.-)},') or error('no match: profiles')
75 local rs = page:match('"signature":"(.-)"')
76 or error('no match: request signature')
78 local rt = page:match('"timestamp":(%d+)')
79 or error('no match: request timestamp')
81 local f = "http://player.vimeo.com/play_redirect?clip_id=%s"
82 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
84 local S = require 'quvi/stream'
85 local r = {}
87 for e,a in p:gmatch('"(.-)":{(.-)}') do -- For each profile.
88 for q in a:gmatch('"(.-)":%d+') do -- For each quality in the profile.
89 local u = string.format(f, qargs.id, rs, rt, q)
90 local t = S.stream_new(u)
91 t.video.encoding = string.lower(e or '')
92 t.id = Vimeo.to_id(t, q)
93 table.insert(r, t)
94 end
95 end
97 if #r >1 then
98 Vimeo.ch_best(r)
99 end
101 return r
104 function Vimeo.normalize(qargs)
105 qargs_input_url = qargs.input_url:gsub("player.", "")
106 qargs.input_url = qargs.input_url:gsub("/video/", "/")
109 function Vimeo.ch_best(t)
110 t[1].flags.best = true -- Should be the 'hd'.
113 -- Return an ID for a stream.
114 function Vimeo.to_id(t, quality)
115 return string.format("%s_%s", quality, t.video.encoding)
118 -- vim: set ts=2 sw=2 tw=72 expandtab: