share/Makefile.am: Reorganize blocks, comments
[libquvi-scripts.git] / share / lua / common / quvi / accepts.lua
blob3311c98553f2f8df5042ab7a35da9ebb84d289e8
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 Check whether a media script accepts the URL
26 Parameters:
27 url .. video page URL
28 domains .. table of domain names
29 paths .. table of URL path patterns to match
30 queries .. table of URL query patterns to match
31 Returns:
32 true if media scripts accepts the URL, otheriwise false
33 ]]--
34 function M.accepts(url, domains, paths, queries)
36 if not url or not domains then
37 return {accepts=false, domains={}}
38 end
40 local U = require 'quvi/util'
41 local R = require 'quvi/url'
43 local t = R.parse(url)
44 local a = U.match_any(domains, t.host)
45 -- for k,v in pairs(t) do print(k,v) end
47 if a then
49 if paths then
50 a = U.match_any(paths, t.path)
51 end -- if paths
53 if a then
54 if queries then
55 if t.query then
56 a = U.match_any(queries, t.query)
57 else
58 a = false
59 end
60 end -- if queries
61 end -- if a
63 end -- if a
65 for _,v in pairs(domains) do
66 v = v:gsub('%%w%+', 'com') -- Naive replacement.
67 v = v:gsub('%%', '') -- Strip any remaining Lua escape characters.
68 v = v:gsub('%s', '') -- Strip any whitespace.
69 table.insert(t, v)
70 end
72 return {accepts=a, domains=table.concat(t,',')}
73 end
75 --[[
76 package.path = package.path .. ';../?.lua'
78 local function dump(r)
79 print('--')
80 for k,v in pairs(r) do print(k,v) end
81 end
83 local function foo()
84 -- should return true
85 dump(M.accepts('http://example.com', {'example%.%w+'}))
86 -- should return false
87 dump(M.accepts('http://example.com', {'examle%.%w+'}))
88 end
90 local function bar()
91 local d = {'example.%w+', 'foo.bar'}
92 -- should return true
93 dump(M.accepts('http://example.com/foo/bar', d, {'/foo/'}))
94 -- should return true
95 dump(M.accepts('http://foo.bar/baz/1234_fubar', d, {'/baz/'}))
96 -- should return false
97 dump(M.accepts('http://foobar/baz/1234_fubar', d, {'/baz/'}))
98 end
100 foo()
101 bar()
102 ]]--
104 return M
106 -- vim: set ts=2 sw=2 tw=72 expandtab: