Update NEWS for v0.9.20130520
[libquvi-scripts.git] / share / lua / website / blip.lua
1
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.
11 --
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.
16 --
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
21 --
22
23 -- Based on get-flash-videos' Blip.pm
24 -- <https://github.com/monsieurvideo/get-flash-videos/>
25
26 local Blip = {} -- Utility functions unique to this script.
27
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
46
47 -- Query formats.
48 function query_formats(self)
49     local c = Blip.get_config(self)
50     local formats = Blip.iter_formats(c)
51
52     local t = {}
53     for _,v in pairs(formats) do
54         table.insert(t, Blip.to_s(v))
55     end
56
57     table.sort(t)
58     self.formats = table.concat(t, "|")
59
60     return self
61 end
62
63 -- Parse media URL.
64 function parse(self)
65     self.host_id = "blip"
66
67     local c,U = Blip.get_config(self)
68
69     self.title = c:match('media:title>(.-)</')
70                   or error('no match: media title')
71
72     self.thumbnail_url = c:match('<blip:smallThumbnail>(.-)<') or ''
73
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
83
84 --
85 -- Utility functions
86 --
87
88 function Blip.get_config(self)
89     local U       = require 'quvi/util'
90     self.page_url = U.unescape(self.page_url)
91
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
100
101     if not id then
102         local p = quvi.fetch(self.page_url)
103         id = p:match('data%-posts%-id="(.-)"')
104     end
105     self.id = id or error('no match: media ID')
106
107     local c_url = 'http://blip.tv/rss/flash/' .. self.id
108
109     return quvi.fetch(c_url, {fetch_type='config'}), U
110 end
111
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:gmatch(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})
128     end
129     return t
130 end
131
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
138         end
139     end
140 --    for k,v in pairs(r) do print(k,v) end
141     return r
142 end
143
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
149         end
150     end
151     error('no match: default')
152 end
153
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)
158 end
159
160 -- vim: set ts=4 sw=4 tw=72 expandtab: