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
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"),
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.
39 package
.path
= self
.script_dir
.. '/?.lua'
40 local C
= require
'quvi/const'
42 r
.domain
= "guardian%.co%.uk"
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"})
51 -- Query available formats.
52 function query_formats(self
)
53 local c
= Guardian
.get_config(self
)
54 local fmts
= Guardian
.iter_formats(c
)
57 for _
,v
in pairs(fmts
) do
58 table.insert(t
, Guardian
.to_s(v
))
62 self
.formats
= table.concat(t
, "|")
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
,
77 Guardian
.choose_default
,
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
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+"(.-)"'
107 for c
,u
in config
:gmatch(p
) do
109 c
= c
:gsub("video/", "")
111 if c
~= "m3u8" then -- http://en.wikipedia.org/wiki/M3U
112 table.insert(t
, {container
=c
, url
=u
})
118 function Guardian
.choose_best(t
) -- Expect the first to be the 'best'
122 function Guardian
.choose_default(t
) -- Use the first
126 function Guardian
.to_s(t
)
130 -- vim: set ts=4 sw=4 tw=72 expandtab: