2 -- Copyright (C) 2012-2013 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.sourceforge.net/>.
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program 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
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
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.
29 can_parse_url
= Arte
.can_parse_url(qargs
),
30 domains
= table.concat({'videos.arte.tv'}, ',')
34 -- Parse media properties.
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
);
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/')
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')
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
93 lang_code
= v
[i
].attr
['lang'],
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
)
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 T
= require
'quvi/time'
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
, T
) then
127 error('media no longer available (expired)')
130 local urls
= L
.find_first_tag(c
, 'urls')
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.
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 ''
155 Arte
.ch_best(S
, r
, lang_code
)
161 function Arte
.has_expired(s
, T
)
162 return (T
.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.
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: