Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / arte.lua
bloba7696db94c122cd03076588c923c037f9f6e1a81
2 -- quvi
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 quvi <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 self.title = format.title or error('no match: title')
68 self.id = format.id or error('no match: id')
69 self.thumbnail_url = format.thumb or ''
70 self.url = {format.url or error("no match: media url")}
72 return self
73 end
76 -- Utility functions
79 function Arte.get_config(self)
80 local page = quvi.fetch(self.page_url)
81 local _,_,s = page:find('videorefFileUrl = "(.-)"')
82 local config_url = s or error('no match: config url')
83 local config = quvi.fetch(config_url, {fetch_type = 'config'})
84 return Arte.get_lang_config(config)
85 end
87 function Arte.get_lang_config(config)
88 local t = {}
89 for lang,url in config:gfind('<video lang="(%w+)" ref="(.-)"') do
90 table.insert(t, {lang=lang,
91 config=quvi.fetch(url, {fetch_type = 'config'})})
92 end
93 return t
94 end
96 function Arte.iter_lang_formats(lang_config, t, U)
98 local p = '<video id="(%d+)" lang="(%w+)"'
99 .. '.-<name>(.-)<'
100 .. '.-<firstThumbnailUrl>(.-)<'
101 .. '.-<dateExpiration>(.-)<'
102 .. '.-<dateVideo>(.-)<'
104 local config = lang_config.config
106 local id,lang,title,thumb,exp,date = config:match(p)
107 if not id then error("no match: media id, etc.") end
109 if lang ~= lang_config.lang then
110 error("no match: lang")
113 if Arte.has_expired(exp, U) then
114 error('error: media no longer available (expired)')
117 local urls = config:match('<urls>(.-)</urls>')
118 or error('no match: urls')
120 for q,u in urls:gfind('<url quality="(%w+)">(.-)<') do
121 -- print(q,u)
122 table.insert(t, {lang=lang, quality=q, url=u,
123 thumb=thumb, title=title, id=id})
127 function Arte.iter_formats(config, U)
128 local t = {}
129 for _,v in pairs(config) do
130 Arte.iter_lang_formats(v, t, U)
132 return t
135 function Arte.has_expired(s, U)
136 return U.to_timestamp(s) - os.time() < 0
139 function Arte.choose_best(formats) -- Whatever matches 'hd' first
140 local r
141 for _,v in pairs(formats) do
142 if Arte.to_s(v):find('hd') then
143 return v
146 return r
149 function Arte.choose_default(formats) -- Whatever matches 'sd' first
150 local r
151 for _,v in pairs(formats) do
152 if Arte.to_s(v):find('sd') then
153 return v
156 return r
159 function Arte.to_s(t)
160 return string.format("%s_%s", t.quality, t.lang)
163 -- vim: set ts=4 sw=4 tw=72 expandtab: