media/videobash.lua: Rewrite parse function
[libquvi-scripts.git] / share / media / videobash.lua
blobff67ad519f6525c9b389c8cbd2ac5655c8f15529
1 -- libquvi-scripts
2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Thomas Preud'homme <robotux@celest.fr>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
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 Videobash = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = Videobash.can_parse_url(qargs),
28 domains = table.concat({'videobash.com'}, ',')
30 end
32 -- Parse the media properties.
33 function parse(qargs)
34 local p = quvi.http.fetch(qargs.input_url).data
36 qargs.duration_ms = tonumber(p:match('duration=(%d+)') or 0)*1000
38 qargs.thumb_url = p:match('"og:image" content="(.-)"') or ''
40 qargs.title = p:match('"og:title" content="(.-)"') or ''
42 qargs.id = qargs.input_url:match('%-(%d+)$') or ''
44 qargs.streams = Videobash.iter_streams(p)
46 return qargs
47 end
50 -- Utility functions.
53 function Videobash.can_parse_url(qargs)
54 local U = require 'socket.url'
55 local t = U.parse(qargs.input_url)
56 if t and t.scheme and t.scheme:lower():match('^https?$')
57 and t.host and t.host:lower():match('^www%.videobash%.com$')
58 and t.path and t.path:lower():match('^/video_show/.-%d+$')
59 then
60 return true
61 else
62 return false
63 end
64 end
66 function Videobash.iter_streams(p)
67 local S = require 'quvi/stream'
68 local u = p:match("file=.-'(%w+%..-)'")
69 or error('no match: media stream URL')
70 u = table.concat({'http://', u})
71 return {S.stream_new(u)}
72 end
74 -- vim: set ts=2 sw=2 tw=72 expandtab: