parse: Rewrite for qargs.streams
[libquvi-scripts.git] / share / lua / media / arte.lua
blobef5d193b8e6a01734b097798f124b506255cc6c4
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 C = require 'quvi/const'
30 local r = {
31 accepts = A.accepts(qargs.input_url,
32 {"videos%.arte%.tv"}, {"/%w+/videos/"}),
33 categories = C.proto_rtmp
35 return r
36 end
38 -- Parse media properties.
39 function parse(qargs)
40 local L = require 'quvi/lxph'
41 local P = require 'lxp.lom'
43 -- Config data ('c') contains config data for each available language.
44 -- Each language consists of >0 media streams, e.g. 'hd', 'sd'.
46 local c,lang_code = Arte.get_config(qargs, L, P)
47 qargs.streams,S = Arte.iter_streams(c, L, P, lang_code)
49 -- Many of the optional properties depend on the language setting.
50 -- e.g. title, even the media ID. Have these parsed _after_ the
51 -- streams have been parsed.
53 Arte.opt_properties(qargs, lang_code);
55 return qargs
56 end
59 -- Utility functions
62 function Arte.get_config(self)
63 local p = quvi.fetch(self.page_url)
65 local c_url = p:match('videorefFileUrl = "(.-)"')
66 or error('no match: config URL')
68 return Arte.get_lang_config(quvi.fetch(c_url, {fetch_type='config'}))
69 end
71 function Arte.get_lang_config(config)
72 local t = {}
73 for lang,url in config:gmatch('<video lang="(%w+)" ref="(.-)"') do
74 table.insert(t, {lang=lang,
75 config=quvi.fetch(url, {fetch_type = 'config'})})
76 end
77 return t
78 end
80 function Arte.iter_lang_formats(lang_config, t, U)
82 local p = '<video id="(%d+)" lang="(%w+)"'
83 .. '.-<name>(.-)<'
84 .. '.-<firstThumbnailUrl>(.-)<'
85 .. '.-<dateExpiration>(.-)<'
86 .. '.-<dateVideo>(.-)<'
88 local config = lang_config.config
90 local id,lang,title,thumb,exp,date = config:match(p)
91 if not id then error("no match: media id, etc.") end
93 if lang ~= lang_config.lang then
94 error("no match: lang")
95 end
97 if Arte.has_expired(exp, U) then
98 error('error: media no longer available (expired)')
99 end
101 local urls = config:match('<urls>(.-)</urls>')
102 or error('no match: urls')
104 for q,u in urls:gmatch('<url quality="(%w+)">(.-)<') do
105 -- print(q,u)
106 table.insert(t, {lang=lang, quality=q, url=u,
107 thumb=thumb, title=title, id=id})
111 function Arte.iter_formats(config, U)
112 local t = {}
113 for _,v in pairs(config) do
114 Arte.iter_lang_formats(v, t, U)
116 return t
119 function Arte.has_expired(s, U)
120 return U.to_timestamp(s) - os.time() < 0
123 function Arte.choose_best(formats) -- Whatever matches 'hd' first
124 local r
125 for _,v in pairs(formats) do
126 if Arte.to_s(v):match('hd') then
127 return v
130 return r
133 function Arte.choose_default(formats) -- Whatever matches 'sd' first
134 local r
135 for _,v in pairs(formats) do
136 if Arte.to_s(v):match('sd') then
137 return v
140 return r
143 function Arte.to_s(t)
144 return string.format("%s_%s", t.quality, t.lang)
147 -- vim: set ts=4 sw=4 tw=72 expandtab: