FIX: website/funnyordie.lua: Media stream pattern (PORTpt9)
[libquvi-scripts.git] / share / lua / website / funnyordie.lua
bloba9939af52b7bf62006c0f402908886dce52b07bd
2 -- libquvi-scripts
3 -- Copyright (C) 2011,2013 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2010 quvi project
5 --
6 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
7 --
8 -- This library is free software; you can redistribute it and/or
9 -- modify it under the terms of the GNU Lesser General Public
10 -- License as published by the Free Software Foundation; either
11 -- version 2.1 of the License, or (at your option) any later version.
13 -- This library is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 -- Lesser General Public License for more details.
18 -- You should have received a copy of the GNU Lesser General Public
19 -- License along with this library; if not, write to the Free Software
20 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 -- 02110-1301 USA
24 local FunnyOrDie = {} -- Utility functions unique to this script
26 -- Identify the script.
27 function ident(self)
28 package.path = self.script_dir .. '/?.lua'
29 local C = require 'quvi/const'
30 local r = {}
31 r.domain = "funnyordie%.com"
32 r.formats = "default"
33 r.categories = C.proto_http
34 local U = require 'quvi/util'
35 r.handles = U.handles(self.page_url, {r.domain}, {"/videos/"})
36 return r
37 end
39 -- Query available formats.
40 function query_formats(self)
41 local page = quvi.fetch(self.page_url)
42 local formats = FunnyOrDie.iter_formats(page)
44 local t = {}
45 for _,v in pairs(formats) do
46 table.insert(t, FunnyOrDie.to_s(v))
47 end
49 table.sort(t)
50 self.formats = table.concat(t, "|")
52 return self
53 end
55 -- Parse media URL.
56 function parse(self)
57 self.host_id = "funnyordie"
58 local page = quvi.fetch(self.page_url)
60 self.title = page:match('"og:title" content="(.-)">')
61 or error ("no match: media title")
63 self.id = page:match('key:%s+"(.-)"')
64 or error ("no match: media ID")
66 self.thumbnail_url = page:match('"og:image" content="(.-)"') or ''
68 local formats = FunnyOrDie.iter_formats(page)
69 local U = require 'quvi/util'
70 local format = U.choose_format(self, formats,
71 FunnyOrDie.choose_best,
72 FunnyOrDie.choose_default,
73 FunnyOrDie.to_s)
74 or error("unable to choose format")
75 self.url = {format.url or error('no match: media stream URL')}
76 return self
77 end
80 -- Utility functions
83 function FunnyOrDie.iter_formats(page)
84 local t = {}
85 for u in page:gmatch('type: "video/mp4", src: "(.-)"') do
86 table.insert(t, u)
87 end
88 for u in page:gmatch('source src="(.-)"') do table.insert(t,u) end
89 if #t ==0 then error('no match: media stream URL') end
90 local r = {}
91 for _,u in pairs(t) do
92 local q,c = u:match('/(%w+)%.(%w+)$')
93 if q and c then
94 table.insert(r, {url=u, quality=q, container=c})
95 end
96 end
97 return r
98 end
100 function FunnyOrDie.choose_best(formats)
101 return FunnyOrDie.choose_default(formats)
104 function FunnyOrDie.choose_default(formats)
105 return formats[1]
108 function FunnyOrDie.to_s(t)
109 return string.format("%s_%s", t.container, t.quality)
112 -- vim: set ts=4 sw=4 tw=72 expandtab: