media/*.lua: Do not specify "categories"
[libquvi-scripts.git] / share / media / arte.lua
blobf61fe4fada218c5231e54bab3da976d106ca41d9
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 r = {
30 accepts = A.accepts(qargs.input_url,
31 {"videos%.arte%.tv"}, {"/%w+/videos/"})
33 return r
34 end
36 -- Parse media properties.
37 function parse(qargs)
38 local L = require 'quvi/lxph'
39 local P = require 'lxp.lom'
41 -- Config data ('c') contains config data for each available language.
42 -- Each language consists of >0 media streams, e.g. 'hd', 'sd'.
44 local c,lang_code = Arte.get_config(qargs, L, P)
45 qargs.streams,S = Arte.iter_streams(c, L, P, lang_code)
47 -- Many of the optional properties depend on the language setting.
48 -- e.g. title, even the media ID. Have these parsed _after_ the
49 -- streams have been parsed.
51 Arte.opt_properties(qargs, lang_code);
53 return qargs
54 end
57 -- Utility functions
60 function Arte.get_config(qargs, L, P)
62 -- Collect all config data for all available (language) streams.
63 -- Return a list containing the config dictionaries, and the language
64 -- code which will be used to select the default and the best streams.
66 local p = quvi.fetch(qargs.input_url)
68 local u = p:match('videorefFileUrl = "(.-)"')
69 or error('no match: config URL')
71 local l = u:match('%.tv/(%w+)/') or error('no match: lang code')
73 local C = require 'quvi/const'
74 local o = { [C.qfo_type] = C.qft_config }
75 local c = quvi.fetch(u, o)
77 local x = lxp.lom.parse(c)
78 local v = L.find_first_tag(x, 'videos')
79 local r = {}
81 for i=1, #v do -- For each language in the config.
82 if v[i].tag == 'video' then
83 local d = quvi.fetch(v[i].attr['ref'], o)
84 local t = {
85 lang_code = v[i].attr['lang'],
86 lang_data = d
88 -- Make the stream the first in the list if the language codes
89 -- match, making it the new default stream.
90 table.insert(r, ((t.lang_code == l) and 1 or #t), t)
91 end
92 end
94 return r, l
95 end
97 function Arte.opt_properties(qargs, lang_code)
99 -- The first stream should now be the default stream. This should
100 -- apply to the 'best' stream also, they are both the first streams
101 -- in the stream list.
103 local r = qargs.streams[1]
104 qargs.thumb_url = r.nostd.thumb_url
105 qargs.title = r.nostd.title
106 qargs.id = r.nostd.id
109 function Arte.iter_streams(config, L, P, lang_code)
110 local S = require 'quvi/stream'
111 local U = require 'quvi/util'
112 local r = {}
114 for _,v in pairs(config) do -- For each language in the config.
115 local c = P.parse(v.lang_data)
117 local d = L.find_first_tag(c, 'dateExpiration')[1]
118 if Arte.has_expired(d, U) then
119 error('media no longer available (expired)')
122 local urls = L.find_first_tag(c, 'urls')
124 for i=1, #urls do
125 if urls[i].tag == 'url' then
126 local t = S.stream_new(urls[i][1])
128 -- Save the property values that may be used later, these depend
129 -- on the language setting. Many of these are the so called
130 -- "optional media properties". The 'nostd' dictionary is used
131 -- only by this script. libquvi ignores it completely.
133 t.nostd = {
134 thumb_url = L.find_first_tag(c, 'firstThumbnailUrl')[1],
135 title = L.find_first_tag(c, 'name')[1],
136 quality = urls[i].attr['quality'],
137 lang_code = c.attr['lang'],
138 id = c.attr['id'] or ''
140 t.id = Arte.to_id(t)
141 table.insert(r, t)
146 if #r >1 then
147 Arte.ch_best(S, r, lang_code)
150 return r,S
153 function Arte.has_expired(s, U)
154 return (U.to_timestamp(s) - os.time()) <0
157 function Arte.ch_best(S, t, lang_code)
158 local r = t[1] -- Make the first one the 'best' by default.
159 r.flags.best = true
160 for _,v in pairs(t) do -- Whatever matches 'hd' first.
161 if v.id:match('hd') and v.nostd.lang_code == lang_code then
162 r = S.swap_best(r, v)
167 -- Return an ID for a stream.
168 function Arte.to_id(t)
169 return string.format("%s_%s", t.nostd.quality, t.nostd.lang_code)
172 -- vim: set ts=2 sw=2 tw=72 expandtab: