ident: Remove r.formats
[libquvi-scripts.git] / share / lua / media / vimeo.lua
blob0b6838f2c2406682881feadfadd372b0581a599c
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 C = require 'quvi/const'
31 local r = {}
32 r.categories = C.proto_http
33 local U = require 'quvi/util'
34 r.handles = U.handles(self.page_url, {r.domain}, {"/%d+$"})
35 return r
36 end
38 -- Query available formats.
39 function query_formats(self)
40 local config = Vimeo.get_config(self)
41 local formats = Vimeo.iter_formats(self, config)
43 local t = {}
44 for _,v in pairs(formats) do
45 table.insert(t, Vimeo.to_s(v))
46 end
48 table.sort(t)
49 self.formats = table.concat(t, "|")
51 return self
52 end
54 -- Parse media URL.
55 function parse(self)
56 self.host_id = "vimeo"
58 local c = Vimeo.get_config(self)
60 local s = c:match('"title":(.-),') or error("no match: media title")
61 local U = require 'quvi/util'
62 self.title = U.slash_unescape(s):gsub('^"',''):gsub('"$','')
64 self.duration = (tonumber(c:match('"duration":(%d+)')) or 0) * 1000
66 local s = c:match('"thumbnail":"(.-)"') or ''
67 if #s >0 then
68 self.thumbnail_url = U.slash_unescape(s)
69 end
71 local formats = Vimeo.iter_formats(self, c)
72 local format = U.choose_format(self, formats,
73 Vimeo.choose_best,
74 Vimeo.choose_default,
75 Vimeo.to_s)
76 or error("unable to choose format")
77 self.url = {format.url or error("no match: media stream URL")}
78 return self
79 end
82 -- Utility functions
85 function Vimeo.normalize(url)
86 url = url:gsub("player.", "") -- player.vimeo.com
87 url = url:gsub("/video/", "/") -- player.vimeo.com
88 return url
89 end
91 function Vimeo.get_config(self)
92 self.page_url = Vimeo.normalize(self.page_url)
94 self.id = self.page_url:match('vimeo.com/(%d+)')
95 or error("no match: media ID")
97 local c_url = "http://vimeo.com/" .. self.id
98 local c = quvi.fetch(c_url, {fetch_type='config'})
100 if c:match('<error>') then
101 local s = c:match('<message>(.-)[\n<]')
102 error( (not s) and "no match: error message" or s )
105 return c
108 function Vimeo.iter_formats(self, config)
109 local t = {}
110 local qualities = config:match('"qualities":%[(.-)%]')
111 or error('no match: qualities')
112 for q in qualities:gmatch('"(.-)"') do
113 Vimeo.add_format(self, config, t, q)
115 return t
118 function Vimeo.add_format(self, config, t, quality)
119 table.insert(t, {quality=quality,
120 url=Vimeo.to_url(self, config, quality)})
123 function Vimeo.choose_best(t) -- First 'hd', then 'sd' and 'mobile' last.
124 for _,v in pairs(t) do
125 local f = Vimeo.to_s(v)
126 for _,q in pairs({'hd','sd','mobile'}) do
127 if f == q then return v end
130 return Vimeo.choose_default(t)
133 function Vimeo.choose_default(t)
134 for _,v in pairs(t) do
135 if Vimeo.to_s(v) == 'sd' then return v end -- Default to 'sd'.
137 return t[1] -- Or whatever is the first.
140 function Vimeo.to_url(self, config, quality)
141 local sign = config:match('"signature":"(.-)"')
142 or error("no match: request signature")
144 local exp = config:match('"timestamp":(%d+)')
145 or error("no match: request timestamp")
147 local s = "http://player.vimeo.com/play_redirect?clip_id=%s"
148 .. "&sig=%s&time=%s&quality=%s&type=moogaloop_local"
150 return string.format(s, self.id, sign, exp, quality)
153 function Vimeo.to_s(t)
154 return string.format("%s", t.quality)
157 -- vim: set ts=4 sw=4 tw=72 expandtab: