Revise YouTube.normalize
[libquvi-scripts.git] / share / lua / website / youtube.lua
blob4819f1bf860112d795594e607da3839661eb3119
2 -- libquvi-scripts
3 -- Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library 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 GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
23 local YouTube = {} -- Utility functions unique to this script
25 -- <http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs>
27 -- Identify the script.
28 function ident(self)
29 package.path = self.script_dir .. '/?.lua'
30 local C = require 'quvi/const'
31 local r = {}
32 r.domain = "youtube%.com"
33 r.formats = "default|best"
34 r.categories = C.proto_http
35 self.page_url = YouTube.normalize(self.page_url)
36 local U = require 'quvi/util'
37 r.handles = U.handles(self.page_url,
38 {r.domain}, {"/watch"}, {"v=[%w-_]+"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 local config,U = YouTube.get_config(self)
45 local formats = YouTube.iter_formats(config, U)
47 local t = {}
48 for _,v in pairs(formats) do
49 table.insert(t, YouTube.to_s(v))
50 end
52 table.sort(t)
53 self.formats = table.concat(t, "|")
55 return self
56 end
58 -- Parse URL.
59 function parse(self)
60 self.host_id = "youtube"
61 local page_url = YouTube.normalize(self.page_url)
63 local _,_,s = page_url:find('#a?t=(.+)')
64 self.start_time = s or ''
66 return YouTube.get_video_info(self)
67 end
70 -- Utility functions
73 function YouTube.normalize(s)
74 if not s then return s end
75 local U = require 'quvi/url'
76 local t = U.parse(s)
77 t.host = t.host:gsub('youtu%.be', 'youtube.com')
78 t.host = t.host:gsub('-nocookie', '')
79 if t.path then
80 local p = {'/embed/([-_%w]+)', '/%w/([-_%w]+)', '/([-_%w]+)'}
81 for _,v in pairs(p) do
82 local m = t.path:match(v)
83 if m and not t.query then
84 t.query = 'v=' .. m
85 t.path = '/watch'
86 end
87 end
88 end
89 return U.build(t)
90 end
92 function YouTube.get_config(self)
93 local _,_,s = self.page_url:find('^(%w+)://')
94 local scheme = s or error("no match: scheme")
96 local page_url = YouTube.normalize(self.page_url)
98 local _,_,s = page_url:find("v=([%w-_]+)")
99 self.id = s or error("no match: media id")
101 local s_fmt = "%s://www.youtube.com/get_video_info?&video_id=%s"
102 .. "&el=detailpage&ps=default&eurl=&gl=US&hl=en"
104 local config_url = string.format(s_fmt, scheme, self.id)
106 local U = require 'quvi/util'
107 local config = U.decode(quvi.fetch(config_url, {fetch_type='config'}))
109 if config['reason'] then
110 local reason = U.unescape(config['reason'])
111 local code = config['errorcode']
112 error(string.format("%s (code=%s)", reason, code))
115 return config,U
118 function YouTube.iter_formats(config, U)
119 local fmt_stream_map = config['url_encoded_fmt_stream_map']
120 or error("no match: url_encoded_fmt_stream_map")
122 fmt_stream_map = U.unescape(fmt_stream_map) .. ","
124 local urls = {}
125 for f in fmt_stream_map:gfind('([^,]*),') do
126 local d = U.decode(f)
127 if d['itag'] and d['url'] then
128 urls[U.unescape(d['itag'])] = U.unescape(d['url'])
132 local fmt_map = config['fmt_list'] or error("no match: fmt_list")
133 fmt_map = U.unescape(fmt_map)
135 local r = {}
136 for f,w,h in fmt_map:gfind('(%d+)/(%d+)x(%d+)') do
137 -- print(f,w,h)
138 table.insert(r, {fmt_id=tonumber(f), url=urls[f],
139 width=tonumber(w), height=tonumber(h)})
142 return r
145 function YouTube.get_video_info(self)
146 local config,U = YouTube.get_config(self)
148 self.title = config['title'] or error('no match: media title')
149 self.title = U.unescape(self.title)
151 self.thumbnail_url = config['thumbnail_url'] or ''
152 if #self.thumbnail_url > 0 then
153 self.thumbnail_url = U.unescape(self.thumbnail_url)
156 self.duration = (config['length_seconds'] or 0)*1000 -- to msec
158 self.requested_format =
159 YouTube.convert_deprecated_id(self.requested_format)
161 local formats = YouTube.iter_formats(config, U)
162 local url = U.choose_format(self, formats,
163 YouTube.choose_best,
164 YouTube.choose_default,
165 YouTube.to_s).url
166 or error("no match: media url")
168 if url and #self.start_time > 0 then
169 local min, sec = self.start_time:match("^(%d+)m(%d+)s$")
170 min = tonumber(min) or 0
171 sec = tonumber(sec) or 0
172 local msec = (min * 60000) + (sec * 1000)
173 if msec > 0 then
174 url = url .. "&begin=" .. msec
178 self.url = {url}
180 return self
183 function YouTube.choose_best(formats) -- Highest quality available
184 local r = {width=0, height=0, url=nil}
185 local U = require 'quvi/util'
186 for _,v in pairs(formats) do
187 if U.is_higher_quality(v,r) then
188 r = v
191 -- for k,v in pairs(r) do print(k,v) end
192 return r
195 function YouTube.choose_default(formats) -- Lowest quality available
196 local r = {width=0xffff, height=0xffff, url=nil}
197 local U = require 'quvi/util'
198 for _,v in pairs(formats) do
199 if U.is_lower_quality(v,r) then
200 r = v
203 -- for k,v in pairs(r) do print(k,v) end
204 return r
207 YouTube.conv_table = { -- Deprecated.
208 -- flv
209 flv_240p = '5',
210 flv_360p = '34',
211 flv_480p = '35',
212 -- mp4
213 mp4_360p = '18',
214 mp4_720p = '22',
215 mp4_1080p = '37',
216 mp4_3072p = '38'
219 function YouTube.convert_deprecated_id(r_fmt)
220 if YouTube.conv_table[r_fmt] then
221 local s = string.format("fmt%02d_", YouTube.conv_table[r_fmt])
222 r_fmt = r_fmt:gsub("^(%w+)_", s)
224 return r_fmt
227 function YouTube.to_s(t)
228 return string.format("fmt%02d_%sp", t.fmt_id, t.height)
231 --[[
232 local a = {
233 {u='http://youtu.be/3WSQH__H1XE', -- u=page url
234 e='http://youtube.com/watch?v=3WSQH__H1XE'}, -- e=expected url
235 {u='http://youtu.be/watch?v=3WSQH__H1XE',
236 e='http://youtube.com/watch?v=3WSQH__H1XE'},
237 {u='http://youtu.be/embed/3WSQH__H1XE',
238 e='http://youtube.com/watch?v=3WSQH__H1XE'},
239 {u='http://youtu.be/v/3WSQH__H1XE',
240 e='http://youtube.com/watch?v=3WSQH__H1XE'},
241 {u='http://youtu.be/e/3WSQH__H1XE',
242 e='http://youtube.com/watch?v=3WSQH__H1XE'},
243 {u='http://youtube.com/watch?v=3WSQH__H1XE',
244 e='http://youtube.com/watch?v=3WSQH__H1XE'},
245 {u='http://youtube.com/embed/3WSQH__H1XE',
246 e='http://youtube.com/watch?v=3WSQH__H1XE'},
247 {u='http://jp.youtube.com/watch?v=3WSQH__H1XE',
248 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
249 {u='http://jp.youtube-nocookie.com/e/3WSQH__H1XE',
250 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
251 {u='http://jp.youtube.com/embed/3WSQH__H1XE',
252 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
253 {u='http://youtube.com/3WSQH__H1XE', -- invalid page url
254 e='http://youtube.com/watch?v=3WSQH__H1XE'}
256 local e = 0
257 for i,v in pairs(a) do
258 local s = YouTube.normalize(v.u)
259 if s ~= v.e then
260 print('\n input: ' .. v.u .. " (#" .. i .. ")")
261 print('expected: ' .. v.e)
262 print(' got: ' .. s)
263 e = e + 1
266 print('\nerrors: ' .. e)
267 ]]--
269 -- vim: set ts=4 sw=4 tw=72 expandtab: