3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
6 -- This file is part of quvi <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 Ted
= {} -- Utility functions unique to this script
26 -- Identify the script.
28 package
.path
= self
.script_dir
.. '/?.lua'
29 local C
= require
'quvi/const'
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/.+$"})
39 -- Query available formats.
40 function query_formats(self
)
41 local page
= quvi
.fetch(self
.page_url
)
42 local formats
= Ted
.iter_formats(page
)
45 for _
,v
in pairs(formats
) do
46 table.insert(t
, Ted
.to_s(v
))
50 self
.formats
= table.concat(t
, "|")
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('&su=(.-)&') or ''
68 local formats
= Ted
.iter_formats(page
)
69 local U
= require
'quvi/util'
70 self
.url
= {U
.choose_format(self
, formats
,
74 or error("no match: media url")}
83 function Ted
.iter_formats(page
)
84 local pp
= 'http://download.ted.com'
85 local p
= 'href="' ..pp
.. '(.-)"'
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
91 if not Ted
.find_duplicate(t
,u
) then
92 table.insert(t
, {url
=u
, container
=c
, quality
=q
})
99 function Ted
.find_duplicate(t
,u
)
100 for _
,v
in pairs(t
) do
101 if v
.url
== u
then return true end
106 function Ted
.choose_best(formats
) -- Last 'mp4' is the 'best'
107 local r
= Ted
.choose_default(formats
)
109 for _
,v
in pairs(formats
) do
110 if v
.container
:match('mp4') 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
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
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: