Merge branch 'tg/next/1.0' into next
[libquvi-scripts.git] / share / lua / website / ted.lua
1
2 -- libquvi-scripts
3 -- Copyright (C) 2012  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.
12 --
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.
17 --
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
22 --
23
24 local Ted = {} -- Utility functions unique to this script
25
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
38
39 -- Query available formats.
40 function query_formats(self)
41     self.formats  = "default"
42     Ted.is_external(self, quvi.fetch(self.page_url))
43     return self
44 end
45
46 -- Parse video URL.
47 function parse(self)
48     self.host_id = "ted"
49     local p = quvi.fetch(self.page_url)
50
51     if Ted.is_external(self, p) then
52         return self
53     end
54
55     self.id = p:match('ti:"(%d+)"')
56                 or error("no match: media ID")
57
58     self.title = p:match('<title>(.-)%s+|')
59                     or error("no match: media title")
60
61     self.thumbnail_url = p:match('rel="image_src" href="(.-)"') or ''
62
63     self.url = {p:match('(http://download.-)"')
64                   or error("no match: media stream URL")}
65     return self
66 end
67
68 --
69 -- Utility functions
70 --
71
72 function Ted.is_external(self, page)
73     -- Some of the videos are hosted elsewhere.
74     self.redirect_url = page:match('name="movie"%s+value="(.-)"') or ''
75     return #self.redirect_url > 0
76 end
77
78 -- vim: set ts=4 sw=4 tw=72 expandtab: