common/quvi/: Change license header
[libquvi-scripts.git] / share / common / quvi / util.lua
blob2a09dfee700fdaf4cc92aff32b95d49a84c1bd9c
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2012 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 M = {}
23 --[[
24 Check whether a string A ends with string B.
25 Parameters:
26 a .. String A
27 b .. String B
28 Returns:
29 true if string A ends with string B.
30 ]]--
31 function M.ends(a, b) -- http://lua-users.org/wiki/StringRecipes
32 return a:sub(-#b) == b
33 end
35 --[[
36 Compare quality properties of two media entities. Compares the height, then
37 the width, followed by the bitrate property comparison (if it is set).
38 Parameters:
39 a .. Media entity A
40 b .. Media entity B
41 Returns:
42 true if entity A is the higher quality, otherwise false.
43 ]]--
44 function M.is_higher_quality(a, b)
45 if a.height > b.height then
46 if a.width > b.width then
47 if a['bitrate'] then -- Optional
48 if a.bitrate > b.bitrate then return true end
49 else
50 return true
51 end
52 end
53 end
54 return false
55 end
57 --[[
58 Compare quality properties of two media entities. Compares the height, then
59 the width, followed by the bitrate property comparison (if it is set).
60 Parameters:
61 a .. Media entity A
62 b .. Media entity B
63 Returns:
64 true if entity A is the lower quality, otherwise false.
65 ]]--
66 function M.is_lower_quality(a, b)
67 if a.height < b.height then
68 if a.width < b.width then
69 if a['bitrate'] then -- Optional
70 if a.bitrate < b.bitrate then return true end
71 else
72 return true
73 end
74 end
75 end
76 return false
77 end
79 --[[
80 Tokenize a string.
81 Parameters:
82 s .. String to tokenize
83 p .. Pattern (e.g. "[%w-_]+")
84 Returns:
85 An array of tokens.
86 ]]--
87 function M.tokenize(s, p)
88 return s:gmatch(p)
89 end
91 --[[
92 Convert a string to a timestamp.
93 Parameters:
94 s .. String to convert
95 Returns:
96 Converted string.
97 ]]--
98 function M.to_timestamp(s) -- Based on <http://is.gd/ee9ZTD>
99 local p = "%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+)"
101 local d,m,y,hh,mm,ss = s:match(p)
102 if not d then error('no match: date') end
104 local MON = {Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8,
105 Sep=9, Oct=10, Nov=11, Dec=12}
107 local m = MON[m]
108 local offset = os.time() - os.time(os.date("!*t"))
110 return os.time({day=d,month=m,year=y,
111 hour=hh,min=mm,sec=ss}) + offset
114 --[[
115 Decode a string.
116 Parameters:
117 s .. String to decode
118 Returns:
119 Decoded string.
120 ]]--
121 function M.decode(s) -- http://www.lua.org/pil/20.3.html
122 r = {}
123 for n,v in s:gmatch ("([^&=]+)=([^&=]+)") do
124 n = M.unescape(n)
125 r[n] = v
127 return r
130 --[[
131 Unescape a string.
132 Parameters:
133 s .. String to unescape
134 Returns:
135 Unescaped string
136 ]]--
137 function M.unescape(s) -- http://www.lua.org/pil/20.3.html
138 s = s:gsub('+', ' ')
139 return (s:gsub('%%(%x%x)',
140 function(h)
141 return string.char(tonumber(h, 16))
142 end))
145 --[[
146 Unescape slashed string.
147 Parameters:
148 s .. String to unescape
149 Returns:
150 Unescaped string
151 ]]--
152 function M.slash_unescape(s)
153 return (s:gsub('\\(.)', '%1'))
156 return M
158 -- vim: set ts=2 sw=2 tw=72 expandtab: