Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / lua / website / golem.lua
1
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.
11 --
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.
16 --
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
21 --
22
23 local Golem = {} -- Utility functions unique to this script
24
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
37
38 -- Query available formats.
39 function query_formats(self)
40 local config = Golem.get_config(self)
41 local formats = Golem.iter_formats(config)
42
43 local t = {}
44 for _,v in pairs(formats) do
45 table.insert(t, Golem.to_s(v))
46 end
47
48 table.sort(t)
49 self.formats = table.concat(t, "|")
50
51 return self
52 end
53
54 -- Parse media URL.
55 function parse(self)
56 self.host_id = "golem"
57
58 local c = Golem.get_config(self)
59
60 self.title = c:match("<title>(.-)</")
61 or error("no match: media title")
62
63 local s = c:match('<teaser.-<url>(.-)<.-</teaser>')
64 if s then
65 self.thumbnail_url = string.format('http://video.golem.de%s', s)
66 end
67
68 local formats = Golem.iter_formats(c)
69 local U = require 'quvi/util'
70 local format = U.choose_format(self, formats,
71 Golem.choose_best,
72 Golem.choose_default,
73 Golem.to_s)
74 or error("unable to choose format")
75 self.url = {format.url or error("no match: media url")}
76 return self
77 end
78
79 --
80 -- Utility functions
81 --
82
83 function Golem.get_config(self)
84 self.id = self.page_url:match('/[%w-_]+/(%d+)/')
85 or error("no match: media id")
86
87 local c_url = "http://video.golem.de/xml/" .. self.id
88 return quvi.fetch(c_url, {fetch_type = 'config'})
89 end
90
91 function Golem.iter_formats(config)
92 local p = '<(%w+) width="(%d+)" height="(%d+)">'
93 .. '.-<filename>.-%.(%w+)<'
94 .. '.-<url>(.-)<'
95 .. '.-</teaser>'
96 local t = {}
97 for id,w,h,c,u in config:gmatch(p) do
98 u = 'http://video.golem.de' .. u
99 -- print(id,w,h,c,u)
100 table.insert(t, {width=tonumber(w), height=tonumber(h),
101 container=c, url=u, id=id})
102 end
103
104 return t
105 end
106
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
112 r = v
113 end
114 end
115 return r
116 end
117
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
123 r = v
124 end
125 end
126 return r
127 end
128
129 function Golem.to_s(t)
130 return string.format("%s_%s_%sp", t.container, t.id, t.height)
131 end
132
133 -- vim: set ts=4 sw=4 tw=72 expandtab: