3 -- Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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
23 local Golem
= {} -- Utility functions unique to this script
25 -- Identify the script.
27 package
.path
= self
.script_dir
.. '/?.lua'
28 local C
= require
'quvi/const'
30 r
.domain
= "video%.golem%.de"
31 r
.formats
= "default|best"
32 r
.categories
= C
.proto_http
33 local U
= require
'quvi/util'
34 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/[%w-_]+/%d+/"})
38 -- Query available formats.
39 function query_formats(self
)
40 local config
= Golem
.get_config(self
)
41 local formats
= Golem
.iter_formats(config
)
44 for _
,v
in pairs(formats
) do
45 table.insert(t
, Golem
.to_s(v
))
49 self
.formats
= table.concat(t
, "|")
56 self
.host_id
= "golem"
58 local c
= Golem
.get_config(self
)
60 self
.title
= c
:match("<title>(.-)</")
61 or error("no match: media title")
63 local s
= c
:match('<teaser.-<url>(.-)<.-</teaser>')
65 self
.thumbnail_url
= string.format('http://video.golem.de%s', s
)
68 local formats
= Golem
.iter_formats(c
)
69 local U
= require
'quvi/util'
70 local format = U
.choose_format(self
, formats
,
74 or error("unable to choose format")
75 self
.url
= {format.url
or error("no match: media url")}
83 function Golem
.get_config(self
)
84 self
.id
= self
.page_url
:match('/[%w-_]+/(%d+)/')
85 or error("no match: media id")
87 local c_url
= "http://video.golem.de/xml/" .. self
.id
88 return quvi
.fetch(c_url
, {fetch_type
= 'config'})
91 function Golem
.iter_formats(config
)
92 local p
= '<(%w+) width="(%d+)" height="(%d+)">'
93 .. '.-<filename>.-%.(%w+)<'
97 for id
,w
,h
,c
,u
in config
:gmatch(p
) do
98 u
= 'http://video.golem.de' .. u
100 table.insert(t
, {width
=tonumber(w
), height
=tonumber(h
),
101 container
=c
, url
=u
, id
=id
})
107 function Golem
.choose_best(formats
) -- Highest quality available
108 local r
= {width
=0, height
=0, url
=nil}
109 local U
= require
'quvi/util'
110 for _
,v
in pairs(formats
) do
111 if U
.is_higher_quality(v
,r
) then
118 function Golem
.choose_default(formats
)
119 local r
= {width
=0xffff, height
=0xffff, url
=nil}
120 local U
= require
'quvi/util'
121 for _
,v
in pairs(formats
) do
122 if U
.is_lower_quality(v
,r
) then
129 function Golem
.to_s(t
)
130 return string.format("%s_%s_%sp", t
.container
, t
.id
, t
.height
)
133 -- vim: set ts=4 sw=4 tw=72 expandtab: