3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2011 Raphaƫl Droz <raphael.droz+floss@gmail.com>
6 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
8 -- This library is free software; you can redistribute it and/or
9 -- modify it under the terms of the GNU Lesser General Public
10 -- License as published by the Free Software Foundation; either
11 -- version 2.1 of the License, or (at your option) any later version.
13 -- This library is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 -- Lesser General Public License for more details.
18 -- You should have received a copy of the GNU Lesser General Public
19 -- License along with this library; if not, write to the Free Software
20 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 -- NOTE: Most videos expire some (7?) days after their original broadcast
25 local Arte
= {} -- Utility functions unique to to this script.
27 -- Identify the script.
29 package
.path
= self
.script_dir
.. '/?.lua'
30 local C
= require
'quvi/const'
32 r
.domain
= "videos%.arte%.tv"
33 r
.formats
= "default|best"
34 r
.categories
= C
.proto_rtmp
35 local U
= require
'quvi/util'
36 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/%w+/videos/"})
40 -- Query available formats.
41 function query_formats(self
)
42 local config
= Arte
.get_config(self
)
43 local U
= require
'quvi/util'
44 local formats
= Arte
.iter_formats(config
, U
)
47 for _
,v
in pairs(formats
) do
48 table.insert(t
, Arte
.to_s(v
))
52 self
.formats
= table.concat(t
, "|")
60 local config
= Arte
.get_config(self
)
61 local U
= require
'quvi/util'
62 local formats
= Arte
.iter_formats(config
, U
)
63 local format = U
.choose_format(self
, formats
,
67 or error("unable to choose format")
68 self
.title
= format.title
or error('no match: title')
69 self
.id
= format.id
or error('no match: id')
70 self
.thumbnail_url
= format.thumb
or ''
71 self
.url
= {format.url
or error("no match: media url")}
80 function Arte
.get_config(self
)
81 local page
= quvi
.fetch(self
.page_url
)
82 local _
,_
,s
= page
:find('videorefFileUrl = "(.-)"')
83 local config_url
= s
or error('no match: config url')
84 local config
= quvi
.fetch(config_url
, {fetch_type
= 'config'})
85 return Arte
.get_lang_config(config
)
88 function Arte
.get_lang_config(config
)
90 for lang
,url
in config
:gfind('<video lang="(%w+)" ref="(.-)"') do
91 table.insert(t
, {lang
=lang
,
92 config
=quvi
.fetch(url
, {fetch_type
= 'config'})})
97 function Arte
.iter_lang_formats(lang_config
, t
, U
)
99 local p
= '<video id="(%d+)" lang="(%w+)"'
101 .. '.-<firstThumbnailUrl>(.-)<'
102 .. '.-<dateExpiration>(.-)<'
103 .. '.-<dateVideo>(.-)<'
105 local config
= lang_config
.config
107 local id
,lang
,title
,thumb
,exp,date = config
:match(p
)
108 if not id
then error("no match: media id, etc.") end
110 if lang
~= lang_config
.lang
then
111 error("no match: lang")
114 if Arte
.has_expired(exp, U
) then
115 error('error: media no longer available (expired)')
118 local urls
= config
:match('<urls>(.-)</urls>')
119 or error('no match: urls')
121 for q
,u
in urls
:gfind('<url quality="(%w+)">(.-)<') do
123 table.insert(t
, {lang
=lang
, quality
=q
, url
=u
,
124 thumb
=thumb
, title
=title
, id
=id
})
128 function Arte
.iter_formats(config
, U
)
130 for _
,v
in pairs(config
) do
131 Arte
.iter_lang_formats(v
, t
, U
)
136 function Arte
.has_expired(s
, U
)
137 return U
.to_timestamp(s
) - os
.time() < 0
140 function Arte
.choose_best(formats
) -- Whatever matches 'hd' first
142 for _
,v
in pairs(formats
) do
143 if Arte
.to_s(v
):find('hd') then
150 function Arte
.choose_default(formats
) -- Whatever matches 'sd' first
152 for _
,v
in pairs(formats
) do
153 if Arte
.to_s(v
):find('sd') then
160 function Arte
.to_s(t
)
161 return string.format("%s_%s", t
.quality
, t
.lang
)
164 -- vim: set ts=4 sw=4 tw=72 expandtab: