3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of quvi <http://quvi.sourceforge.net/>.
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
25 -- http://www.lua.org/pil/20.3.html
28 for n
,v
in s
:gfind ("([^&=]+)=([^&=]+)") do
35 -- http://www.lua.org/pil/20.3.html
36 function M
.unescape (s
)
38 s
= s
:gsub ('%%(%x%x)', function (h
)
39 return string.char (tonumber (h
, 16))
44 function M
.slash_unescape (s
)
45 s
= s
:gsub ('\\(.)', '%1')
50 -- Check whether a website script can "handle" the specified URL
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
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
)
66 r
= M
.contains(t
.path
, paths
)
71 r
= M
.contains(t
.query
, queries
)
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
89 function M
.ends(s
,e
) -- http://lua-users.org/wiki/StringRecipes
90 return e
== '' or s
:sub(-#e
) == e
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
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
123 function M
.choose_format(self
,
125 callback_choose_best
,
126 callback_choose_default
,
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
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}
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
162 -- vim: set ts=4 sw=4 tw=72 expandtab: