2 -- Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Bastien Nocera <hadess@hadess.net>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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.
27 can_parse_url
= Ted
.can_parse_url(qargs
),
28 domains
= table.concat({'ted.com'}, ',')
32 -- Parse the media properties.
34 local p
= quvi
.http
.fetch(qargs
.input_url
).data
36 if not Ted
.chk_ext(qargs
, p
) then
37 qargs
.title
= p
:match('<title>(.-)%s+|') or ''
47 function Ted
.can_parse_url(qargs
)
48 local U
= require
'socket.url'
49 local t
= U
.parse(qargs
.input_url
)
50 if t
and t
.scheme
and t
.scheme
:lower():match('^http$')
51 and t
.host
and t
.host
:lower():match('^www.ted%.com$')
52 and t
.path
and t
.path
:lower():match('^/talks/.+$')
60 function Ted
.chk_ext(qargs
, p
)
61 Ted
.iter_streams(qargs
, p
)
62 if #qargs
.streams
>0 then -- Self-hosted media.
64 else -- External media. Try the first iframe.
65 qargs
.goto_url
= p
:match('<iframe src="(.-)"') or ''
66 if #qargs
.goto_url
>0 then
69 error('no match: media stream URL')
74 function Ted
.iter_streams(qargs
, p
)
77 local d
= p
:match('talkDetails%s+=%s+(.-)<') or ''
78 if #d
==0 then return end
80 local S
= require
'quvi/stream'
81 local J
= require
'json'
84 Ted
.rtmp_streams(qargs
, S
, J
, j
)
85 Ted
.html_streams(qargs
, S
, j
)
87 qargs
.duration_ms
= tonumber(j
['duration'] or 0) * 1000
88 qargs
.thumb_url
= j
['posterUrl'] or ''
89 qargs
.id
= j
['id'] or ''
91 if #qargs
.streams
>1 then
96 function Ted
.html_streams(qargs
, S
, j
)
97 local h
= j
['htmlStreams']
99 local u
= h
[i
]['file']
100 local t
= S
.stream_new(u
)
101 t
.video
.bitrate_kbit_s
= tonumber(u
:match('(%d+)k') or 0)
102 t
.container
= u
:match('%d+k%.(%w+)') or ''
103 t
.id
= Ted
.html_stream_id(h
[i
]['id'], t
)
104 table.insert(qargs
.streams
, t
)
108 function Ted
.rtmp_streams(qargs
, S
, J
, j
)
109 local U
= require
'quvi/util'
110 local l
= J
.decode(U
.unescape(j
['flashVars']['playlist']))
112 for _
,v
in pairs(l
['talks']) do
113 local s
= v
['streamer'] or error('no match: streamer')
115 for _
,vv
in pairs(v
['resource']) do
116 local u
= table.concat({s
,vv
['file']:match('^%w+:(.-)$')},'/')
117 local t
= S
.stream_new(u
)
119 t
.video
.bitrate_kbit_s
= tonumber(vv
['bitrate'] or 0)
120 t
.video
.height
= tonumber(vv
['height'] or 0)
121 t
.video
.width
= tonumber(vv
['width'] or 0)
123 t
.container
= vv
['file']:match('^(%w+):') or ''
124 t
.id
= Ted
.rtmp_stream_id(t
)
126 table.insert(qargs
.streams
, t
)
131 function Ted
.html_stream_id(id
, t
)
132 return string.format('%s_%s_%sk', id
, t
.container
, t
.video
.bitrate_kbit_s
)
135 function Ted
.rtmp_stream_id(t
)
136 return string.format('%s_%dk_%dp',
137 t
.container
, t
.video
.bitrate_kbit_s
, t
.video
.height
)
140 function Ted
.ch_best(qargs
, S
)
141 local r
= qargs
.streams
[1] -- Make the first one the 'best' by default.
143 for _
,v
in pairs(qargs
.streams
) do
144 if v
.video
.height
> r
.video
.height
then
150 -- vim: set ts=2 sw=2 tw=72 expandtab: