3 -- Copyright (C) 2011 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 config
= CollegeHumor
.get_config(self
)
71 local _
,_
,s
= config
:find('<caption>(.-)<')
72 self
.title
= s
or error("no match: media title")
74 local _
,_
,s
= config
:find('<thumbnail><!%[.-%[(.-)%]')
75 self
.thumbnail_url
= s
or ""
77 local formats
= CollegeHumor
.iter_formats(config
)
78 local U
= require
'quvi/util'
79 local format = U
.choose_format(self
, formats
,
80 CollegeHumor
.choose_best
,
81 CollegeHumor
.choose_default
,
83 or error("unable to choose format")
84 self
.url
= {format.url
or error("no match: media url")}
92 function CollegeHumor
.redirect_if_embed(self
) -- dorkly embeds YouTube videos
93 if self
.page_url
:match('/embed/%d+') then
94 local page
= quvi
.fetch(self
.page_url
)
95 local _
,_
,s
= page
:find('youtube.com/embed/([%w-_]+)')
97 -- Hand over to youtube.lua
98 self
.redirect_url
= 'http://youtube.com/watch?v=' .. s
105 function CollegeHumor
.get_media_id(self
)
106 local R
= require
'quvi/url'
107 local domain
= R
.parse(self
.page_url
).host
:gsub('^www%.', '', 1)
109 _
,_
,self
.host_id
= domain
:find('^(.+)%.[^.]+$')
110 if not self
.host_id
then
111 error("no match: domain")
114 _
,_
,self
.id
= self
.page_url
:find('/video[/:](%d+)')
116 error("no match: media id")
122 function CollegeHumor
.get_config(self
)
123 local domain
= CollegeHumor
.get_media_id(self
)
125 -- quvi normally checks the page URL for a redirection to another
126 -- URL. Disabling this check (QUVIOPT_NORESOLVE) breaks the support
127 -- which is why we do this manually here.
128 local r
= quvi
.resolve(self
.page_url
)
130 -- Make a note of the use of the quvi.resolve returned string.
132 string.format("http://www.%s/moogaloop/video%s%s",
133 domain
, (#r
> 0) and ':' or '/', self
.id
)
135 return quvi
.fetch(config_url
, {fetch_type
='config'})
138 function CollegeHumor
.iter_formats(config
)
139 local _
,_
,sd_url
= config
:find('<file><!%[.-%[(.-)%]')
140 local _
,_
,hq_url
= config
:find('<hq><!%[.-%[(.-)%]')
141 local hq_avail
= (hq_url
and #hq_url
> 0) and 1 or 0
145 local _
,_
,s
= sd_url
:find('%.(%w+)$')
146 table.insert(t
, {quality
='sd', url
=sd_url
, container
=s
})
148 if hq_avail
== 1 and hq_url
then
149 local _
,_
,s
= hq_url
:find('%.(%w+)$')
150 table.insert(t
, {quality
='hq', url
=hq_url
, container
=s
})
156 function CollegeHumor
.choose_best(formats
) -- Assume last is best.
158 for _
,v
in pairs(formats
) do r
= v
end
162 function CollegeHumor
.choose_default(formats
) -- Whatever is found first.
163 for _
,v
in pairs(formats
) do return v
end
166 function CollegeHumor
.to_s(t
)
167 return string.format('%s_%s', t
.container
, t
.quality
)
170 -- vim: set ts=4 sw=4 tw=72 expandtab: