Add IMDb support (#88)
[libquvi-scripts.git] / share / lua / website / collegehumor.lua
blob0e7f23a2e014bd34d9303c43d77c5b14e0ee847d
2 -- libquvi-scripts
3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2010-2011 Lionel Elie Mamane <lionel@mamane.lu>
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 CollegeHumor = {} -- 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 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+"})
38 return r
39 end
41 -- Query formats.
42 function query_formats(self)
43 if CollegeHumor.redirect_if_embed(self) then
44 return self
45 end
47 local config = CollegeHumor.get_config(self)
48 local formats = CollegeHumor.iter_formats(config)
50 local t = {}
51 for k,v in pairs(formats) do
52 table.insert(t, CollegeHumor.to_s(v))
53 end
55 table.sort(t)
56 self.formats = table.concat(t, "|")
58 return self
59 end
61 -- Parse media URL.
62 function parse(self)
63 self.host_id = "collegehumor"
65 if CollegeHumor.redirect_if_embed(self) then
66 return self
67 end
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,
82 CollegeHumor.to_s)
83 or error("unable to choose format")
84 self.url = {format.url or error("no match: media url")}
85 return self
86 end
89 -- Utility functions
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-_]+)')
96 if s then
97 -- Hand over to youtube.lua
98 self.redirect_url = 'http://youtube.com/watch?v=' .. s
99 return true
102 return false
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+)')
115 if not self.id then
116 error("no match: media id")
119 return domain
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.
131 local config_url =
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
143 local t = {}
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})
153 return t
156 function CollegeHumor.choose_best(formats) -- Assume last is best.
157 local r
158 for _,v in pairs(formats) do r = v end
159 return r
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: