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"
57 local config
= Golem
.get_config(self
)
59 local _
,_
,s
= config
:find("<title>(.-)</")
60 self
.title
= s
or error("no match: media title")
62 local _
,_
,s
= config
:find('<teaser.-<url>(.-)<.-</teaser>')
64 self
.thumbnail_url
= string.format('http://video.golem.de%s', s
)
67 local formats
= Golem
.iter_formats(config
)
68 local U
= require
'quvi/util'
69 local format = U
.choose_format(self
, formats
,
73 or error("unable to choose format")
74 self
.url
= {format.url
or error("no match: media url")}
82 function Golem
.get_config(self
)
83 local _
,_
,s
= self
.page_url
:find('/[%w-_]+/(%d+)/')
84 self
.id
= s
or error("no match: media id")
86 local config_url
= "http://video.golem.de/xml/" .. self
.id
87 return quvi
.fetch(config_url
, {fetch_type
= 'config'})
90 function Golem
.iter_formats(config
)
91 local p
= '<(%w+) width="(%d+)" height="(%d+)">'
92 .. '.-<filename>.-%.(%w+)<'
96 for id
,w
,h
,c
,u
in config
:gfind(p
) do
97 u
= 'http://video.golem.de' .. u
99 table.insert(t
, {width
=tonumber(w
), height
=tonumber(h
),
100 container
=c
, url
=u
, id
=id
})
106 function Golem
.choose_best(formats
) -- Highest quality available
107 local r
= {width
=0, height
=0, url
=nil}
108 local U
= require
'quvi/util'
109 for _
,v
in pairs(formats
) do
110 if U
.is_higher_quality(v
,r
) then
117 function Golem
.choose_default(formats
)
118 local r
= {width
=0xffff, height
=0xffff, url
=nil}
119 local U
= require
'quvi/util'
120 for _
,v
in pairs(formats
) do
121 if U
.is_lower_quality(v
,r
) then
128 function Golem
.to_s(t
)
129 return string.format("%s_%s_%sp", t
.container
, t
.id
, t
.height
)
132 -- vim: set ts=4 sw=4 tw=72 expandtab: