3 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2010-2011 Lionel Elie Mamane <lionel@mamane.lu>
6 -- This file is part of libquvi-scripts <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 CollegeHumor
= {} -- Utility functions unique to this script
26 -- Identify the script.
28 package
.path
= self
.script_dir
.. '/?.lua'
29 local C
= require
'quvi/const'
31 local domains
= {"collegehumor%.com", "dorkly%.com"}
32 r
.domain
= table.concat(domains
, "|")
33 r
.formats
= "default|best"
34 r
.categories
= C
.proto_http
35 local U
= require
'quvi/util'
36 r
.handles
= U
.handles(self
.page_url
, domains
,
37 {"/video[:/]%d+/?", "/embed/%d+"})
42 function query_formats(self
)
43 if CollegeHumor
.redirect_if_embed(self
) then
47 local config
= CollegeHumor
.get_config(self
)
48 local formats
= CollegeHumor
.iter_formats(config
)
51 for k
,v
in pairs(formats
) do
52 table.insert(t
, CollegeHumor
.to_s(v
))
56 self
.formats
= table.concat(t
, "|")
63 self
.host_id
= "collegehumor"
65 if CollegeHumor
.redirect_if_embed(self
) then
69 local c
= CollegeHumor
.get_config(self
)
71 self
.title
= c
:match('<caption>(.-)<')
72 or error("no match: media title")
74 self
.thumbnail_url
= c
:match('<thumbnail><!%[.-%[(.-)%]') or ''
76 local formats
= CollegeHumor
.iter_formats(c
)
77 local U
= require
'quvi/util'
78 local format = U
.choose_format(self
, formats
,
79 CollegeHumor
.choose_best
,
80 CollegeHumor
.choose_default
,
82 or error("unable to choose format")
83 self
.url
= {format.url
or error("no match: media URL")}
91 function CollegeHumor
.redirect_if_embed(self
) -- dorkly embeds YouTube videos
92 if self
.page_url
:match('/embed/%d+') then
93 local p
= quvi
.fetch(self
.page_url
)
94 local s
= p
:match('youtube.com/embed/([%w-_]+)')
96 -- Hand over to youtube.lua
97 self
.redirect_url
= 'http://youtube.com/watch?v=' .. s
104 function CollegeHumor
.get_media_id(self
)
105 local U
= require
'quvi/url'
106 local domain
= U
.parse(self
.page_url
).host
:gsub('^www%.', '', 1)
108 self
.host_id
= domain
:match('^(.+)%.[^.]+$')
109 or error("no match: domain")
111 self
.id
= self
.page_url
:match('/video[/:](%d+)')
112 or error("no match: media ID")
117 function CollegeHumor
.get_config(self
)
118 local domain
= CollegeHumor
.get_media_id(self
)
120 -- quvi normally checks the page URL for a redirection to another
121 -- URL. Disabling this check (QUVIOPT_NORESOLVE) breaks the support
122 -- which is why we do this manually here.
123 local r
= quvi
.resolve(self
.page_url
)
125 -- Make a note of the use of the quvi.resolve returned string.
127 string.format("http://www.%s/moogaloop/video%s%s",
128 domain
, (#r
> 0) and ':' or '/', self
.id
)
130 return quvi
.fetch(config_url
, {fetch_type
='config'})
133 function CollegeHumor
.iter_formats(config
)
134 local sd_url
= config
:match('<file><!%[.-%[(.-)%]')
135 local hq_url
= config
:match('<hq><!%[.-%[(.-)%]')
136 local hq_avail
= (hq_url
and #hq_url
> 0) and 1 or 0
140 local s
= sd_url
:match('%.(%w+)$')
141 table.insert(t
, {quality
='sd', url
=sd_url
, container
=s
})
143 if hq_avail
== 1 and hq_url
then
144 local s
= hq_url
:match('%.(%w+)$')
145 table.insert(t
, {quality
='hq', url
=hq_url
, container
=s
})
151 function CollegeHumor
.choose_best(formats
) -- Assume last is best.
153 for _
,v
in pairs(formats
) do r
= v
end
157 function CollegeHumor
.choose_default(formats
) -- Whatever is found first.
158 for _
,v
in pairs(formats
) do return v
end
161 function CollegeHumor
.to_s(t
)
162 return string.format('%s_%s', t
.container
, t
.quality
)
165 -- vim: set ts=4 sw=4 tw=72 expandtab: