2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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/>.
24 Check whether a string A ends with string B.
29 true if string A ends with string B.
31 function M
.ends_with(a
, b
) -- http://lua-users.org/wiki/StringRecipes
32 return a
:sub(-#b
) == b
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).
42 true if entity A is the higher quality, otherwise false.
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
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).
64 true if entity A is the lower quality, otherwise false.
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
82 s .. String to tokenize
83 p .. Pattern (e.g. "[%w-_]+")
87 function M
.tokenize(s
, p
)
92 Convert a string to a timestamp.
94 s .. String to convert
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}
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
117 s .. String to decode
121 function M
.decode(s
) -- http://www.lua.org/pil/20.3.html
123 for n
,v
in s
:gmatch ("([^&=]+)=([^&=]+)") do
133 s .. String to unescape
137 function M
.unescape(s
) -- http://www.lua.org/pil/20.3.html
139 return (s
:gsub('%%(%x%x)',
141 return string.char(tonumber(h
, 16))
146 Unescape slashed string.
148 s .. String to unescape
152 function M
.slash_unescape(s
)
153 return (s
:gsub('\\(.)', '%1'))
157 Trim a string removing leading and trailing whitespace.
164 s
= s
:gsub('^%s+(.)', '%1')
165 return s
:gsub('(.)%s+$', '%1')
170 -- vim: set ts=2 sw=2 tw=72 expandtab: