media/ted.lua: Rewrite ident function
[libquvi-scripts.git] / share / media / ted.lua
blob2a712320db5e84db7773cd04a62c30c06051b59d
1 -- libquvi-scripts
2 -- Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local Ted = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = Ted.can_parse_url(qargs),
28 domains = table.concat({'ted.com'}, ',')
30 end
32 -- Query available formats.
33 function query_formats(self)
34 self.formats = "default"
35 Ted.is_external(self, quvi.fetch(self.page_url))
36 return self
37 end
39 -- Parse video URL.
40 function parse(self)
41 self.host_id = "ted"
43 local p = quvi.fetch(self.page_url)
45 if Ted.is_external(self, p) then return self end
47 self.id = p:match('ti:"(%d+)"') or error("no match: media ID")
49 self.title = p:match('<title>(.-)%s+|') or error("no match: media title")
51 self.thumbnail_url = p:match('"og:image" content="(.-)"') or ''
53 return self
54 end
57 -- Utility functions
60 function Ted.can_parse_url(qargs)
61 local U = require 'socket.url'
62 local t = U.parse(qargs.input_url)
63 if t and t.scheme and t.scheme:lower():match('^http$')
64 and t.host and t.host:lower():match('^www.ted%.com$')
65 and t.path and t.path:lower():match('^/talks/.+$')
66 then
67 return true
68 else
69 return false
70 end
71 end
73 function Ted.is_external(self, p)
74 self.url = {p:match('(http://download.-)"') or ''}
75 if #self.url[1] ==0 then -- Try the first iframe
76 self.redirect_url = p:match('<iframe src="(.-)"') or ''
77 if #self.redirect_url >0 then
78 return true
79 else
80 error('no match: media stream URL')
81 end
82 end
83 return false
84 end
86 -- vim: set ts=4 sw=4 tw=72 expandtab: