2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2013 Thomas Weißschuh <thomas@t-8ch.de>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local ArdMediathek
= {} -- Utility functions unique to to this script.
24 -- Identify the media script.
27 can_parse_url
= ArdMediathek
.can_parse_url(qargs
),
28 domains
= table.concat({'ardmediathek.de'}, ',')
32 -- Parse the media properties.
35 local p
= quvi
.http
.fetch(qargs
.input_url
).data
37 ArdMediathek
.chk_avail(p
)
39 qargs
.thumb_url
= p
:match('property="og:image" content="(.-)"') or ''
41 qargs
.id
= qargs
.input_url
:match('documentId=(%d+)') or ''
43 qargs
.title
= ArdMediathek
.get_title(p
)
45 qargs
.streams
= ArdMediathek
.iter_streams(p
)
54 function ArdMediathek
.can_parse_url(qargs
)
55 local U
= require
'socket.url'
56 local t
= U
.parse(qargs
.input_url
)
57 if t
and t
.scheme
and t
.scheme
:lower():match('^http$')
58 and t
.host
and t
.host
:lower():match('ardmediathek%.de$')
59 and t
.query
and t
.query
:match('^documentId=%d+$')
67 function ArdMediathek
.get_title(p
)
68 return p
:match('<meta property="og:title" content="([^"]*)')
69 :gsub('%s*%- %w-$', '') -- remove name of station
70 :gsub('%s*%(FSK.*', '') -- remove FSK nonsense
74 function ArdMediathek
.chk_avail(p
)
75 if p
:match('<title>ARD Mediathek %- Fehlerseite</title>') then
76 error('an invalid media URL -- the media is no longer available?')
78 -- Some videos are only scrapable at certain times
79 local s
= 'Der Clip ist deshalb nur von (%d%d?) bis (%d%d?) Uhr verfügbar'
80 local from
,to
= p
:match(s
)
82 local t
= {'media available from ', from
, ':00 to ', to
, ':00 CET only'}
83 error(table.concat(t
,''))
87 function ArdMediathek
.to_container(s
)
88 return (s
:match('^(...):') or s
:match('%.(...)$') or ''):lower()
91 function ArdMediathek
.to_encoding(s
)
92 return (s
:match('%.(%w+)%.%w+$') or ''):lower()
95 function ArdMediathek
.to_resolution(s
)
96 local w
,h
= s
:match('_(%d+)x(%d+)[_%.]')
97 return tonumber(w
or 0), tonumber(h
or 0)
100 function ArdMediathek
.to_quality(s
)
101 local q
= (s
:match('%.web(%w)%.') or s
:match('%.(%w)%.')
102 or s
:match('[=%.]Web%-(%w)') -- ".webs", ".s" or "Web-S"
105 for k
,v
in pairs({s
='ld', m
='md', l
='sd', xl
='hd'}) do
114 function ArdMediathek
.iter_streams(p
)
115 local S
= require
'quvi/stream'
116 local s
= 'mediaCollection%.addMediaStream'
117 .. '%(0, (%d+), "(.-)", "(.-)", "%w+"%);'
119 for id
,pre
,suf
in p
:gmatch(s
) do
120 local u
= table.concat({pre
,suf
},'')
121 u
= u
:match('^(.-)?') or u
-- remove the query string
123 local t
= S
.stream_new(u
)
125 -- Available only for some videos.
126 t
.video
.width
, t
.video
.height
= ArdMediathek
.to_resolution(u
)
128 t
.container
= ArdMediathek
.to_container(u
)
129 t
.quality
= ArdMediathek
.to_quality(u
)
131 -- Ignored by libquvi.
132 t
.nostd
= { internal_id
=id
}
134 -- Without the 'quality' the stream ID would incomplete.
135 if #t
.quality
==0 then
136 t
.quality
= ArdMediathek
.to_encoding(u
)
138 t
.video
.encoding
= ArdMediathek
.to_encoding(u
)
141 t
.id
= ArdMediathek
.to_id(t
)
147 ArdMediathek
.ch_best(S
, r
)
153 function ArdMediathek
.ch_best(S
, t
)
154 local r
= t
[#t
] -- Make the last the best quality.
158 function ArdMediathek
.to_id(t
)
159 return string.format('%s_%s_i%02d%s%s',
160 (#t
.quality
>0) and t
.quality
or 'sd',
161 t
.container
, t
.nostd
.internal_id
,
162 (#t
.video
.encoding
>0) and ('_'..t
.video
.encoding
) or '',
163 (t
.video
.height
>0) and ('_'..t
.video
.height
..'p') or '')
166 -- vim: set ts=2 sw=2 tw=72 expandtab: