3 -- Copyright (C) 2011 Toni Gundogdu
4 -- Copyright (C) 2010 quvi project
6 -- This file is part of quvi <http://quvi.sourceforge.net/>.
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
24 local FunnyOrDie
= {} -- Utility functions unique to this script
26 -- Identify the script.
28 package
.path
= self
.script_dir
.. '/?.lua'
29 local C
= require
'quvi/const'
31 r
.domain
= "funnyordie.com"
33 r
.categories
= C
.proto_http
34 local U
= require
'quvi/util'
35 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, {"/videos/"})
39 -- Query available formats.
40 function query_formats(self
)
41 local page
= quvi
.fetch(self
.page_url
)
42 local formats
= FunnyOrDie
.iter_formats(page
)
45 for _
,v
in pairs(formats
) do
46 table.insert(t
, FunnyOrDie
.to_s(v
))
50 self
.formats
= table.concat(t
, "|")
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
,
74 or error('no match: media url')}
82 function FunnyOrDie
.iter_formats(page
)
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
})
92 function FunnyOrDie
.choose_best(formats
) -- Last is 'best'
93 local r
= FunnyOrDie
.choose_default(formats
)
94 for _
,v
in pairs(formats
) do
100 function FunnyOrDie
.choose_default(formats
) -- First is 'default'
101 for _
,v
in pairs(formats
) do
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: