Add IMDb support (#88)
[libquvi-scripts.git] / share / lua / website / ted.lua
blobb045d7c659d2fe32b557b41bd6053ef619c407ac
2 -- libquvi-scripts
3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
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 Ted = {} -- 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 = "ted%.com"
32 r.formats = "default|best"
33 r.categories = C.proto_http
34 local U = require 'quvi/util'
35 r.handles = U.handles(self.page_url, {r.domain}, {"/talks/.+$"})
36 return r
37 end
39 -- Query available formats.
40 function query_formats(self)
41 local page = quvi.fetch(self.page_url)
43 if Ted.is_external(self, page) then
44 return self
45 end
47 local formats = Ted.iter_formats(page)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Ted.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "|")
56 return self
57 end
59 -- Parse video URL.
60 function parse(self)
61 self.host_id = "ted"
62 local page = quvi.fetch(self.page_url)
64 if Ted.is_external(self, page) then
65 return self
66 end
68 self.id = page:match('ti:"(%d+)"')
69 or error("no match: media id")
71 self.title = page:match('<title>(.-)%s+|')
72 or error("no match: media title")
74 self.thumbnail_url = page:match('rel="image_src" href="(.-)"') or ''
76 local formats = Ted.iter_formats(page)
77 local U = require 'quvi/util'
78 local format = U.choose_format(self, formats,
79 Ted.choose_best,
80 Ted.choose_default,
81 Ted.to_s)
82 or error("unable to choose format")
83 self.url = {format.url or error("no match: media url")}
84 return self
85 end
88 -- Utility functions
91 function Ted.iter_formats(page)
92 local pp = 'http://download.ted.com'
93 local p = 'href="' ..pp.. '(.-)"'
94 local t = {}
95 for u in page:gfind(p) do
96 local c = u:match('%.(%w+)$') or error('no match: container')
97 local q = u:match('%-(%w+)%.%w+$') -- nil is acceptable here
98 u = pp .. u
99 if not Ted.find_duplicate(t,u) then
100 table.insert(t, {url=u, container=c, quality=q})
101 -- print(u,c,q)
104 return t
107 function Ted.find_duplicate(t,u)
108 for _,v in pairs(t) do
109 if v.url == u then return true end
111 return false
114 function Ted.choose_best(formats) -- Last 'mp4' is the 'best'
115 local r = Ted.choose_default(formats)
116 local p = '(%d+)p'
117 for _,v in pairs(formats) do
118 if v.container:match('mp4') then
119 if v.quality then
120 local a = v.quality:match(p)
121 local b = (r.quality) and r.quality:match(p) or 0
122 if a and tonumber(a) > tonumber(b) then
123 r = v
125 else
126 r = v
130 return r
133 function Ted.choose_default(formats) -- First 'mp4' is the 'default'
134 local r -- Return this if mp4 is not found for some reason
135 for _,v in pairs(formats) do
136 if v.container:match('mp4') then
137 return v
139 r = v
141 return r
144 function Ted.to_s(t)
145 return (t.quality)
146 and string.format("%s_%s", t.container, t.quality)
147 or string.format("%s", t.container)
150 function Ted.is_external(self, page)
151 -- Some of the videos are hosted elsewhere.
152 self.redirect_url = page:match('name="movie"%s+value="(.-)"') or ''
153 return #self.redirect_url > 0
156 -- vim: set ts=4 sw=4 tw=72 expandtab: