media/spiegel.lua: Remove query_formats function
[libquvi-scripts.git] / share / media / spiegel.lua
blob002920854e878b8ec4d0da1f85cdca08255cf254
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 -- Parse the media properties.
37 function parse(qargs)
38 local C = require 'quvi/const'
39 local o = { [C.qoo_fetch_from_charset] = 'iso-8859-1' }
40 local p = quvi.http.fetch(qargs.input_url, o).data
42 qargs.thumb_url = p:match('"og:image" content="(.-)"') or ''
44 qargs.title = p:match('"spVideoTitle">(.-)<') or ''
46 -- Make mandatory: needed to fetch the config XML
47 qargs.id = qargs.input_url:match('/video/.-(%d+)%.html$')
48 or error('no match: media ID')
50 local t = {'http://video.spiegel.de/flash/', qargs.id, '.xml'}
51 local c = quvi.http.fetch(table.concat(t,'')).data
53 local P = require 'lxp.lom'
54 local x = P.parse(c)
56 qargs.streams = Spiegel.iter_streams(x)
58 qargs.duration_ms = qargs.streams[1].nostd.duration_ms
60 return qargs
61 end
64 -- Utility functions
67 function Spiegel.can_parse_url(qargs)
68 local U = require 'socket.url'
69 local t = U.parse(qargs.input_url)
70 if t and t.scheme and t.scheme:lower():match('^http$')
71 and t.host and t.host:lower():match('spiegel%.de$')
72 and t.path and t.path:lower():match('^/video/.-%d+%.html$')
73 then
74 return true
75 else
76 return false
77 end
78 end
80 function Spiegel.iter_streams(x)
81 local S = require 'quvi/stream'
82 local L = require 'quvi/lxph'
84 local r = {}
86 for i=1, #x do
87 if x[i].tag and x[i].tag:match('type%d+') then
89 local n = L.find_first_tag(x[i], 'filename')[1]
90 local u = 'http://video.spiegel.de/flash/'..n
91 local t = S.stream_new(u)
93 t.video = {
94 bitrate_kbit_s = tonumber(L.find_first_tag(x[i], 'totalbitrate')[1]),
95 height = tonumber(L.find_first_tag(x[i], 'height')[1]),
96 encoding = L.find_first_tag(x[i], 'codec')[1]:lower(),
97 width = tonumber(L.find_first_tag(x[i], 'width')[1])
100 -- Used by this script only. libquvi will ignore the 'nostd' values.
101 t.nostd = {
102 duration_ms = (tonumber(L.find_first_tag(x[i], 'duration')[1])*1000)
105 t.container = (n:match('%.(%w+)$') or ''):lower()
106 t.id = Spiegel.to_id(t)
108 table.insert(r,t)
112 if #r >1 then
113 Spiegel.ch_best(S, r)
116 return r
119 function Spiegel.ch_best(S, t)
120 local r = t[1]
121 r.flags.best = true
122 for _,v in pairs(t) do
123 if v.video.height > r.video.height then
124 r = S.swap_best(r, v)
129 function Spiegel.to_id(t)
130 return string.format('%s_%s_%sk_%sp',
131 t.container, t.video.encoding, t.video.bitrate_kbit_s, t.video.height)
134 -- vim: set ts=2 sw=2 tw=72 expandtab: