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
.proto_rtmp
41 local config
= Arte
.get_config(self
)
42 local U
= require
'quvi/util'
43 local formats
= Arte
.iter_formats(config
, U
)
44 local format = U
.choose_format(self
, formats
,
48 or error("unable to choose format")
49 self
.title
= format.title
or error('no match: title')
50 self
.id
= format.id
or error('no match: id')
51 self
.thumbnail_url
= format.thumb
or ''
52 self
.url
= {format.url
or error("no match: media url")}
61 function Arte
.get_config(self
)
62 local p
= quvi
.fetch(self
.page_url
)
64 local c_url
= p
:match('videorefFileUrl = "(.-)"')
65 or error('no match: config URL')
67 return Arte
.get_lang_config(quvi
.fetch(c_url
, {fetch_type
='config'}))
70 function Arte
.get_lang_config(config
)
72 for lang
,url
in config
:gmatch('<video lang="(%w+)" ref="(.-)"') do
73 table.insert(t
, {lang
=lang
,
74 config
=quvi
.fetch(url
, {fetch_type
= 'config'})})
79 function Arte
.iter_lang_formats(lang_config
, t
, U
)
81 local p
= '<video id="(%d+)" lang="(%w+)"'
83 .. '.-<firstThumbnailUrl>(.-)<'
84 .. '.-<dateExpiration>(.-)<'
85 .. '.-<dateVideo>(.-)<'
87 local config
= lang_config
.config
89 local id
,lang
,title
,thumb
,exp,date = config
:match(p
)
90 if not id
then error("no match: media id, etc.") end
92 if lang
~= lang_config
.lang
then
93 error("no match: lang")
96 if Arte
.has_expired(exp, U
) then
97 error('error: media no longer available (expired)')
100 local urls
= config
:match('<urls>(.-)</urls>')
101 or error('no match: urls')
103 for q
,u
in urls
:gmatch('<url quality="(%w+)">(.-)<') do
105 table.insert(t
, {lang
=lang
, quality
=q
, url
=u
,
106 thumb
=thumb
, title
=title
, id
=id
})
110 function Arte
.iter_formats(config
, U
)
112 for _
,v
in pairs(config
) do
113 Arte
.iter_lang_formats(v
, t
, U
)
118 function Arte
.has_expired(s
, U
)
119 return U
.to_timestamp(s
) - os
.time() < 0
122 function Arte
.choose_best(formats
) -- Whatever matches 'hd' first
124 for _
,v
in pairs(formats
) do
125 if Arte
.to_s(v
):match('hd') then
132 function Arte
.choose_default(formats
) -- Whatever matches 'sd' first
134 for _
,v
in pairs(formats
) do
135 if Arte
.to_s(v
):match('sd') then
142 function Arte
.to_s(t
)
143 return string.format("%s_%s", t
.quality
, t
.lang
)
146 -- vim: set ts=4 sw=4 tw=72 expandtab: