Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / lua / website / golem.lua
Commit [+]AuthorDateLineData
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +03001
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
23local Golem = {} -- Utility functions unique to this script
24
25-- Identify the script.
26function 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
36end
37
38-- Query available formats.
39function 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
52end
53
54-- Parse media URL.
55function parse(self)
56 self.host_id = "golem"
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030057
ca3208c6 Toni Gundogdu2012-03-17 18:34:52 +020058 local c = Golem.get_config(self)
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030059
ca3208c6
TG
Toni Gundogdu2012-03-17 18:34:52 +020060 self.title = c:match("<title>(.-)</")
61 or error("no match: media title")
62
63 local s = c:match('<teaser.-<url>(.-)<.-</teaser>')
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030064 if s then
65 self.thumbnail_url = string.format('http://video.golem.de%s', s)
66 end
67
ca3208c6 Toni Gundogdu2012-03-17 18:34:52 +020068 local formats = Golem.iter_formats(c)
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030069 local U = require 'quvi/util'
8a40fa04 Toni Gundogdu2011-11-24 17:38:25 +020070 local format = U.choose_format(self, formats,
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030071 Golem.choose_best,
72 Golem.choose_default,
8a40fa04
TG
Toni Gundogdu2011-11-24 17:38:25 +020073 Golem.to_s)
74 or error("unable to choose format")
75 self.url = {format.url or error("no match: media url")}
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030076 return self
77end
78
79--
80-- Utility functions
81--
82
83function Golem.get_config(self)
ca3208c6
TG
Toni Gundogdu2012-03-17 18:34:52 +020084 self.id = self.page_url:match('/[%w-_]+/(%d+)/')
85 or error("no match: media id")
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030086
ca3208c6
TG
Toni Gundogdu2012-03-17 18:34:52 +020087 local c_url = "http://video.golem.de/xml/" .. self.id
88 return quvi.fetch(c_url, {fetch_type = 'config'})
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030089end
90
91function Golem.iter_formats(config)
92 local p = '<(%w+) width="(%d+)" height="(%d+)">'
93 .. '.-<filename>.-%.(%w+)<'
94 .. '.-<url>(.-)<'
95 .. '.-</teaser>'
96 local t = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +020097 for id,w,h,c,u in config:gmatch(p) do
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030098 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
105end
106
107function 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
116end
117
118function 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
127end
128
129function Golem.to_s(t)
130 return string.format("%s_%s_%sp", t.container, t.id, t.height)
131end
132
133-- vim: set ts=4 sw=4 tw=72 expandtab: