Split from git://repo.or.cz/quvi.git
[libquvi-scripts.git] / share / lua / website / spiegel.lua
blob9949c6b98d22b0914ec511413f221127ed8f9899
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
24 -- NOTE: mp4s do not appear to be available (404) even if listed in the
25 -- config xml, this is the case at least with the test URL
28 local Spiegel = {} -- Utility functions unique to this script
30 -- Identify the script.
31 function ident(self)
32 package.path = self.script_dir .. '/?.lua'
33 local C = require 'quvi/const'
34 local r = {}
35 r.domain = "spiegel%.de"
36 r.formats = "default|best"
37 r.categories = C.proto_http
38 local U = require 'quvi/util'
39 r.handles = U.handles(self.page_url, {r.domain}, {"/video/"})
40 return r
41 end
43 -- Query available formats.
44 function query_formats(self)
45 Spiegel.get_media_id(self)
47 local config = Spiegel.get_config(self)
48 local formats = Spiegel.iter_formats(config)
50 local t = {}
51 for _,v in pairs(formats) do
52 table.insert(t, Spiegel.to_s(v))
53 end
55 table.sort(t)
56 self.formats = table.concat(t, "¦")
58 return self
59 end
61 -- Parse media URL.
62 function parse(self)
63 self.host_id = "spiegel"
65 Spiegel.get_media_id(self)
67 local playlist = Spiegel.get_playlist(self)
69 local _,_,s = playlist:find("<headline>(.-)</")
70 self.title = s or error ("no match: media title")
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)
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 local _,_,s = self.page_url:find("/video/video%-(.-)%.")
93 self.id = s or error ("no match: media id")
94 end
96 function Spiegel.get_playlist(self)
97 local fmt_s = "http://www1.spiegel.de/active/playlist/fcgi/playlist.fcgi/"
98 .. "asset=flashvideo/mode=id/id=%s"
100 local playlist_url = string.format(fmt_s, self.id)
102 return quvi.fetch(playlist_url, {fetch_type = 'playlist'})
105 function Spiegel.get_config(self)
106 local fmt_s = "http://video.spiegel.de/flash/%s.xml"
107 local config_url = string.format(fmt_s, self.id)
108 return quvi.fetch(config_url, {fetch_type = 'config'})
111 function Spiegel.iter_formats(config)
112 local p = '<filename>(.-)<'
113 .. '.-<codec>(.-)<'
114 .. '.-<totalbitrate>(%d+)'
115 .. '.-<width>(%d+)'
116 .. '.-<height>(%d+)'
117 .. '.-<duration>(%d+)'
118 local t = {}
119 for fn,c,b,w,h,d in config:gfind(p) do
120 local _,_,cn = fn:find('%.(%w+)$')
121 local u = 'http://video.spiegel.de/flash/' .. fn
122 -- print(u,c,b,w,h,cn,d)
123 table.insert(t, {codec=string.lower(c), url=u,
124 width=tonumber(w), height=tonumber(h),
125 bitrate=tonumber(b), duration=tonumber(d),
126 container=cn})
128 return t
131 function Spiegel.choose_best(formats) -- Highest quality available
132 local r = {width=0, height=0, bitrate=0, url=nil}
133 local U = require 'quvi/util'
134 for _,v in pairs(formats) do
135 if U.is_higher_quality(v,r) then
136 r = v
139 return r
142 function Spiegel.choose_default(formats) -- Lowest quality available
143 local r = {width=0xffff, height=0xffff, bitrate=0xffff, url=nil}
144 local U = require 'quvi/util'
145 for _,v in pairs(formats) do
146 if U.is_lower_quality(v,r) then
147 r = v
150 return r
153 function Spiegel.to_s(t)
154 return string.format('%s_%s_%sk_%sp',
155 t.container, t.codec, t.bitrate, t.height)
158 -- vim: set ts=4 sw=4 tw=72 expandtab: