arte.lua: Use "foo:match" instead
[libquvi-scripts.git] / share / lua / website / arte.lua
blob255006326a2972470a4def64286ae090b654d207
2 -- libquvi-scripts
3 -- Copyright (C) 2012 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 p = quvi.fetch(self.page_url)
83 local c_url = p:match('videorefFileUrl = "(.-)"')
84 or error('no match: config URL')
86 return Arte.get_lang_config(quvi.fetch(c_url, {fetch_type='config'}))
87 end
89 function Arte.get_lang_config(config)
90 local t = {}
91 for lang,url in config:gfind('<video lang="(%w+)" ref="(.-)"') do
92 table.insert(t, {lang=lang,
93 config=quvi.fetch(url, {fetch_type = 'config'})})
94 end
95 return t
96 end
98 function Arte.iter_lang_formats(lang_config, t, U)
100 local p = '<video id="(%d+)" lang="(%w+)"'
101 .. '.-<name>(.-)<'
102 .. '.-<firstThumbnailUrl>(.-)<'
103 .. '.-<dateExpiration>(.-)<'
104 .. '.-<dateVideo>(.-)<'
106 local config = lang_config.config
108 local id,lang,title,thumb,exp,date = config:match(p)
109 if not id then error("no match: media id, etc.") end
111 if lang ~= lang_config.lang then
112 error("no match: lang")
115 if Arte.has_expired(exp, U) then
116 error('error: media no longer available (expired)')
119 local urls = config:match('<urls>(.-)</urls>')
120 or error('no match: urls')
122 for q,u in urls:gfind('<url quality="(%w+)">(.-)<') do
123 -- print(q,u)
124 table.insert(t, {lang=lang, quality=q, url=u,
125 thumb=thumb, title=title, id=id})
129 function Arte.iter_formats(config, U)
130 local t = {}
131 for _,v in pairs(config) do
132 Arte.iter_lang_formats(v, t, U)
134 return t
137 function Arte.has_expired(s, U)
138 return U.to_timestamp(s) - os.time() < 0
141 function Arte.choose_best(formats) -- Whatever matches 'hd' first
142 local r
143 for _,v in pairs(formats) do
144 if Arte.to_s(v):match('hd') then
145 return v
148 return r
151 function Arte.choose_default(formats) -- Whatever matches 'sd' first
152 local r
153 for _,v in pairs(formats) do
154 if Arte.to_s(v):match('sd') then
155 return v
158 return r
161 function Arte.to_s(t)
162 return string.format("%s_%s", t.quality, t.lang)
165 -- vim: set ts=4 sw=4 tw=72 expandtab: