blip.lua: Use "foo:match" instead
[libquvi-scripts.git] / share / lua / website / blip.lua
blobb1708451ae0c765d998f54723dfa9b5830fe4c53
2 -- libquvi-scripts
3 -- Copyright (C) 2011-2012 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <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 -- 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.
29 function ident(self)
30 package.path = self.script_dir .. '/?.lua'
31 local C = require 'quvi/const'
32 local r = {}
33 r.domain = "blip%.tv"
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.
39 {"/file/%d+",
40 "/play/%w+",
41 "/flash/%d+",
42 "/scripts/flash/",
43 "%-%d+"})
44 return r
45 end
47 -- Query formats.
48 function query_formats(self)
49 local c = Blip.get_config(self)
50 local formats = Blip.iter_formats(c)
52 local t = {}
53 for _,v in pairs(formats) do
54 table.insert(t, Blip.to_s(v))
55 end
57 table.sort(t)
58 self.formats = table.concat(t, "|")
60 return self
61 end
63 -- Parse media URL.
64 function parse(self)
65 self.host_id = "blip"
67 local c,U = Blip.get_config(self)
69 self.title = c:match('media:title>(.-)</')
70 or error('no match: media title')
72 self.thumbnail_url = c:match('<blip:smallThumbnail>(.-)<') or ''
74 local formats = Blip.iter_formats(c)
75 local format = U.choose_format(self, formats,
76 Blip.choose_best,
77 Blip.choose_default,
78 Blip.to_s)
79 or error("unable to choose format")
80 self.url = {format.url or error("no match: media URL")}
81 return self
82 end
85 -- Utility functions
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:match('/flash/(%d+)')
93 if not id then
94 local r = quvi.resolve(self.page_url)
95 if #r > 0 then
96 r = U.unescape(r)
97 id = r:match('/rss/flash/(%d+)')
98 end
99 end
101 if not id then
102 local p = quvi.fetch(self.page_url)
103 id = p:match('data%-posts%-id="(.-)"')
105 self.id = id or error('no match: media ID')
107 local c_url = 'http://blip.tv/rss/flash/' .. self.id
109 return quvi.fetch(c_url, {fetch_type='config'}), U
112 function Blip.iter_formats(c)
113 local p = '<media:content url="(.-)"'
114 .. '.-blip:role="(.-)"'
115 .. '.-height="(.-)"'
116 .. '.-isDefault="(.-)"'
117 .. '.-width="(.-)"'
118 local t = {}
119 for u,r,h,d,w in c:gfind(p) do
120 r = string.lower(r)
121 r = r:gsub(' ','_')
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:match('%.(%w+)$')
126 table.insert(t, {width=w, height=h, url=u, default=d,
127 role=r, container=s})
129 return t
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
137 r = v
140 -- for k,v in pairs(r) do print(k,v) end
141 return r
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
148 return v
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: