Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / quvi / util.lua
blob99e33a65dfead173059af42afdbbb353d03ed925
2 -- quvi
3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of quvi <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 M = {}
25 -- http://www.lua.org/pil/20.3.html
26 function M.decode (s)
27 r = {}
28 for n,v in s:gfind ("([^&=]+)=([^&=]+)") do
29 n = M.unescape (n)
30 r[n] = v
31 end
32 return r
33 end
35 -- http://www.lua.org/pil/20.3.html
36 function M.unescape (s)
37 s = s:gsub ('+', ' ')
38 s = s:gsub ('%%(%x%x)', function (h)
39 return string.char (tonumber (h, 16))
40 end)
41 return s
42 end
44 function M.slash_unescape (s)
45 s = s:gsub ('\\(.)', '%1')
46 return s
47 end
49 -- handles
50 -- Check whether a website script can "handle" the specified URL
51 -- Params:
52 -- url .. video page URL
53 -- domains .. table of domain names
54 -- paths .. table of URL path patterns to match
55 -- queries .. table of URL query patterns to match
56 function M.handles(url, domains, paths, queries)
57 if not url or not domains then
58 return false
59 end
60 local U = require 'quvi/url'
61 local t = U.parse(url)
62 -- for k,v in pairs(t) do print(k,v) end
63 local r = M.contains(t.host, domains)
64 if r then
65 if paths then
66 r = M.contains(t.path, paths)
67 end
68 if r then
69 if queries then
70 if t.query then
71 r = M.contains(t.query, queries)
72 else
73 r = false
74 end
75 end
76 end
77 end
78 return r
79 end
81 function M.contains(s,t)
82 if not s then return false end
83 for k,v in pairs(t) do
84 if s:find(v) then return true end
85 end
86 return false
87 end
89 function M.ends(s,e) -- http://lua-users.org/wiki/StringRecipes
90 return e == '' or s:sub(-#e) == e
91 end
93 function M.is_higher_quality(a,b)
94 if a.height > b.height then
95 if a.width > b.width then
96 if a['bitrate'] then -- Optional: bitrate
97 if a.bitrate > b.bitrate then
98 return true
99 end
100 else
101 return true
105 return false
108 function M.is_lower_quality(a,b)
109 if a.height < b.height then
110 if a.width < b.width then
111 if a['bitrate'] then -- Optiona: bitrate
112 if a.bitrate < b.bitrate then
113 return true
115 else
116 return true
120 return false
123 function M.choose_format(self,
124 formats,
125 callback_choose_best,
126 callback_choose_default,
127 callback_to_s)
128 local r_fmt = self.requested_format
130 if r_fmt == 'best' then
131 return callback_choose_best(formats)
134 for _,v in pairs(formats) do
135 if r_fmt == callback_to_s(v) then
136 return v
140 return callback_choose_default(formats)
143 function M.to_timestamp(s) -- Based on <http://is.gd/ee9ZTD>
144 local p = "%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+)"
146 local d,m,y,hh,mm,ss = s:match(p)
147 if not d then error('no match: date') end
149 local MON = {Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8,
150 Sep=9, Oct=10, Nov=11, Dec=12}
152 local m = MON[m]
154 local offset = os.time() - os.time(os.date("!*t"))
156 return os.time({day=d,month=m,year=y,
157 hour=hh,min=mm,sec=ss}) + offset
160 return M
162 -- vim: set ts=4 sw=4 tw=72 expandtab: