media/spiegel.lua: Rewrite ident function
[libquvi-scripts.git] / share / media / spiegel.lua
blob5ad8af2103c9b775732e02802c304ad74e569c87
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 media script.
29 function ident(qargs)
30 return {
31 can_parse_url = Spiegel.can_parse_url(qargs),
32 domains = table.concat({'spiegel.de'}, ',')
34 end
36 -- Query available formats.
37 function query_formats(self)
38 Spiegel.get_media_id(self)
40 local config = Spiegel.get_config(self)
41 local formats = Spiegel.iter_formats(config)
43 local t = {}
44 for _,v in pairs(formats) do
45 table.insert(t, Spiegel.to_s(v))
46 end
48 table.sort(t)
49 self.formats = table.concat(t, "¦")
51 return self
52 end
54 -- Parse media URL.
55 function parse(self)
56 self.host_id = "spiegel"
58 local p = quvi.fetch(self.page_url)
60 self.title = p:match('"spVideoTitle">(.-)<')
61 or error('no match: media title')
63 self.thumbnail_url = p:match('"og:image" content="(.-)"') or ''
65 Spiegel.get_media_id(self)
67 local config = Spiegel.get_config(self)
68 local formats = Spiegel.iter_formats(config)
70 local U = require 'quvi/util'
71 local format = U.choose_format(self, formats,
72 Spiegel.choose_best,
73 Spiegel.choose_default,
74 Spiegel.to_s)
75 or error("unable to choose format")
76 self.duration = (format.duration or 0) * 1000 -- to msec
77 self.url = {format.url or error("no match: media url")}
79 return self
80 end
83 -- Utility functions
86 function Spiegel.can_parse_url(qargs)
87 local U = require 'socket.url'
88 local t = U.parse(qargs.input_url)
89 if t and t.scheme and t.scheme:lower():match('^http$')
90 and t.host and t.host:lower():match('spiegel%.de$')
91 and t.path and t.path:lower():match('^/video/.-%d+%.html$')
92 then
93 return true
94 else
95 return false
96 end
97 end
99 function Spiegel.get_media_id(self)
100 self.id = self.page_url:match("/video/.-video%-(.-)%.")
101 or error ("no match: media id")
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:gmatch(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)
142 return formats[1]
145 function Spiegel.to_s(t)
146 return string.format('%s_%s_%sk_%sp',
147 t.container, t.codec, t.bitrate, t.height)
150 -- vim: set ts=4 sw=4 tw=72 expandtab: