From 401fe5b520cf98e56ff911f7d9604a28934a25dd Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Tue, 7 May 2013 23:09:08 +0300 Subject: [PATCH] media/ardmediathek.lua: Rewrite parse function Rewrite the `parse' function for the libquvi 0.9+ series. * Remove now obsolete code (e.g. self.host_id) * Update vim modeline ArdMediathek: * Reimplement `container_from' as `to_container' * Reimplement `test_availability`as `chk_avail' * Reimplement `iter_formats' as `iter_streams' * Reimplement `quality_from' as `to_quality' * Reimplement `height_from' as `to_resolution' * Implement `to_encoding' * Implement `get_title' * Remove `get_config' Signed-off-by: Toni Gundogdu --- share/media/ardmediathek.lua | 331 ++++++++++++++++++++++--------------------- 1 file changed, 166 insertions(+), 165 deletions(-) rewrite share/media/ardmediathek.lua (69%) diff --git a/share/media/ardmediathek.lua b/share/media/ardmediathek.lua dissimilarity index 69% index b9769ad..7aba3c5 100644 --- a/share/media/ardmediathek.lua +++ b/share/media/ardmediathek.lua @@ -1,165 +1,166 @@ --- libquvi-scripts --- Copyright (C) 2013 Toni Gundogdu --- Copyright (C) 2013 Thomas Weißschuh --- --- This file is part of libquvi-scripts . --- --- This program is free software: you can redistribute it and/or --- modify it under the terms of the GNU Affero General Public --- License as published by the Free Software Foundation, either --- version 3 of the License, or (at your option) any later version. --- --- This program 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 Affero General Public License for more details. --- --- You should have received a copy of the GNU Affero General --- Public License along with this program. If not, see --- . --- - -local ArdMediathek = {} -- Utility functions unique to to this script. - --- Identify the media script. -function ident(qargs) - return { - can_parse_url = ArdMediathek.can_parse_url(qargs), - domains = table.concat({'ardmediathek.de'}, ',') - } -end - -function parse(self) - - local config = ArdMediathek.get_config(self) - local Util = require 'quvi/util' - - self.host_id = 'ard' - self.title = config:match( - 'ARD Mediathek %- Fehlerseite') then - error('invalid URL, maybe the media is no longer available') - end - - return c -end - -function ArdMediathek.choose_best(t) - return t[#t] -- return the last from the array -end - -function ArdMediathek.choose_default(t) - return t[1] -- return the first from the array -end - -function ArdMediathek.to_s(t) - return string.format("%s_%s_i%02d%s%s", - (t.quality) and t.quality or 'sd', - t.container, t.stream_id, - (t.encoding) and '_'..t.encoding or '', - (t.height) and '_'..t.height or '') -end - -function ArdMediathek.quality_from(suffix) - local q = suffix:match('%.web(%w)%.') or suffix:match('%.(%w)%.') - or suffix:match('[=%.]Web%-(%w)') -- .webs. or Web-S or .s - if q then - q = q:lower() - local t = {s='ld', m='md', l='sd', xl='hd'} - for k,v in pairs(t) do - if q == k then return v end - end - end - return q -end - -function ArdMediathek.height_from(suffix) - local h = suffix:match('_%d+x(%d+)[_%.]') - if h then return h..'p' end -end - -function ArdMediathek.container_from(suffix) - return suffix:match('^(...):') or suffix:match('%.(...)$') or 'mp4' -end - -function ArdMediathek.iter_formats(page) - local r = {} - local s = 'mediaCollection%.addMediaStream' - .. '%(0, (%d+), "(.-)", "(.-)", "%w+"%);' - - ArdMediathek.test_availability(page) - - for s_id, prefix, suffix in page:gmatch(s) do - local u = prefix .. suffix - u = u:match('^(.-)?') or u -- remove querystring - local t = { - container = ArdMediathek.container_from(suffix), - encoding = suffix:match('%.(h264)%.'), - quality = ArdMediathek.quality_from(suffix), - height = ArdMediathek.height_from(suffix), - stream_id = s_id, -- internally (by service) used stream ID - url = u - } - table.insert(r,t) - end - if #r == 0 then error('no media urls found') end - return r -end - --- vim: set ts=4 sw=4 sts=4 tw=72 expandtab: +-- libquvi-scripts +-- Copyright (C) 2013 Toni Gundogdu +-- Copyright (C) 2013 Thomas Weißschuh +-- +-- This file is part of libquvi-scripts . +-- +-- This program is free software: you can redistribute it and/or +-- modify it under the terms of the GNU Affero General Public +-- License as published by the Free Software Foundation, either +-- version 3 of the License, or (at your option) any later version. +-- +-- This program 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 Affero General Public License for more details. +-- +-- You should have received a copy of the GNU Affero General +-- Public License along with this program. If not, see +-- . +-- + +local ArdMediathek = {} -- Utility functions unique to to this script. + +-- Identify the media script. +function ident(qargs) + return { + can_parse_url = ArdMediathek.can_parse_url(qargs), + domains = table.concat({'ardmediathek.de'}, ',') + } +end + +-- Parse the media properties. +function parse(qargs) + + local p = quvi.http.fetch(qargs.input_url).data + + ArdMediathek.chk_avail(p) + + qargs.thumb_url = p:match('property="og:image" content="(.-)"') or '' + + qargs.id = qargs.input_url:match('documentId=(%d+)') or '' + + qargs.title = ArdMediathek.get_title(p) + + qargs.streams = ArdMediathek.iter_streams(p) + + return qargs +end + +-- +-- Utility functions +-- + +function ArdMediathek.can_parse_url(qargs) + local U = require 'socket.url' + local t = U.parse(qargs.input_url) + if t and t.scheme and t.scheme:lower():match('^http$') + and t.host and t.host:lower():match('ardmediathek%.de$') + and t.query and t.query:match('^documentId=%d+$') + then + return true + else + return false + end +end + +function ArdMediathek.get_title(p) + return p:match('ARD Mediathek %- Fehlerseite') then + error('an invalid media URL -- the media is no longer available?') + end + -- Some videos are only scrapable at certain times + local s = 'Der Clip ist deshalb nur von (%d%d?) bis (%d%d?) Uhr verfügbar' + local from,to = p:match(s) + if from and to then + local t = {'media available from ', from, ':00 to ', to, ':00 CET only'} + error(table.concat(t,'')) + end +end + +function ArdMediathek.to_container(s) + return (s:match('^(...):') or s:match('%.(...)$') or ''):lower() +end + +function ArdMediathek.to_encoding(s) + return (s:match('%.(%w+)%.%w+$') or ''):lower() +end + +function ArdMediathek.to_resolution(s) + local w,h = s:match('_(%d+)x(%d+)[_%.]') + return tonumber(w or 0), tonumber(h or 0) +end + +function ArdMediathek.to_quality(s) + local q = (s:match('%.web(%w)%.') or s:match('%.(%w)%.') + or s:match('[=%.]Web%-(%w)') -- ".webs", ".s" or "Web-S" + or ''):lower() + if #q then + for k,v in pairs({s='ld', m='md', l='sd', xl='hd'}) do + if q == k then + return v + end + end + end + return q +end + +function ArdMediathek.iter_streams(p) + local S = require 'quvi/stream' + local s = 'mediaCollection%.addMediaStream' + .. '%(0, (%d+), "(.-)", "(.-)", "%w+"%);' + local r = {} + for id,pre,suf in p:gmatch(s) do + local u = table.concat({pre,suf},'') + u = u:match('^(.-)?') or u -- remove the query string + + local t = S.stream_new(u) + + -- Available only for some videos. + t.video.width, t.video.height = ArdMediathek.to_resolution(u) + + t.container = ArdMediathek.to_container(u) + t.quality = ArdMediathek.to_quality(u) + + -- Ignored by libquvi. + t.nostd = { internal_id=id } + + -- Without the 'quality' the stream ID would incomplete. + if #t.quality ==0 then + t.quality = ArdMediathek.to_encoding(u) + else + t.video.encoding = ArdMediathek.to_encoding(u) + end + + t.id = ArdMediathek.to_id(t) + + table.insert(r,t) + end + + if #r >1 then + ArdMediathek.ch_best(S, r) + end + + return r +end + +function ArdMediathek.ch_best(S, t) + local r = t[#t] -- Make the last the best quality. + r.flags.best = true +end + +function ArdMediathek.to_id(t) + return string.format('%s_%s_i%02d%s%s', + (#t.quality >0) and t.quality or 'sd', + t.container, t.nostd.internal_id, + (#t.video.encoding >0) and ('_'..t.video.encoding) or '', + (t.video.height >0) and ('_'..t.video.height..'p') or '') +end + +-- vim: set ts=2 sw=2 tw=72 expandtab: -- 2.11.4.GIT