FIX: media/vimeo: Reimpl. support
[libquvi-scripts.git] / share / media / vimeo.lua
blob192073f820e326ed7da2811957aa3512846e638e
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 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'
43 local j = J.decode(c)
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 ''
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.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')
79 end
81 function Vimeo.thumb_new(j)
82 local r = {}
83 for _,u in pairs(j.video.thumbs) do
84 table.insert(r,u)
85 end
86 return r[#r] or ''
87 end
89 function Vimeo.normalize(qargs)
90 qargs_input_url = qargs.input_url:gsub("player%.", "")
91 qargs.input_url = qargs.input_url:gsub("/video/", "/")
92 end
94 function Vimeo.iter_streams(qargs, j)
95 local S = require 'quvi/stream'
96 local h = j.request.files.h264
98 local r = {}
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)
106 table.insert(r,s)
109 if #r >1 then
110 Vimeo.ch_best(S, r)
113 return r
116 function Vimeo.ch_best(S, t)
117 local r = t[1]
118 r.flags.best = true
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: