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 C
= require
'quvi/const'
30 r
.formats
= "default|best"
31 r
.categories
= C
.proto_rtmp
32 local U
= require
'quvi/util'
33 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/%w+/videos/"})
37 -- Query available formats.
38 function query_formats(self
)
39 local config
= Arte
.get_config(self
)
40 local U
= require
'quvi/util'
41 local formats
= Arte
.iter_formats(config
, U
)
44 for _
,v
in pairs(formats
) do
45 table.insert(t
, Arte
.to_s(v
))
49 self
.formats
= table.concat(t
, "|")
57 local config
= Arte
.get_config(self
)
58 local U
= require
'quvi/util'
59 local formats
= Arte
.iter_formats(config
, U
)
60 local format = U
.choose_format(self
, formats
,
64 or error("unable to choose format")
65 self
.title
= format.title
or error('no match: title')
66 self
.id
= format.id
or error('no match: id')
67 self
.thumbnail_url
= format.thumb
or ''
68 self
.url
= {format.url
or error("no match: media url")}
77 function Arte
.get_config(self
)
78 local p
= quvi
.fetch(self
.page_url
)
80 local c_url
= p
:match('videorefFileUrl = "(.-)"')
81 or error('no match: config URL')
83 return Arte
.get_lang_config(quvi
.fetch(c_url
, {fetch_type
='config'}))
86 function Arte
.get_lang_config(config
)
88 for lang
,url
in config
:gmatch('<video lang="(%w+)" ref="(.-)"') do
89 table.insert(t
, {lang
=lang
,
90 config
=quvi
.fetch(url
, {fetch_type
= 'config'})})
95 function Arte
.iter_lang_formats(lang_config
, t
, U
)
97 local p
= '<video id="(%d+)" lang="(%w+)"'
99 .. '.-<firstThumbnailUrl>(.-)<'
100 .. '.-<dateExpiration>(.-)<'
101 .. '.-<dateVideo>(.-)<'
103 local config
= lang_config
.config
105 local id
,lang
,title
,thumb
,exp,date = config
:match(p
)
106 if not id
then error("no match: media id, etc.") end
108 if lang
~= lang_config
.lang
then
109 error("no match: lang")
112 if Arte
.has_expired(exp, U
) then
113 error('error: media no longer available (expired)')
116 local urls
= config
:match('<urls>(.-)</urls>')
117 or error('no match: urls')
119 for q
,u
in urls
:gmatch('<url quality="(%w+)">(.-)<') do
121 table.insert(t
, {lang
=lang
, quality
=q
, url
=u
,
122 thumb
=thumb
, title
=title
, id
=id
})
126 function Arte
.iter_formats(config
, U
)
128 for _
,v
in pairs(config
) do
129 Arte
.iter_lang_formats(v
, t
, U
)
134 function Arte
.has_expired(s
, U
)
135 return U
.to_timestamp(s
) - os
.time() < 0
138 function Arte
.choose_best(formats
) -- Whatever matches 'hd' first
140 for _
,v
in pairs(formats
) do
141 if Arte
.to_s(v
):match('hd') then
148 function Arte
.choose_default(formats
) -- Whatever matches 'sd' first
150 for _
,v
in pairs(formats
) do
151 if Arte
.to_s(v
):match('sd') then
158 function Arte
.to_s(t
)
159 return string.format("%s_%s", t
.quality
, t
.lang
)
162 -- vim: set ts=4 sw=4 tw=72 expandtab: