media/spiegel.lua: Relicense under AGPLv3+
[libquvi-scripts.git] / share / media / spiegel.lua
blobc5a8e81d37cc82235e496fee6cd72a8f665f89a1
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2011,2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
22 -- NOTE: Some streams (e.g. 3gp) do not appear to be available (404) even
23 -- if they are listed in the config XML.
26 local Spiegel = {} -- Utility functions unique to this script
28 -- Identify the script.
29 function ident(self)
30 package.path = self.script_dir .. '/?.lua'
31 local C = require 'quvi/const'
32 local r = {}
33 r.domain = "spiegel%.de"
34 r.formats = "default|best"
35 r.categories = C.proto_http
36 local U = require 'quvi/util'
37 r.handles = U.handles(self.page_url, {r.domain}, {"/video/"})
38 return r
39 end
41 -- Query available formats.
42 function query_formats(self)
43 Spiegel.get_media_id(self)
45 local config = Spiegel.get_config(self)
46 local formats = Spiegel.iter_formats(config)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Spiegel.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "¦")
56 return self
57 end
59 -- Parse media URL.
60 function parse(self)
61 self.host_id = "spiegel"
63 local p = quvi.fetch(self.page_url)
65 self.title = p:match('"spVideoTitle">(.-)<')
66 or error('no match: media title')
68 self.thumbnail_url = p:match('"og:image" content="(.-)"') or ''
70 Spiegel.get_media_id(self)
72 local config = Spiegel.get_config(self)
73 local formats = Spiegel.iter_formats(config)
75 local U = require 'quvi/util'
76 local format = U.choose_format(self, formats,
77 Spiegel.choose_best,
78 Spiegel.choose_default,
79 Spiegel.to_s)
80 or error("unable to choose format")
81 self.duration = (format.duration or 0) * 1000 -- to msec
82 self.url = {format.url or error("no match: media url")}
84 return self
85 end
88 -- Utility functions
91 function Spiegel.get_media_id(self)
92 self.id = self.page_url:match("/video/.-video%-(.-)%.")
93 or error ("no match: media id")
94 end
96 function Spiegel.get_config(self)
97 local fmt_s = "http://video.spiegel.de/flash/%s.xml"
98 local config_url = string.format(fmt_s, self.id)
99 return quvi.fetch(config_url, {fetch_type = 'config'})
102 function Spiegel.iter_formats(config)
103 local p = '<filename>(.-)<'
104 .. '.-<codec>(.-)<'
105 .. '.-<totalbitrate>(%d+)'
106 .. '.-<width>(%d+)'
107 .. '.-<height>(%d+)'
108 .. '.-<duration>(%d+)'
109 local t = {}
110 for fn,c,b,w,h,d in config:gmatch(p) do
111 local cn = fn:match('%.(%w+)$') or error('no match: container')
112 local u = 'http://video.spiegel.de/flash/' .. fn
113 -- print(u,c,b,w,h,cn,d)
114 table.insert(t, {codec=string.lower(c), url=u,
115 width=tonumber(w), height=tonumber(h),
116 bitrate=tonumber(b), duration=tonumber(d),
117 container=cn})
119 return t
122 function Spiegel.choose_best(formats) -- Highest quality available
123 local r = {width=0, height=0, bitrate=0, url=nil}
124 local U = require 'quvi/util'
125 for _,v in pairs(formats) do
126 if U.is_higher_quality(v,r) then
127 r = v
130 return r
133 function Spiegel.choose_default(formats)
134 return formats[1]
137 function Spiegel.to_s(t)
138 return string.format('%s_%s_%sk_%sp',
139 t.container, t.codec, t.bitrate, t.height)
142 -- vim: set ts=4 sw=4 tw=72 expandtab: