ard.lua: Cleanup Ard.iter_formats
[libquvi-scripts.git] / share / lua / website / guardian.lua
blob9661f556c05af0405938bc5217d10361c614649f
2 -- libquvi-scripts
3 -- Copyright (C) 2011 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
24 -- NOTE: Ignores the m3u8 format. Patches welcome.
26 -- libquvi allows specifying multiple media stream URLs in
27 -- "self.url" (referred sometimes as "media or video segments"),
28 -- e.g.
29 -- self.url = {"http://foo", "http://bar"}
31 -- Whether the applications using libquvi make any use of this,
32 -- is a whole different matter.
35 local Guardian = {} -- Utility functions unique to this script
37 -- Identify the script.
38 function ident (self)
39 package.path = self.script_dir .. '/?.lua'
40 local C = require 'quvi/const'
41 local r = {}
42 r.domain = "guardian%.co%.uk"
43 r.formats = "default"
44 r.categories = C.proto_http
45 local U = require 'quvi/util'
46 r.handles = U.handles(self.page_url,
47 {r.domain}, {"/video/","/audio"})
48 return r
49 end
51 -- Query available formats.
52 function query_formats(self)
53 local c = Guardian.get_config(self)
54 local fmts = Guardian.iter_formats(c)
56 local t = {}
57 for _,v in pairs(fmts) do
58 table.insert(t, Guardian.to_s(v))
59 end
61 table.sort(t)
62 self.formats = table.concat(t, "|")
64 return self
65 end
67 -- Parse media URL.
68 function parse(self)
69 self.host_id = "guardian"
71 local c = Guardian.get_config(self)
73 local formats = Guardian.iter_formats(c)
74 local U = require 'quvi/util'
75 local format = U.choose_format(self, formats,
76 Guardian.choose_best,
77 Guardian.choose_default,
78 Guardian.to_s)
79 or error("unable to choose format")
80 self.url = {format.url or error("no match: media url")}
82 self.title = c:match('"headline":%s+"(.-)%s+-%s+video"')
83 or error("no match: media title")
85 self.id = c:match('"video%-id":%s+"(.-)"')
86 or error ("no match: media id")
88 self.thumbnail_url = c:match('"thumbnail%-image%-url":%s+"(.-)"') or ''
90 local d = c:match('"duration":%s+(%d+)') or 0
91 self.duration = tonumber(d)*1000 -- to msec
93 return self
94 end
97 -- Utility functions
100 function Guardian.get_config(self)
101 return quvi.fetch(self.page_url .. "/json", {fetch_type='config'})
104 function Guardian.iter_formats(config)
105 local p = '"format":%s+"(.-)".-"video%-file%-url":%s+"(.-)"'
106 local t = {}
107 for c,u in config:gmatch(p) do
108 -- print(f,u)
109 c = c:gsub("video/", "")
110 c = c:gsub(":", "_")
111 if c ~= "m3u8" then -- http://en.wikipedia.org/wiki/M3U
112 table.insert(t, {container=c, url=u})
115 return t
118 function Guardian.choose_best(t) -- Expect the first to be the 'best'
119 return t[1]
122 function Guardian.choose_default(t) -- Use the first
123 return t[1]
126 function Guardian.to_s(t)
127 return t.container
130 -- vim: set ts=4 sw=4 tw=72 expandtab: