spiegel.lua: Use 'foo:match' instead
[libquvi-scripts.git] / share / lua / website / spiegel.lua
blob457f4762b37cc5489ca06be3d511764ac8431ff1
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.
27 local Spiegel = {} -- Utility functions unique to this script
29 -- Identify the script.
30 function ident(self)
31 package.path = self.script_dir .. '/?.lua'
32 local C = require 'quvi/const'
33 local r = {}
34 r.domain = "spiegel%.de"
35 r.formats = "default|best"
36 r.categories = C.proto_http
37 local U = require 'quvi/util'
38 r.handles = U.handles(self.page_url, {r.domain}, {"/video/"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 Spiegel.get_media_id(self)
46 local config = Spiegel.get_config(self)
47 local formats = Spiegel.iter_formats(config)
49 local t = {}
50 for _,v in pairs(formats) do
51 table.insert(t, Spiegel.to_s(v))
52 end
54 table.sort(t)
55 self.formats = table.concat(t, "¦")
57 return self
58 end
60 -- Parse media URL.
61 function parse(self)
62 self.host_id = "spiegel"
64 Spiegel.get_media_id(self)
66 local p = Spiegel.get_playlist(self)
68 self.title = p:match("<headline>(.-)</")
69 or error ("no match: media title")
71 local config = Spiegel.get_config(self)
72 local formats = Spiegel.iter_formats(config)
74 local U = require 'quvi/util'
75 local format = U.choose_format(self, formats,
76 Spiegel.choose_best,
77 Spiegel.choose_default,
78 Spiegel.to_s)
79 or error("unable to choose format")
80 self.duration = (format.duration or 0) * 1000 -- to msec
81 self.url = {format.url or error("no match: media url")}
83 return self
84 end
87 -- Utility functions
90 function Spiegel.get_media_id(self)
91 self.id = self.page_url:match("/video/video%-(.-)%.")
92 or error ("no match: media id")
93 end
95 function Spiegel.get_playlist(self)
96 local fmt_s = "http://www1.spiegel.de/active/playlist/fcgi/playlist.fcgi/"
97 .. "asset=flashvideo/mode=id/id=%s"
99 local playlist_url = string.format(fmt_s, self.id)
101 return quvi.fetch(playlist_url, {fetch_type = 'playlist'})
104 function Spiegel.get_config(self)
105 local fmt_s = "http://video.spiegel.de/flash/%s.xml"
106 local config_url = string.format(fmt_s, self.id)
107 return quvi.fetch(config_url, {fetch_type = 'config'})
110 function Spiegel.iter_formats(config)
111 local p = '<filename>(.-)<'
112 .. '.-<codec>(.-)<'
113 .. '.-<totalbitrate>(%d+)'
114 .. '.-<width>(%d+)'
115 .. '.-<height>(%d+)'
116 .. '.-<duration>(%d+)'
117 local t = {}
118 for fn,c,b,w,h,d in config:gfind(p) do
119 local cn = fn:match('%.(%w+)$') or error('no match: container')
120 local u = 'http://video.spiegel.de/flash/' .. fn
121 -- print(u,c,b,w,h,cn,d)
122 table.insert(t, {codec=string.lower(c), url=u,
123 width=tonumber(w), height=tonumber(h),
124 bitrate=tonumber(b), duration=tonumber(d),
125 container=cn})
127 return t
130 function Spiegel.choose_best(formats) -- Highest quality available
131 local r = {width=0, height=0, bitrate=0, url=nil}
132 local U = require 'quvi/util'
133 for _,v in pairs(formats) do
134 if U.is_higher_quality(v,r) then
135 r = v
138 return r
141 function Spiegel.choose_default(formats) -- Lowest quality available
142 local r = {width=0xffff, height=0xffff, bitrate=0xffff, url=nil}
143 local U = require 'quvi/util'
144 for _,v in pairs(formats) do
145 if U.is_lower_quality(v,r) then
146 r = v
149 return r
152 function Spiegel.to_s(t)
153 return string.format('%s_%s_%sk_%sp',
154 t.container, t.codec, t.bitrate, t.height)
157 -- vim: set ts=4 sw=4 tw=72 expandtab: