Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / lua / website / blip.lua
Commit [+]AuthorDateLineData
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +03001
2-- libquvi-scripts
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +02003-- Copyright (C) 2011-2012 Toni Gundogdu <legatvs@gmail.com>
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +03004--
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
26local Blip = {} -- Utility functions unique to this script.
27
28-- Identify the script.
29function 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
45end
46
47-- Query formats.
48function query_formats(self)
05edfb52
TG
Toni Gundogdu2012-03-17 21:46:37 +020049 local c = Blip.get_config(self)
50 local formats = Blip.iter_formats(c)
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030051
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
61end
62
63-- Parse media URL.
64function parse(self)
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +020065 self.host_id = "blip"
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030066
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +020067 local c,U = Blip.get_config(self)
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030068
05edfb52
TG
Toni Gundogdu2012-03-17 21:46:37 +020069 self.title = c:match('media:title>(.-)</')
70 or error('no match: media title')
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030071
05edfb52
TG
Toni Gundogdu2012-03-17 21:46:37 +020072 self.thumbnail_url = c:match('<blip:smallThumbnail>(.-)<') or ''
73
74 local formats = Blip.iter_formats(c)
8a40fa04 Toni Gundogdu2011-11-24 17:38:25 +020075 local format = U.choose_format(self, formats,
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030076 Blip.choose_best,
77 Blip.choose_default,
8a40fa04
TG
Toni Gundogdu2011-11-24 17:38:25 +020078 Blip.to_s)
79 or error("unable to choose format")
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +020080 self.url = {format.url or error("no match: media URL")}
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030081 return self
82end
83
84--
85-- Utility functions
86--
87
88function Blip.get_config(self)
89 local U = require 'quvi/util'
90 self.page_url = U.unescape(self.page_url)
91
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +020092 local id = self.page_url:match('/flash/(%d+)')
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030093 if not id then
94 local r = quvi.resolve(self.page_url)
95 if #r > 0 then
96 r = U.unescape(r)
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +020097 id = r:match('/rss/flash/(%d+)')
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030098 end
99 end
100
101 if not id then
05edfb52
TG
Toni Gundogdu2012-03-17 21:46:37 +0200102 local p = quvi.fetch(self.page_url)
103 id = p:match('data%-posts%-id="(.-)"')
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300104 end
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +0200105 self.id = id or error('no match: media ID')
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300106
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +0200107 local c_url = 'http://blip.tv/rss/flash/' .. self.id
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300108
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +0200109 return quvi.fetch(c_url, {fetch_type='config'}), U
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300110end
111
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +0200112function Blip.iter_formats(c)
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300113 local p = '<media:content url="(.-)"'
114 .. '.-blip:role="(.-)"'
115 .. '.-height="(.-)"'
116 .. '.-isDefault="(.-)"'
117 .. '.-width="(.-)"'
118 local t = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +0200119 for u,r,h,d,w in c:gmatch(p) do
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300120 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
05edfb52 Toni Gundogdu2012-03-17 21:46:37 +0200125 local s = u:match('%.(%w+)$')
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300126 table.insert(t, {width=w, height=h, url=u, default=d,
127 role=r, container=s})
128 end
129 return t
130end
131
132function 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
142end
143
144function 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')
152end
153
154function 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)
158end
159
160-- vim: set ts=4 sw=4 tw=72 expandtab: