Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / ted.lua
blobf07e1e92773ef4c89c56e8b3cbe5480701bf4f8c
2 -- quvi
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 quvi <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)
42 local formats = Ted.iter_formats(page)
44 local t = {}
45 for _,v in pairs(formats) do
46 table.insert(t, Ted.to_s(v))
47 end
49 table.sort(t)
50 self.formats = table.concat(t, "|")
52 return self
53 end
55 -- Parse video URL.
56 function parse(self)
57 self.host_id = "ted"
58 local page = quvi.fetch(self.page_url)
60 self.id = page:match('ti:"(%d+)"')
61 or error("no match: media id")
63 self.title = page:match('<title>(.-)%s+|')
64 or error("no match: media title")
66 self.thumbnail_url = page:match('&amp;su=(.-)&amp;') or ''
68 local formats = Ted.iter_formats(page)
69 local U = require 'quvi/util'
70 self.url = {U.choose_format(self, formats,
71 Ted.choose_best,
72 Ted.choose_default,
73 Ted.to_s).url
74 or error("no match: media url")}
76 return self
77 end
80 -- Utility functions
83 function Ted.iter_formats(page)
84 local pp = 'http://download.ted.com'
85 local p = 'href="' ..pp.. '(.-)"'
86 local t = {}
87 for u in page:gfind(p) do
88 local c = u:match('%.(%w+)$') or error('no match: container')
89 local q = u:match('%-(%w+)%.%w+$') -- nil is acceptable here
90 u = pp .. u
91 if not Ted.find_duplicate(t,u) then
92 table.insert(t, {url=u, container=c, quality=q})
93 -- print(u,c,q)
94 end
95 end
96 return t
97 end
99 function Ted.find_duplicate(t,u)
100 for _,v in pairs(t) do
101 if v.url == u then return true end
103 return false
106 function Ted.choose_best(formats) -- Last 'mp4' is the 'best'
107 local r = Ted.choose_default(formats)
108 local p = '(%d+)p'
109 for _,v in pairs(formats) do
110 if v.container:match('mp4') then
111 if v.quality then
112 local a = v.quality:match(p)
113 local b = (r.quality) and r.quality:match(p) or 0
114 if a and tonumber(a) > tonumber(b) then
115 r = v
117 else
118 r = v
122 return r
125 function Ted.choose_default(formats) -- First 'mp4' is the 'default'
126 local r -- Return this if mp4 is not found for some reason
127 for _,v in pairs(formats) do
128 if v.container:match('mp4') then
129 return v
131 r = v
133 return r
136 function Ted.to_s(t)
137 return (t.quality)
138 and string.format("%s_%s", t.container, t.quality)
139 or string.format("%s", t.container)
142 -- vim: set ts=4 sw=4 tw=72 expandtab: