FIX: Potential "attempt to index ... (a nil value)"
[libquvi-scripts.git] / share / lua / website / arte.lua
blob100d9e92f643a3e8a2efe56ca8bdef2c012e1d69
2 -- libquvi-scripts
3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2011 Raphaƫl Droz <raphael.droz+floss@gmail.com>
5 --
6 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
7 --
8 -- This library is free software; you can redistribute it and/or
9 -- modify it under the terms of the GNU Lesser General Public
10 -- License as published by the Free Software Foundation; either
11 -- version 2.1 of the License, or (at your option) any later version.
13 -- This library is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 -- Lesser General Public License for more details.
18 -- You should have received a copy of the GNU Lesser General Public
19 -- License along with this library; if not, write to the Free Software
20 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 -- 02110-1301 USA
23 -- NOTE: Most videos expire some (7?) days after their original broadcast
25 local Arte = {} -- Utility functions unique to to this script.
27 -- Identify the script.
28 function ident(self)
29 package.path = self.script_dir .. '/?.lua'
30 local C = require 'quvi/const'
31 local r = {}
32 r.domain = "videos%.arte%.tv"
33 r.formats = "default|best"
34 r.categories = C.proto_rtmp
35 local U = require 'quvi/util'
36 r.handles = U.handles(self.page_url, {r.domain}, {"/%w+/videos/"})
37 return r
38 end
40 -- Query available formats.
41 function query_formats(self)
42 local config = Arte.get_config(self)
43 local U = require 'quvi/util'
44 local formats = Arte.iter_formats(config, U)
46 local t = {}
47 for _,v in pairs(formats) do
48 table.insert(t, Arte.to_s(v))
49 end
51 table.sort(t)
52 self.formats = table.concat(t, "|")
54 return self
55 end
57 -- Parse media URL.
58 function parse(self)
59 self.host_id = 'arte'
60 local config = Arte.get_config(self)
61 local U = require 'quvi/util'
62 local formats = Arte.iter_formats(config, U)
63 local format = U.choose_format(self, formats,
64 Arte.choose_best,
65 Arte.choose_default,
66 Arte.to_s)
67 or error("unable to choose format")
68 self.title = format.title or error('no match: title')
69 self.id = format.id or error('no match: id')
70 self.thumbnail_url = format.thumb or ''
71 self.url = {format.url or error("no match: media url")}
73 return self
74 end
77 -- Utility functions
80 function Arte.get_config(self)
81 local page = quvi.fetch(self.page_url)
82 local _,_,s = page:find('videorefFileUrl = "(.-)"')
83 local config_url = s or error('no match: config url')
84 local config = quvi.fetch(config_url, {fetch_type = 'config'})
85 return Arte.get_lang_config(config)
86 end
88 function Arte.get_lang_config(config)
89 local t = {}
90 for lang,url in config:gfind('<video lang="(%w+)" ref="(.-)"') do
91 table.insert(t, {lang=lang,
92 config=quvi.fetch(url, {fetch_type = 'config'})})
93 end
94 return t
95 end
97 function Arte.iter_lang_formats(lang_config, t, U)
99 local p = '<video id="(%d+)" lang="(%w+)"'
100 .. '.-<name>(.-)<'
101 .. '.-<firstThumbnailUrl>(.-)<'
102 .. '.-<dateExpiration>(.-)<'
103 .. '.-<dateVideo>(.-)<'
105 local config = lang_config.config
107 local id,lang,title,thumb,exp,date = config:match(p)
108 if not id then error("no match: media id, etc.") end
110 if lang ~= lang_config.lang then
111 error("no match: lang")
114 if Arte.has_expired(exp, U) then
115 error('error: media no longer available (expired)')
118 local urls = config:match('<urls>(.-)</urls>')
119 or error('no match: urls')
121 for q,u in urls:gfind('<url quality="(%w+)">(.-)<') do
122 -- print(q,u)
123 table.insert(t, {lang=lang, quality=q, url=u,
124 thumb=thumb, title=title, id=id})
128 function Arte.iter_formats(config, U)
129 local t = {}
130 for _,v in pairs(config) do
131 Arte.iter_lang_formats(v, t, U)
133 return t
136 function Arte.has_expired(s, U)
137 return U.to_timestamp(s) - os.time() < 0
140 function Arte.choose_best(formats) -- Whatever matches 'hd' first
141 local r
142 for _,v in pairs(formats) do
143 if Arte.to_s(v):find('hd') then
144 return v
147 return r
150 function Arte.choose_default(formats) -- Whatever matches 'sd' first
151 local r
152 for _,v in pairs(formats) do
153 if Arte.to_s(v):find('sd') then
154 return v
157 return r
160 function Arte.to_s(t)
161 return string.format("%s_%s", t.quality, t.lang)
164 -- vim: set ts=4 sw=4 tw=72 expandtab: