3 -- Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
5 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
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
22 local CanalPlus = {} -- Utility functions unique to to this script.
24 -- Identify the script.
26 package.path = self.script_dir .. '/?.lua'
27 local C = require 'quvi/const'
29 r.domain = "canalplus%.fr"
30 r.formats = "default|best"
31 r.categories = C.proto_rtmp
32 local U = require 'quvi/util'
33 r.handles = U.handles(self.page_url, {r.domain}, {"/pid%d",
34 -- presidentielle2012.canalplus.fr
35 "/emissions", "/candidats", "/debats",
36 -- canalstreet.canalplus.fr
37 "/musique", "/actu", "/humour", "/tendances", "/sport", "/arts", "/danse"})
42 -- Query available formats.
43 function query_formats(self)
44 local config = CanalPlus.get_config(self)
46 if #self.redirect_url >0 then
50 local U = require 'quvi/util'
51 local formats = CanalPlus.iter_formats(self, config, U)
54 for _,v in pairs(formats) do
55 table.insert(t, CanalPlus.to_s(v))
59 self.formats = table.concat(t, "|")
66 self.host_id = 'canalplus'
68 local config = CanalPlus.get_config(self)
70 if #self.redirect_url >0 then
74 local U = require 'quvi/util'
75 local formats = CanalPlus.iter_formats(self, config, U)
76 local format = U.choose_format(self, formats,
77 CanalPlus.choose_best,
78 CanalPlus.choose_default,
80 or error("unable to choose format")
81 self.url = {format.url or error("no match: media url")}
90 function CanalPlus.get_config(self)
92 local page = quvi.fetch(self.page_url)
94 local u = page:match('"og:video" content="(.-)"')
95 if u and not u:match('canalplus%.fr') then
96 self.redirect_url = u -- Media is hosted elsewhere, e.g. YouTube.
101 self.title = page:match('videoTitre%s-=%s-"(.-)"')
102 if not self.title then
103 -- presidentielle2012.canalplus.fr
104 self.title = page:match('property="og:title"%s+content="(.-)"')
105 or error('no match: media title')
108 self.id = page:match('videoId=(%d+)')
109 or page:match('videoId%s+=%s+"(%d+)"')
110 or error('no match: media ID')
112 local u = "http://service.canal-plus.com/video/rest/getVideosLiees/cplus/"
115 return quvi.fetch(u, {fetch_type = 'config'})
118 function CanalPlus.iter_formats(self, config, U)
120 local id = config:match('<ID>(.-)</ID>')
121 if id and id == '-1' then
122 error('Media is no longer available (expired)')
125 local p = '<ID>' .. self.id .. '</ID>'
132 .. '.-<BAS_DEBIT>(.-)<'
133 .. '.-<HAUT_DEBIT>(.-)<'
136 -- sd = low definition flv
137 -- hd = high definition flv
138 -- hq = high definition mp4
140 local thumb,sd_url,hd_url,hq_url = config:match(p)
141 if not sd_url then error("no match: media url") end
143 self.thumbnail_url = thumb or ''
146 table.insert(t, {url=sd_url, quality="sd"})
147 table.insert(t, {url=hd_url, quality="hd"})
148 table.insert(t, {url=hq_url, quality="hq"})
152 function CanalPlus.choose_default(t)
153 return t[1] -- Presume the first to be the 'default'.
156 function CanalPlus.choose_best(t)
157 return t[#t] -- Presume the last to be the 'best'.
160 function CanalPlus.to_s(t)
164 -- vim: set ts=4 sw=4 tw=72 expandtab: