media/bikeradar.lua: Relicense under AGPLv3+
[libquvi-scripts.git] / share / media / funnyordie.lua
blobd50eb7c97d90e7684aa79cc5c368bbf81b1bc3aa
1 -- libquvi-scripts
2 -- Copyright (C) 2011,2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2010 quvi project
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 FunnyOrDie = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = FunnyOrDie.can_parse_url(qargs),
28 domains = table.concat({'funnyordie.com'}, ',')
30 end
32 -- Parse media properties.
33 function parse(qargs)
34 local p = quvi.http.fetch(qargs.input_url).data
36 qargs.thumb_url = p:match('"og:image" content="(.-)"') or ''
37 qargs.title = p:match('"og:title" content="(.-)">') or ''
38 qargs.id = p:match('key:%s+"(.-)"') or ''
40 qargs.streams = FunnyOrDie.iter_streams(p)
42 return qargs
43 end
46 -- Utility functions
49 function FunnyOrDie.can_parse_url(qargs)
50 local U = require 'socket.url'
51 local t = U.parse(qargs.input_url)
52 if t and t.scheme and t.scheme:lower():match('^http$')
53 and t.host and t.host:lower():match('funnyordie%.com$')
54 and t.path and t.path:lower():match('^/videos/%w+')
55 then
56 return true
57 else
58 return false
59 end
60 end
62 function FunnyOrDie.iter_streams(p)
63 local t = {}
64 for u in p:gmatch('source src="(.-)"') do table.insert(t,u) end
65 -- There should be at least two stream URLs at this point.
66 -- first: the (playlist) URL for the segmented videos (unusable to us)
67 -- ...: the media stream URLs
68 if #t <2 then error('no match: media stream URL') end
69 table.remove(t,1) -- Remove the first stream URL.
71 local S = require 'quvi/stream'
72 local r = {}
74 -- nostd is a dictionary used by this script only. libquvi ignores it.
75 for _,u in pairs(t) do
76 local q,c = u:match('/(%w+)%.(%w+)$')
77 local s = S.stream_new(u)
78 s.nostd = {quality=q}
79 s.container = c
80 s.id = FunnyOrDie.to_id(s)
81 table.insert(r,s)
82 end
84 if #r >1 then
85 FunnyOrDie.ch_best(r)
86 end
88 return r
89 end
91 function FunnyOrDie.ch_best(t)
92 t[1].flags.best = true
93 end
95 function FunnyOrDie.to_id(t)
96 return string.format("%s_%s", t.nostd.quality, t.container)
97 end
99 -- vim: set ts=2 sw=2 tw=72 expandtab: