summaryrefslogtreecommitdiff
path: root/.local/share/libquvi-scripts/lua/website/zdfmediathek.lua
blob: 4b9e4be370db784abcdadb54fab1a2679dbf6b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

-- libquvi-scripts
-- Copyright (C) 2014  Benjamin Franzke <benjaminfranzke@googlemail.com>
--
-- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-- 02110-1301  USA
--

local ZDFmediathek = {} -- Utility functions unique to this script.

-- Identify the script.
function ident(self)
    package.path = self.script_dir .. '/?.lua'
    local B      = require 'quvi/bit'
    local C      = require 'quvi/const'
    local U      = require 'quvi/util'
    local r      = {}
    r.domain     = "zdf%.de"
    r.formats    = "default|best"
    r.categories = B.bit_or(C.proto_http, C.proto_rtsp)
    r.handles    = U.handles(self.page_url, {r.domain}, {"/ZDFmediathek/"})
    return r
end

-- Query available formats.
function query_formats(self)
    local c = ZDFmediathek.get_config(self)
    local fmts = ZDFmediathek.iter_formats(c)
    local r = {}

    table.sort(fmts, ZDFmediathek.compare_format)
    for _,f in pairs(fmts) do
        local s = ZDFmediathek.to_s(f)
        -- There may be duplicates -> filter out
        if s ~= r[#r] then table.insert(r,s) end
    end
    self.formats = table.concat(r, "|")

    return self
end

-- Parse media URL.
function parse(self)
    local U = require 'quvi/util'

    local c = ZDFmediathek.get_config(self)
    local fmts = ZDFmediathek.iter_formats(c)

    local f = U.choose_format(self, fmts,
                              ZDFmediathek.choose_best,
                              ZDFmediathek.choose_default,
                              ZDFmediathek.to_s)
                    or error("unable to match format")

    self.url = { f.url or error("no match: media URL") }

    return self
end

function ZDFmediathek.merge_format(fmt1, fmt2)
    local t = {}
    for k,v in pairs(fmt1) do t[k] = v end
    for k,v in pairs(fmt2) do t[k] = v end
    return t
end

function ZDFmediathek.table_add_format(t, fmt)
    -- Only accessible with HBBTV user agent, lets drop it
    if fmt.facets and fmt.facets:match('<facet>hbbtv</facet>') then return end

    table.insert(t, fmt)

    -- Some URLs/Formats are not explicitly listed but can be derived:

    -- The "high"-quality f4m file may contain a reference to a file that is
    -- of better quality than "veryhigh" classified http streams.
    -- Example: 2256k instead of 1496k
    if fmt.quality == "high" and fmt.type == "h264_aac_f4f_http_f4m_http" then
        local new = { type = "h264_aac_mp4_http_na_na", container = "mp4" }
        new.url = ZDFmediathek.http_mp4_from_f4m(fmt.url)
        if new.url then
            table.insert(t, ZDFmediathek.merge_format(fmt, new))
        end
    end

    -- HD quality movies are not listed with an http url.
    -- But one can build it from the rtmp URL given in a .meta file:
    if fmt.quality == "hd" and fmt.type == "h264_aac_mp4_rtmp_zdfmeta_http" then
        local new = { type = "h264_aac_mp4_http_na_na", container = "mp4",
                      protocol = "http" }
        new.url = ZDFmediathek.http_mp4_from_zdfmeta(fmt.url)
        if new.url then
            table.insert(t, ZDFmediathek.merge_format(fmt, new))
        end
    end

    return
end

function ZDFmediathek.iter_formats(c)
    local p = {
        '<formitaet basetype="(.-_.-_(.-)_(.-)_.-)".->',
          '<quality>(.-)</quality>',
          '<url>(.-)</url>',
          '(.-)',
        '</formitaet>'
    }
    local t = {}

    for type,container,protocol,quality,url,data in
        c:gmatch(table.concat(p, '%s*')) do
        f = { type = type, container = container, protocol = protocol,
              quality = quality, url = url }
        -- width and height are not available for live streams -> may be nil
        f.width = tonumber(data:match('<width>(.-)</width>'))
        f.height = tonumber(data:match('<height>(.-)</height>'))
        f.facets = data:match('<facets>(.+)</facets>')
        ZDFmediathek.table_add_format(t, f)
    end

    return t
end

function ZDFmediathek.get_config(self)
    local U = require 'quvi/util'
    self.host_id  = "zdfmediathek"

    self.id = self.page_url:match('/ZDFmediathek/#?/?beitrag/video/(%d+)') or
        self.page_url:match('/ZDFmediathek/#?/?beitrag/live/(%d+)')
        or error ("no match: media id")

    local xml = { "http://www.zdf.de/",
                  "ZDFmediathek/xmlservice/web/beitragsDetails?id=", self.id }

    local c = quvi.fetch(table.concat(xml), {fetch_type='config'})
    if not(c:match('<statuscode>ok</statuscode>')) then
        error(table.concat({'error:', U.xml_get(c, 'statuscode', false), '-',
                                      U.xml_get(c, 'debuginfo', false)}, ' '))
    end

    self.title = U.xml_get(c, 'title', false)
    self.thumbnail_url =
            c:match('<teaserimage alt=".-" key="946x532">([^<]+)</teaserimage>')

    -- Quirks for broken stream descriptors
    if U.xml_get(c, 'type', false) == "livevideo" then
        -- The server returns 403 without ?hdcore
        c = c:gsub('manifest%.f4m</url>', 'manifest.f4m?hdcore</url>')

        -- Fix incorrect basetype
        c = c:gsub('h264_aac_na_rtsp_mov_http', 'h264_aac_3gp_rtsp_na_na')
    end

    return c
end

function ZDFmediathek.http_mp4_from_zdfmeta(meta_url)
    local c = quvi.fetch(meta_url, {fetch_type='config'})
    local path = c:match("mp4:(.-%.mp4)")

    return path and "http://nrodl.zdf.de/none/" .. path or nil
end

function ZDFmediathek.http_mp4_from_f4m(f4m_url)
    local c = quvi.fetch(f4m_url, {fetch_type='config'})
    local p = '<media href="(.-)/manifest%.f4m%?hdcore" bitrate="(%d+)"/>'
    local max_bitrate = 0
    local path = nil

    for _path, bitrate in c:gmatch(p) do
        if tonumber(bitrate) > max_bitrate then
            max_bitrate = tonumber(bitrate)
            path = _path
        end
    end

    return path and "http://nrodl.zdf.de/none/" .. path or nil
end

function ZDFmediathek.rank_type(f)
    local rank = {
        h264_aac_mp4_http_na_na    = 4,
        vp8_vorbis_webm_http_na_na = 3,
        h264_aac_3gp_rtsp_na_na    = 2,
        h264_aac_ts_http_m3u8_http = 1
    }
    return rank[f.type] and rank[f.type] or 0
end

function ZDFmediathek.rank_quality(f)
    local rank = { hd = 5, veryhigh = 4, high = 3, med = 2, low = 1 }
    return rank[f.quality] and rank[f.quality] or 0
end

function ZDFmediathek.compare_format(f1, f2)
    local t1 = ZDFmediathek.rank_type(f1)
    local t2 = ZDFmediathek.rank_type(f2)

    if (t1 ~= t2) then
        return t1 > t2
    end

    if f1.width and f2.width and f1.height and f2.height then
        local U = require 'quvi/util'
        return U.is_higher_quality(f1, f2)
    end

    return ZDFmediathek.rank_quality(f1) > ZDFmediathek.rank_quality(f2)
end

function ZDFmediathek.choose_best(t)
    table.sort(t, ZDFmediathek.compare_format)
    return t[1]
end

ZDFmediathek.choose_default = ZDFmediathek.choose_best

function ZDFmediathek.to_s(t)
    return table.concat({ t.container,
                          (t.protocol ~= "http") and '_' .. t.protocol or '',
                          '_', (t.height) and t.height ..'p' or t.quality })
end

-- vim: set ts=4 sw=4 tw=72 expandtab: