FIX: media/spiegel.lua: title pattern (BACKPORTpt4)
[libquvi-scripts.git] / share / media / spiegel.lua
blob492f1c3153d899a9f0b8c5b077c0f62811730ce0
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('"module%-title">(.-)</')
45 or p:match('"og:title".-content="(.-)%s+%-%s+SP')
46 or ''
48 -- Make mandatory: needed to fetch the config XML
49 qargs.id = qargs.input_url:match('/video/.-(%d+)%.html$')
50 or error('no match: media ID')
52 local t = {'http://video.spiegel.de/flash/', qargs.id, '.xml'}
53 local c = quvi.http.fetch(table.concat(t,'')).data
55 local P = require 'lxp.lom'
56 local x = P.parse(c)
58 qargs.streams = Spiegel.iter_streams(x)
60 qargs.duration_ms = qargs.streams[1].nostd.duration_ms
62 return qargs
63 end
66 -- Utility functions
69 function Spiegel.can_parse_url(qargs)
70 local U = require 'socket.url'
71 local t = U.parse(qargs.input_url)
72 if t and t.scheme and t.scheme:lower():match('^http$')
73 and t.host and t.host:lower():match('spiegel%.de$')
74 and t.path and t.path:lower():match('^/video/.-%d+%.html$')
75 then
76 return true
77 else
78 return false
79 end
80 end
82 function Spiegel.iter_streams(x)
83 local S = require 'quvi/stream'
84 local L = require 'quvi/lxph'
86 local r = {}
88 for i=1, #x do
89 if x[i].tag and x[i].tag:match('type%d+') then
91 local n = L.find_first_tag(x[i], 'filename')[1]
92 local u = 'http://video.spiegel.de/flash/'..n
93 local t = S.stream_new(u)
95 t.video = {
96 bitrate_kbit_s = tonumber(L.find_first_tag(x[i], 'totalbitrate')[1]),
97 height = tonumber(L.find_first_tag(x[i], 'height')[1]),
98 encoding = L.find_first_tag(x[i], 'codec')[1]:lower(),
99 width = tonumber(L.find_first_tag(x[i], 'width')[1])
102 -- Used by this script only. libquvi will ignore the 'nostd' values.
103 t.nostd = {
104 duration_ms = (tonumber(L.find_first_tag(x[i], 'duration')[1])*1000)
107 t.container = (n:match('%.(%w+)$') or ''):lower()
108 t.id = Spiegel.to_id(t)
110 table.insert(r,t)
114 if #r >1 then
115 Spiegel.ch_best(S, r)
118 return r
121 function Spiegel.ch_best(S, t)
122 local r = t[1]
123 r.flags.best = true
124 for _,v in pairs(t) do
125 if v.video.height > r.video.height then
126 r = S.swap_best(r, v)
131 function Spiegel.to_id(t)
132 return string.format('%s_%s_%sk_%sp',
133 t.container, t.video.encoding, t.video.bitrate_kbit_s, t.video.height)
136 -- vim: set ts=2 sw=2 tw=72 expandtab: