common/: Use socket.url
[libquvi-scripts.git] / share / common / quvi / youtube.lua
blob87fc00ca4c5dd468284322d1a56dc5137994a338
1 -- libquvi-scripts
2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library 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 GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 -- 02110-1301 USA
22 local M = {}
24 --[[
25 "Normalize" URL to YouTube media URL. See the test URLs for examples.
26 Parameters:
27 s .. URL to normalize
28 Returns:
29 Normalized URL
30 ]]--
31 function M.normalize(url)
32 if not url then return url end
34 local U = require 'socket.url'
35 local t = U.parse(url)
37 if not t.host then return url end
39 t.host = t.host:gsub('youtu%.be', 'youtube.com')
40 t.host = t.host:gsub('-nocookie', '')
42 if t.path then
43 local p = {'/embed/([-_%w]+)', '/%w/([-_%w]+)', '/([-_%w]+)'}
44 for _,v in pairs(p) do
45 local m = t.path:match(v)
46 if m and #m == 11 then
47 t.query = 'v=' .. m
48 t.path = '/watch'
49 end
50 end
51 end
52 return U.build(t)
53 end
55 --[[
56 Append URL to qargs.media_url if it is unique by comparing video IDs.
57 Parameters:
58 url .. URL to append
59 ]]--
60 function M.append_if_unique(qargs, url)
61 if not url then return end
63 url = M.normalize(url)
65 local U = require 'socket.url'
66 local t = U.parse(url)
68 if not t.host or not t.query then return end
70 local p = 'v=([%w-_]+)'
71 local v = t.query:match(p)
73 for _,u in pairs(qargs.media_url) do
74 local tt = U.parse(u)
75 if tt.query and v == tt.query:match(p) then
76 return -- Found duplicate. Ignore URL.
77 end
78 end
80 table.insert(qargs.media_url, url)
81 end
83 -- Uncomment to test.
84 --[[
85 package.path = package.path .. ';../?.lua'
86 local a = {
87 {u='http://youtu.be/3WSQH__H1XE', -- u=page url
88 e='http://youtube.com/watch?v=3WSQH__H1XE'}, -- e=expected url
89 {u='http://youtu.be/v/3WSQH__H1XE?hl=en',
90 e='http://youtube.com/watch?v=3WSQH__H1XE'},
91 {u='http://youtu.be/watch?v=3WSQH__H1XE',
92 e='http://youtube.com/watch?v=3WSQH__H1XE'},
93 {u='http://youtu.be/embed/3WSQH__H1XE',
94 e='http://youtube.com/watch?v=3WSQH__H1XE'},
95 {u='http://youtu.be/v/3WSQH__H1XE',
96 e='http://youtube.com/watch?v=3WSQH__H1XE'},
97 {u='http://youtu.be/e/3WSQH__H1XE',
98 e='http://youtube.com/watch?v=3WSQH__H1XE'},
99 {u='http://youtube.com/watch?v=3WSQH__H1XE',
100 e='http://youtube.com/watch?v=3WSQH__H1XE'},
101 {u='http://youtube.com/embed/3WSQH__H1XE',
102 e='http://youtube.com/watch?v=3WSQH__H1XE'},
103 {u='http://jp.youtube.com/watch?v=3WSQH__H1XE',
104 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
105 {u='http://jp.youtube-nocookie.com/e/3WSQH__H1XE',
106 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
107 {u='http://jp.youtube.com/embed/3WSQH__H1XE',
108 e='http://jp.youtube.com/watch?v=3WSQH__H1XE'},
109 {u='http://youtube.com/3WSQH__H1XE', -- invalid page url
110 e='http://youtube.com/watch?v=3WSQH__H1XE'}
112 local e = 0
113 for i,v in pairs(a) do
114 local s = M.normalize(v.u)
115 if s ~= v.e then
116 print('\n input: ' .. v.u .. " (#" .. i .. ")")
117 print('expected: ' .. v.e)
118 print(' got: ' .. s)
119 e = e + 1
122 print((e == 0) and 'Tests OK' or ('\nerrors: ' .. e))
123 ]]--
125 return M
127 -- vim: set ts=2 sw=2 tw=72 expandtab: