2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Raphaƫl Droz <raphael.droz+floss@gmail.com>
5 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
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
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.
28 local A
= require
'quvi/accepts'
29 local C
= require
'quvi/const'
31 accepts
= A
.accepts(qargs
.input_url
,
32 {"videos%.arte%.tv"}, {"/%w+/videos/"}),
33 categories
= C
.qmspc_rtmp
38 -- Parse media properties.
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
);
62 function Arte
.get_config(qargs
, L
, P
)
64 -- Collect all config data for all available (language) streams.
65 -- Return a list containing the config dictionaries, and the language
66 -- code which will be used to select the default and the best streams.
68 local p
= quvi
.fetch(qargs
.input_url
)
70 local u
= p
:match('videorefFileUrl = "(.-)"')
71 or error('no match: config URL')
73 local l
= u
:match('%.tv/(%w+)/') or error('no match: lang code')
75 local C
= require
'quvi/const'
76 local o
= { [C
.qfo_type
] = C
.qft_config
}
77 local c
= quvi
.fetch(u
, o
)
79 local x
= lxp
.lom
.parse(c
)
80 local v
= L
.find_first_tag(x
, 'videos')
83 for i
=1, #v
do -- For each language in the config.
84 if v
[i
].tag == 'video' then
85 local d
= quvi
.fetch(v
[i
].attr
['ref'], o
)
87 lang_code
= v
[i
].attr
['lang'],
90 -- Make the stream the first in the list if the language codes
91 -- match, making it the new default stream.
92 table.insert(r
, ((t
.lang_code
== l
) and 1 or #t
), t
)
99 function Arte
.opt_properties(qargs
, lang_code
)
101 -- The first stream should now be the default stream. This should
102 -- apply to the 'best' stream also, they are both the first streams
103 -- in the stream list.
105 local r
= qargs
.streams
[1]
106 qargs
.thumb_url
= r
.nostd
.thumb_url
107 qargs
.title
= r
.nostd
.title
108 qargs
.id
= r
.nostd
.id
111 function Arte
.iter_streams(config
, L
, P
, lang_code
)
112 local S
= require
'quvi/stream'
113 local U
= require
'quvi/util'
116 for _
,v
in pairs(config
) do -- For each language in the config.
117 local c
= P
.parse(v
.lang_data
)
119 local d
= L
.find_first_tag(c
, 'dateExpiration')[1]
120 if Arte
.has_expired(d
, U
) then
121 error('media no longer available (expired)')
124 local urls
= L
.find_first_tag(c
, 'urls')
127 if urls
[i
].tag == 'url' then
128 local t
= S
.stream_new(urls
[i
][1])
130 -- Save the property values that may be used later, these depend
131 -- on the language setting. Many of these are the so called
132 -- "optional media properties". The 'nostd' dictionary is used
133 -- only by this script. libquvi ignores it completely.
136 thumb_url
= L
.find_first_tag(c
, 'firstThumbnailUrl')[1],
137 title
= L
.find_first_tag(c
, 'name')[1],
138 quality
= urls
[i
].attr
['quality'],
139 lang_code
= c
.attr
['lang'],
140 id
= c
.attr
['id'] or ''
149 Arte
.ch_best(S
, r
, lang_code
)
155 function Arte
.has_expired(s
, U
)
156 return (U
.to_timestamp(s
) - os
.time()) <0
159 function Arte
.ch_best(S
, t
, lang_code
)
160 local r
= t
[1] -- Make the first one the 'best' by default.
162 for _
,v
in pairs(t
) do -- Whatever matches 'hd' first.
163 if v
.id
:match('hd') and v
.nostd
.lang_code
== lang_code
then
164 r
= S
.swap_best(r
, v
)
169 -- Return an ID for a stream.
170 function Arte
.to_id(t
)
171 return string.format("%s_%s", t
.nostd
.quality
, t
.nostd
.lang_code
)
174 -- vim: set ts=2 sw=2 tw=72 expandtab: