ident: Remove r.domain
[libquvi-scripts.git] / share / lua / media / arte.lua
blobb22bd16674b4f1856cc0b25693c1e3fa7fd471b4
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 C = require 'quvi/const'
29 local r = {}
30 r.formats = "default|best"
31 r.categories = C.proto_rtmp
32 local U = require 'quvi/util'
33 r.handles = U.handles(self.page_url, {r.domain}, {"/%w+/videos/"})
34 return r
35 end
37 -- Query available formats.
38 function query_formats(self)
39 local config = Arte.get_config(self)
40 local U = require 'quvi/util'
41 local formats = Arte.iter_formats(config, U)
43 local t = {}
44 for _,v in pairs(formats) do
45 table.insert(t, Arte.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 = 'arte'
57 local config = Arte.get_config(self)
58 local U = require 'quvi/util'
59 local formats = Arte.iter_formats(config, U)
60 local format = U.choose_format(self, formats,
61 Arte.choose_best,
62 Arte.choose_default,
63 Arte.to_s)
64 or error("unable to choose format")
65 self.title = format.title or error('no match: title')
66 self.id = format.id or error('no match: id')
67 self.thumbnail_url = format.thumb or ''
68 self.url = {format.url or error("no match: media url")}
70 return self
71 end
74 -- Utility functions
77 function Arte.get_config(self)
78 local p = quvi.fetch(self.page_url)
80 local c_url = p:match('videorefFileUrl = "(.-)"')
81 or error('no match: config URL')
83 return Arte.get_lang_config(quvi.fetch(c_url, {fetch_type='config'}))
84 end
86 function Arte.get_lang_config(config)
87 local t = {}
88 for lang,url in config:gmatch('<video lang="(%w+)" ref="(.-)"') do
89 table.insert(t, {lang=lang,
90 config=quvi.fetch(url, {fetch_type = 'config'})})
91 end
92 return t
93 end
95 function Arte.iter_lang_formats(lang_config, t, U)
97 local p = '<video id="(%d+)" lang="(%w+)"'
98 .. '.-<name>(.-)<'
99 .. '.-<firstThumbnailUrl>(.-)<'
100 .. '.-<dateExpiration>(.-)<'
101 .. '.-<dateVideo>(.-)<'
103 local config = lang_config.config
105 local id,lang,title,thumb,exp,date = config:match(p)
106 if not id then error("no match: media id, etc.") end
108 if lang ~= lang_config.lang then
109 error("no match: lang")
112 if Arte.has_expired(exp, U) then
113 error('error: media no longer available (expired)')
116 local urls = config:match('<urls>(.-)</urls>')
117 or error('no match: urls')
119 for q,u in urls:gmatch('<url quality="(%w+)">(.-)<') do
120 -- print(q,u)
121 table.insert(t, {lang=lang, quality=q, url=u,
122 thumb=thumb, title=title, id=id})
126 function Arte.iter_formats(config, U)
127 local t = {}
128 for _,v in pairs(config) do
129 Arte.iter_lang_formats(v, t, U)
131 return t
134 function Arte.has_expired(s, U)
135 return U.to_timestamp(s) - os.time() < 0
138 function Arte.choose_best(formats) -- Whatever matches 'hd' first
139 local r
140 for _,v in pairs(formats) do
141 if Arte.to_s(v):match('hd') then
142 return v
145 return r
148 function Arte.choose_default(formats) -- Whatever matches 'sd' first
149 local r
150 for _,v in pairs(formats) do
151 if Arte.to_s(v):match('sd') then
152 return v
155 return r
158 function Arte.to_s(t)
159 return string.format("%s_%s", t.quality, t.lang)
162 -- vim: set ts=4 sw=4 tw=72 expandtab: