3 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <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
23 -- Based on get-flash-videos' Blip.pm
24 -- <https://github.com/monsieurvideo/get-flash-videos/>
26 local Blip
= {} -- Utility functions unique to this script.
28 -- Identify the script.
30 package
.path
= self
.script_dir
.. '/?.lua'
31 local C
= require
'quvi/const'
34 r
.formats
= "default|best"
35 r
.categories
= C
.proto_http
36 local U
= require
'quvi/util'
37 r
.handles
= U
.handles(self
.page_url
, {r
.domain
},
38 -- Paths: Room for improvement.
48 function query_formats(self
)
49 local config
= Blip
.get_config(self
)
50 local formats
= Blip
.iter_formats(config
)
53 for _
,v
in pairs(formats
) do
54 table.insert(t
, Blip
.to_s(v
))
58 self
.formats
= table.concat(t
, "|")
66 local config
,U
= Blip
.get_config(self
)
68 local _
,_
,s
= config
:find('media:title>(.-)</')
69 self
.title
= s
or error('no match: media title')
71 local _
,_
,s
= config
:find('<blip:smallThumbnail>(.-)<')
72 self
.thumbnail_url
= s
or ''
74 local formats
= Blip
.iter_formats(config
)
75 local format = U
.choose_format(self
, formats
,
79 or error("unable to choose format")
80 self
.url
= {format.url
or error("no match: media url")}
88 function Blip
.get_config(self
)
89 local U
= require
'quvi/util'
90 self
.page_url
= U
.unescape(self
.page_url
)
92 local _
,_
,id
= self
.page_url
:find('/flash/(%d+)')
94 local r
= quvi
.resolve(self
.page_url
)
97 _
,_
,id
= r
:find('/rss/flash/(%d+)')
102 local page
= quvi
.fetch(self
.page_url
)
103 _
,_
,id
= page
:find('data%-posts%-id="(.-)"')
105 self
.id
= id
or error('no match: media id')
107 local config_url
= 'http://blip.tv/rss/flash/' .. self
.id
109 return quvi
.fetch(config_url
, {fetch_type
= 'config'}), U
112 function Blip
.iter_formats(config
)
113 local p
= '<media:content url="(.-)"'
114 .. '.-blip:role="(.-)"'
116 .. '.-isDefault="(.-)"'
119 for u
,r
,h
,d
,w
in config
:gfind(p
) do
122 r
= r
:gsub('[()]','')
123 w
= (w
~= nil) and tonumber(w
) or 0 -- Some media do not have 'width'
124 h
= (h
~= nil) and tonumber(h
) or 0 -- or 'height', e.g. audio
125 local _
,_
,s
= u
:find('%.(%w+)$')
126 table.insert(t
, {width
=w
, height
=h
, url
=u
, default
=d
,
127 role
=r
, container
=s
})
132 function Blip
.choose_best(formats
) -- Highest quality available
133 local r
= {width
=0, height
=0, url
=nil}
134 local U
= require
'quvi/util'
135 for _
,v
in pairs(formats
) do
136 if U
.is_higher_quality(v
,r
) then
140 -- for k,v in pairs(r) do print(k,v) end
144 function Blip
.choose_default(formats
) -- Website sets this flag, reuse it.
145 for _
,v
in pairs(formats
) do
146 if v
.default
== 'true' then
147 -- for k,_v in pairs(v) do print(k,_v) end
151 error('no match: default')
154 function Blip
.to_s(t
)
155 return (t
.height
> 0)
156 and string.format("%s_%s_%sp", t
.container
, t
.role
, t
.height
)
157 or string.format("%s_%s", t
.container
, t
.role
)
160 -- vim: set ts=4 sw=4 tw=72 expandtab: