Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / funnyordie.lua
blob3d7bd1c04f049ccc4fbac2640410d2135d096b3f
2 -- quvi
3 -- Copyright (C) 2011 Toni Gundogdu
4 -- Copyright (C) 2010 quvi project
5 --
6 -- This file is part of quvi <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 self.url = {U.choose_format(self, formats,
71 FunnyOrDie.choose_best,
72 FunnyOrDie.choose_default,
73 FunnyOrDie.to_s).url
74 or error('no match: media url')}
75 return self
76 end
79 -- Utility functions
82 function FunnyOrDie.iter_formats(page)
83 local t = {}
84 for u in page:gfind("'src',%s+'(.-)'") do
85 local q,c = u:match('(%w+)%.(%w+)$')
86 table.insert(t, {url=u, quality=q, container=c})
87 -- print(u,c)
88 end
89 return t
90 end
92 function FunnyOrDie.choose_best(formats) -- Last is 'best'
93 local r = FunnyOrDie.choose_default(formats)
94 for _,v in pairs(formats) do
95 r = v
96 end
97 return r
98 end
100 function FunnyOrDie.choose_default(formats) -- First is 'default'
101 for _,v in pairs(formats) do
102 return v
106 function FunnyOrDie.to_s(t)
107 return string.format("%s_%s", t.container, t.quality)
110 -- vim: set ts=4 sw=4 tw=72 expandtab: