2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local YouTube
= {} -- Utility functions unique to this script
23 -- Identify the script.
25 local Y
= require
'quvi/youtube'
29 -- Parse subtitle properties.
31 local Y
= require
'quvi/youtube'
32 local C
= require
'quvi/const'
34 local u
= Y
.normalize(qargs
.input_url
)
35 local v
= u
:match('v=([%w-_]+)')
36 or error('no match: media ID')
40 YouTube
.tts_get(qargs
, v
, u
, C
)
41 YouTube
.cc_get(qargs
, v
, C
)
50 -- Extract the TTS (text-to-speech, or transcript).
51 function YouTube
.tts_get(qargs
, v
, u
, C
)
52 local u_fmt
= "%s&tlang=%s&type=trackformat=1,&lang=en&kind=asr"
53 local p
= quvi
.http
.fetch(u
).data
55 local tts_url
= p
:match('[\'"]TTS_URL[\'"]:%s+[\'"](.-)[\'"]')
56 if not tts_url
then return end
60 tts_url
= tts_url
:gsub('\\u0026','&')
61 local U
= require
'quvi/util'
62 tts_url
= U
.slash_unescape(tts_url
)
63 tts_url
= U
.unescape(tts_url
)
65 local langs
= tts_url
:match('asr_langs=(.-)&')
66 if not langs
then return end
69 for c
in langs
:gmatch('(%w+)') do
71 url
= string.format(u_fmt
, tts_url
, c
),
79 table.insert(qargs
.subtitles
, {format=C
.sif_tt
, type=C
.st_tts
, lang
=r
})
82 -- Extract the CC (closed-captions) data.
83 function YouTube
.cc_get(qargs
, v
, C
)
85 "http://youtube.com/api/timedtext?hl=en&type=track&v=%s&name=%s&lang=%s"
87 local u
= 'http://video.google.com/timedtext?hl=en&type=list&v=' ..v
88 local x
= quvi
.http
.fetch(u
).data
89 local L
= require
'lxp.lom'
94 if t
[i
].tag == 'track' then
95 local lang
= t
[i
].attr
['lang_code']
97 local name
= t
[i
].attr
['name'] or ''
99 translated
= t
[i
].attr
['lang_translated'] or '',
100 original
= t
[i
].attr
['lang_original'] or '',
101 url
= string.format(u_fmt
, v
, name
, lang
),
109 table.insert(qargs
.subtitles
, {format=C
.sif_tt
, type=C
.st_cc
, lang
=r
})
112 -- vim: set ts=2 sw=2 tw=72 expandtab: