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 quvi <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 self
.title
= format.title
or error('no match: title')
68 self
.id
= format.id
or error('no match: id')
69 self
.thumbnail_url
= format.thumb
or ''
70 self
.url
= {format.url
or error("no match: media url")}
79 function Arte
.get_config(self
)
80 local page
= quvi
.fetch(self
.page_url
)
81 local _
,_
,s
= page
:find('videorefFileUrl = "(.-)"')
82 local config_url
= s
or error('no match: config url')
83 local config
= quvi
.fetch(config_url
, {fetch_type
= 'config'})
84 return Arte
.get_lang_config(config
)
87 function Arte
.get_lang_config(config
)
89 for lang
,url
in config
:gfind('<video lang="(%w+)" ref="(.-)"') do
90 table.insert(t
, {lang
=lang
,
91 config
=quvi
.fetch(url
, {fetch_type
= 'config'})})
96 function Arte
.iter_lang_formats(lang_config
, t
, U
)
98 local p
= '<video id="(%d+)" lang="(%w+)"'
100 .. '.-<firstThumbnailUrl>(.-)<'
101 .. '.-<dateExpiration>(.-)<'
102 .. '.-<dateVideo>(.-)<'
104 local config
= lang_config
.config
106 local id
,lang
,title
,thumb
,exp,date = config
:match(p
)
107 if not id
then error("no match: media id, etc.") end
109 if lang
~= lang_config
.lang
then
110 error("no match: lang")
113 if Arte
.has_expired(exp, U
) then
114 error('error: media no longer available (expired)')
117 local urls
= config
:match('<urls>(.-)</urls>')
118 or error('no match: urls')
120 for q
,u
in urls
:gfind('<url quality="(%w+)">(.-)<') do
122 table.insert(t
, {lang
=lang
, quality
=q
, url
=u
,
123 thumb
=thumb
, title
=title
, id
=id
})
127 function Arte
.iter_formats(config
, U
)
129 for _
,v
in pairs(config
) do
130 Arte
.iter_lang_formats(v
, t
, U
)
135 function Arte
.has_expired(s
, U
)
136 return U
.to_timestamp(s
) - os
.time() < 0
139 function Arte
.choose_best(formats
) -- Whatever matches 'hd' first
141 for _
,v
in pairs(formats
) do
142 if Arte
.to_s(v
):find('hd') then
149 function Arte
.choose_default(formats
) -- Whatever matches 'sd' first
151 for _
,v
in pairs(formats
) do
152 if Arte
.to_s(v
):find('sd') then
159 function Arte
.to_s(t
)
160 return string.format("%s_%s", t
.quality
, t
.lang
)
163 -- vim: set ts=4 sw=4 tw=72 expandtab: