ard.lua: cleanup
[libquvi-scripts.git] / share / lua / website / ard.lua
blob39fb0577b8ea8a9fbdc12724d9eeed8c76b27ec0
1 -- libquvi-scripts
2 -- Copyright (C) 2013 Thomas Weißschuh
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library 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 GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 -- 02110-1301 USA
21 local Ard = {}
23 function ident(self)
24 local C = require 'quvi/const'
25 local U = require 'quvi/util'
26 local B = require 'quvi/bit'
27 local r = {}
28 r.domain = 'www%.ardmediathek%.de'
29 r.formats = 'default|best'
30 r.categories = B.bit_or(C.proto_http, C.proto_rtmp)
31 r.handles = U.handles(self.page_url, {r.domain},
32 nil, {"documentId=%d+$"})
33 return r
34 end
36 function query_formats(self)
37 local config = Ard.get_config(self)
38 local formats = Ard.iter_formats(config)
40 local t = {}
41 for _,v in pairs(formats) do
42 table.insert(t, Ard.to_s(v))
43 end
45 table.sort(t)
46 self.formats = table.concat(t, "|")
48 return self
49 end
51 function parse(self)
53 local config = Ard.get_config(self)
54 local Util = require 'quvi/util'
56 self.host_id = 'ard'
57 self.title = config:match(
58 '<meta property="og:title" content="([^"]*)'
59 ):gsub(
60 '%s*%- %w-$', '' -- remove name of station
61 ):gsub(
62 '%s*%(FSK.*', '' -- remove FSK nonsense
64 or error('no match: media title')
65 self.thumbnail_url = config:match(
66 '<meta property="og:image" content="([^"]*)'
67 ) or ''
69 local formats = Ard.iter_formats(config)
70 local format = Util.choose_format(self,
71 formats,
72 Ard.choose_best,
73 Ard.choose_default,
74 Ard.to_s)
75 or error('unable to choose format')
77 if not format.url then error('no match: media url') end
78 self.url = { format.url }
80 return self
81 end
83 function Ard.test_availability(page)
84 -- some videos are only scrapable at certain times
85 local fsk_pattern =
86 'Der Clip ist deshalb nur von (%d%d?) bis (%d%d?) Uhr verfügbar'
87 local from, to = page:match(fsk_pattern)
88 if from and to then
89 error('video only available from ' ..from.. ':00 to '
90 ..to.. ':00 CET')
91 end
92 end
94 function Ard.get_config(self)
95 local c = quvi.fetch(self.page_url)
96 self.id = self.page_url:match('documentId=(%d*)')
97 or error('no match: media id')
98 if c:match('<title>ARD Mediathek %- Fehlerseite</title>') then
99 error('invalid URL, maybe the media is no longer available')
102 return c
105 function Ard.choose_best(t)
106 return t[#t] -- return the last from the array
109 function Ard.choose_default(t)
110 return t[1] -- return the first from the array
113 function Ard.to_s(t)
114 return string.format("%s_%s_i%02d%s%s",
115 (t.quality) and t.quality or 'sd',
116 t.container, t.stream_id,
117 (t.encoding) and '_'..t.encoding or '',
118 (t.height) and '_'..t.height or '')
121 function Ard.quality_from(suffix)
122 local q = suffix:match('%.web(%w)%.') or suffix:match('%.(%w)%.')
123 or suffix:match('[=%.]Web%-(%w)') -- .webs. or Web-S or .s
124 if q then
125 q = q:lower()
126 local t = {s='ld', m='md', l='sd', xl='hd'}
127 for k,v in pairs(t) do
128 if q == k then return v end
131 return q
134 function Ard.height_from(suffix)
135 local h = suffix:match('_%d+x(%d+)[_%.]')
136 if h then return h..'p' end
139 function Ard.container_from(suffix)
140 return suffix:match('^(...):') or suffix:match('%.(...)$') or 'mp4'
143 function Ard.iter_formats(page)
144 local r = {}
145 local s = 'mediaCollection%.addMediaStream'
146 .. '%(0, (%d+), "(.-)", "(.-)", "%w+"%);'
148 Ard.test_availability(page)
150 for s_id, prefix, suffix in page:gmatch(s) do
151 local u = prefix .. suffix
152 u = u:match('^(.-)?') or u -- remove querystring
153 local t = {
154 container = Ard.container_from(suffix),
155 encoding = suffix:match('%.(h264)%.'),
156 quality = Ard.quality_from(suffix),
157 height = Ard.height_from(suffix),
158 stream_id = s_id, -- internally (by service) used stream ID
159 url = u
161 table.insert(r,t)
163 if #r == 0 then error('no media urls found') end
164 return r
167 -- vim: set ts=4 sw=4 sts=4 tw=72 expandtab: