Split from git://repo.or.cz/quvi.git
[libquvi-scripts.git] / share / lua / website / golem.lua
blobd5d1096198f7dc8a740f6d35974fdc9db2348ba6
2 -- libquvi-scripts
3 -- Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
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
20 -- 02110-1301 USA
23 local Golem = {} -- Utility functions unique to this script
25 -- Identify the script.
26 function ident(self)
27 package.path = self.script_dir .. '/?.lua'
28 local C = require 'quvi/const'
29 local r = {}
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+/"})
35 return r
36 end
38 -- Query available formats.
39 function query_formats(self)
40 local config = Golem.get_config(self)
41 local formats = Golem.iter_formats(config)
43 local t = {}
44 for _,v in pairs(formats) do
45 table.insert(t, Golem.to_s(v))
46 end
48 table.sort(t)
49 self.formats = table.concat(t, "|")
51 return self
52 end
54 -- Parse media URL.
55 function parse(self)
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>')
63 if s then
64 self.thumbnail_url = string.format('http://video.golem.de%s', s)
65 end
67 local formats = Golem.iter_formats(config)
68 local U = require 'quvi/util'
69 self.url = {U.choose_format(self, formats,
70 Golem.choose_best,
71 Golem.choose_default,
72 Golem.to_s).url
73 or error("no match: media url")}
75 return self
76 end
79 -- Utility functions
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'})
88 end
90 function Golem.iter_formats(config)
91 local p = '<(%w+) width="(%d+)" height="(%d+)">'
92 .. '.-<filename>.-%.(%w+)<'
93 .. '.-<url>(.-)<'
94 .. '.-</teaser>'
95 local t = {}
96 for id,w,h,c,u in config:gfind(p) do
97 u = 'http://video.golem.de' .. u
98 -- print(id,w,h,c,u)
99 table.insert(t, {width=tonumber(w), height=tonumber(h),
100 container=c, url=u, id=id})
103 return t
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
111 r = v
114 return r
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
122 r = v
125 return r
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: