media/arte.lua: Arte.get_config: Remove unused parameter
[libquvi-scripts.git] / share / media / arte.lua
blob9ab1981a0355ff4c8f82f4bcf408c8d12ab35e43
1 -- libquvi-scripts
2 -- Copyright (C) 2012-2013 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 return {
29 can_parse_url = Arte.can_parse_url(qargs),
30 domains = table.concat({'videos.arte.tv'}, ',')
32 end
34 -- Parse media properties.
35 function parse(qargs)
36 local L = require 'quvi/lxph'
37 local P = require 'lxp.lom'
39 -- Config data ('c') contains config data for each available language.
40 -- Each language consists of >0 media streams, e.g. 'hd', 'sd'.
42 local c,lang_code = Arte.get_config(qargs, L)
43 qargs.streams,S = Arte.iter_streams(c, L, P, lang_code)
45 -- Many of the optional properties depend on the language setting.
46 -- e.g. title, even the media ID. Have these parsed _after_ the
47 -- streams have been parsed.
49 Arte.opt_properties(qargs, lang_code);
51 return qargs
52 end
55 -- Utility functions
58 function Arte.can_parse_url(qargs)
59 local U = require 'socket.url'
60 local t = U.parse(qargs.input_url)
61 if t and t.scheme and t.scheme:lower():match('^http$')
62 and t.host and t.host:lower():match('^videos%.arte%.tv$')
63 and t.path and t.path:lower():match('^/%w+/videos/')
64 then
65 return true
66 else
67 return false
68 end
69 end
71 function Arte.get_config(qargs, L)
73 -- Collect all config data for all available (language) streams.
74 -- Return a list containing the config dictionaries, and the language
75 -- code which will be used to select the default and the best streams.
77 local p = quvi.http.fetch(qargs.input_url).data
79 local u = p:match('videorefFileUrl = "(.-)"')
80 or error('no match: config URL')
82 local l = u:match('%.tv/(%w+)/') or error('no match: lang code')
84 local c = quvi.http.fetch(u).data
85 local x = lxp.lom.parse(c)
86 local v = L.find_first_tag(x, 'videos')
87 local r = {}
89 for i=1, #v do -- For each language in the config.
90 if v[i].tag == 'video' then
91 local d = quvi.http.fetch(v[i].attr['ref'], o).data
92 local t = {
93 lang_code = v[i].attr['lang'],
94 lang_data = d
96 -- Make the stream the first in the list if the language codes
97 -- match, making it the new default stream.
98 table.insert(r, ((t.lang_code == l) and 1 or #t), t)
99 end
102 return r, l
105 function Arte.opt_properties(qargs, lang_code)
107 -- The first stream should now be the default stream. This should
108 -- apply to the 'best' stream also, they are both the first streams
109 -- in the stream list.
111 local r = qargs.streams[1]
112 qargs.thumb_url = r.nostd.thumb_url
113 qargs.title = r.nostd.title
114 qargs.id = r.nostd.id
117 function Arte.iter_streams(config, L, P, lang_code)
118 local S = require 'quvi/stream'
119 local U = require 'quvi/util'
120 local r = {}
122 for _,v in pairs(config) do -- For each language in the config.
123 local c = P.parse(v.lang_data)
125 local d = L.find_first_tag(c, 'dateExpiration')[1]
126 if Arte.has_expired(d, U) then
127 error('media no longer available (expired)')
130 local urls = L.find_first_tag(c, 'urls')
132 for i=1, #urls do
133 if urls[i].tag == 'url' then
134 local t = S.stream_new(urls[i][1])
136 -- Save the property values that may be used later, these depend
137 -- on the language setting. Many of these are the so called
138 -- "optional media properties". The 'nostd' dictionary is used
139 -- only by this script. libquvi ignores it completely.
141 t.nostd = {
142 thumb_url = L.find_first_tag(c, 'firstThumbnailUrl')[1],
143 title = L.find_first_tag(c, 'name')[1],
144 quality = urls[i].attr['quality'],
145 lang_code = c.attr['lang'],
146 id = c.attr['id'] or ''
148 t.id = Arte.to_id(t)
149 table.insert(r, t)
154 if #r >1 then
155 Arte.ch_best(S, r, lang_code)
158 return r,S
161 function Arte.has_expired(s, U)
162 return (U.to_timestamp(s) - os.time()) <0
165 function Arte.ch_best(S, t, lang_code)
166 local r = t[1] -- Make the first one the 'best' by default.
167 r.flags.best = true
168 for _,v in pairs(t) do -- Whatever matches 'hd' first.
169 if v.id:match('hd') and v.nostd.lang_code == lang_code then
170 r = S.swap_best(r, v)
175 -- Return an ID for a stream.
176 function Arte.to_id(t)
177 return string.format("%s_%s", t.nostd.quality, t.nostd.lang_code)
180 -- vim: set ts=2 sw=2 tw=72 expandtab: