Remove the query_formats function
[libquvi-scripts.git] / share / lua / media / arte.lua
blob7e7e0867b92dca4fc4550b70a60631ccb79b895e
1 -- libquvi-scripts
2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Raphaƫl Droz <raphael.droz+floss@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
22 -- NOTE: Most videos expire some (7?) days after their original broadcast
24 local Arte = {} -- Utility functions unique to to this script.
26 -- Identify the media script.
27 function ident(qargs)
28 local A = require 'quvi/accepts'
29 local C = require 'quvi/const'
30 local r = {
31 accepts = A.accepts(qargs.input_url,
32 {"videos%.arte%.tv"}, {"/%w+/videos/"}),
33 categories = C.proto_rtmp
35 return r
36 end
38 -- Parse media URL.
39 function parse(self)
40 self.host_id = 'arte'
41 local config = Arte.get_config(self)
42 local U = require 'quvi/util'
43 local formats = Arte.iter_formats(config, U)
44 local format = U.choose_format(self, formats,
45 Arte.choose_best,
46 Arte.choose_default,
47 Arte.to_s)
48 or error("unable to choose format")
49 self.title = format.title or error('no match: title')
50 self.id = format.id or error('no match: id')
51 self.thumbnail_url = format.thumb or ''
52 self.url = {format.url or error("no match: media url")}
54 return self
55 end
58 -- Utility functions
61 function Arte.get_config(self)
62 local p = quvi.fetch(self.page_url)
64 local c_url = p:match('videorefFileUrl = "(.-)"')
65 or error('no match: config URL')
67 return Arte.get_lang_config(quvi.fetch(c_url, {fetch_type='config'}))
68 end
70 function Arte.get_lang_config(config)
71 local t = {}
72 for lang,url in config:gmatch('<video lang="(%w+)" ref="(.-)"') do
73 table.insert(t, {lang=lang,
74 config=quvi.fetch(url, {fetch_type = 'config'})})
75 end
76 return t
77 end
79 function Arte.iter_lang_formats(lang_config, t, U)
81 local p = '<video id="(%d+)" lang="(%w+)"'
82 .. '.-<name>(.-)<'
83 .. '.-<firstThumbnailUrl>(.-)<'
84 .. '.-<dateExpiration>(.-)<'
85 .. '.-<dateVideo>(.-)<'
87 local config = lang_config.config
89 local id,lang,title,thumb,exp,date = config:match(p)
90 if not id then error("no match: media id, etc.") end
92 if lang ~= lang_config.lang then
93 error("no match: lang")
94 end
96 if Arte.has_expired(exp, U) then
97 error('error: media no longer available (expired)')
98 end
100 local urls = config:match('<urls>(.-)</urls>')
101 or error('no match: urls')
103 for q,u in urls:gmatch('<url quality="(%w+)">(.-)<') do
104 -- print(q,u)
105 table.insert(t, {lang=lang, quality=q, url=u,
106 thumb=thumb, title=title, id=id})
110 function Arte.iter_formats(config, U)
111 local t = {}
112 for _,v in pairs(config) do
113 Arte.iter_lang_formats(v, t, U)
115 return t
118 function Arte.has_expired(s, U)
119 return U.to_timestamp(s) - os.time() < 0
122 function Arte.choose_best(formats) -- Whatever matches 'hd' first
123 local r
124 for _,v in pairs(formats) do
125 if Arte.to_s(v):match('hd') then
126 return v
129 return r
132 function Arte.choose_default(formats) -- Whatever matches 'sd' first
133 local r
134 for _,v in pairs(formats) do
135 if Arte.to_s(v):match('sd') then
136 return v
139 return r
142 function Arte.to_s(t)
143 return string.format("%s_%s", t.quality, t.lang)
146 -- vim: set ts=4 sw=4 tw=72 expandtab: