Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / util / resolve_redirections.lua
blob0e075ce9cf4687e0f157e49e2c7db2945c3dbc73
1 -- libquvi-scripts
2 -- Copyright (C) 2012-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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 ResolveExceptions = {} -- Utility functions unique to this script
23 function resolve_redirections(qargs)
24 --[[
26 UPDATE (2013-10-07)
28 libquvi will always attempt to resolve URL redirections. This means
29 that quvi_media_new (for example) will always resolve first and only
30 then pass the input URL to the media scripts to determine the support.
32 Problem:
34 - g00gle now redirects the embedded media URLs to the API pages
36 - This causes libquvi to return the "no support" because the
37 destination URL looks nothing like a typical media page URL
39 Solution:
41 - "normalize" (quvi/youtube) the input URL before we try to resolve
42 URL redirections
44 - "normalize" will convert the embedded media URLs to media page URLs,
45 which can then be passed to libquvi/libcurl for URL resolving
47 Notes:
49 - quvi_supports function skips resolving altogether unless online
50 check is forced
52 - This is the reason "normalize" must still be called in "ident"
53 function of the media script, even if we do so here
55 ]]--
56 local Y = require 'quvi/youtube'
57 local u = Y.normalize(qargs.input_url)
59 -- Have libcurl resolve the URL redirections.
60 local r = quvi.http.resolve(u)
61 if #r.resolved_url ==1 then return u end
63 -- Apply any exception rules to the destination URL.
64 return ResolveExceptions.YouTube(qargs, r.resolved_url)
65 end
68 -- Utility functions
71 function ResolveExceptions.YouTube(qargs, dst)
72 return dst -- UPDATE (2012-11-18): g00gle servers no longer strip the "#t="
74 -- Preserve the #t= (if any) after redirection.
75 -- e.g. http://www.youtube.com/watch?v=LWxTGJ3TK1U#t=2m22s
76 -- -> http://www.youtube.com/watch?v=LWxTGJ3TK1U
78 -- return table.concat({dst, (qargs.input_url:match('(#t=%w+)') or '')})
79 end
81 -- vim: set ts=2 sw=2 tw=72 expandtab: