canalplus.lua: choose_*: Use array index instead
[libquvi-scripts.git] / share / lua / website / canalplus.lua
blobd6cb9063c35833f024bebdcd71aa37bcb6461fac
2 -- libquvi-scripts
3 -- Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.googlecode.com/>.
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
22 local CanalPlus = {} -- Utility functions unique to to this script.
24 -- Identify the script.
25 function ident(self)
26 package.path = self.script_dir .. '/?.lua'
27 local C = require 'quvi/const'
28 local r = {}
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"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 local config = CanalPlus.get_config(self)
46 if #self.redirect_url >0 then
47 return self
48 end
50 local U = require 'quvi/util'
51 local formats = CanalPlus.iter_formats(self, config, U)
53 local t = {}
54 for _,v in pairs(formats) do
55 table.insert(t, CanalPlus.to_s(v))
56 end
58 table.sort(t)
59 self.formats = table.concat(t, "|")
61 return self
62 end
64 -- Parse media URL.
65 function parse(self)
66 self.host_id = 'canalplus'
68 local config = CanalPlus.get_config(self)
70 if #self.redirect_url >0 then
71 return self
72 end
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,
79 CanalPlus.to_s)
80 or error("unable to choose format")
81 self.url = {format.url or error("no match: media url")}
83 return self
84 end
87 -- Utility functions
90 function CanalPlus.get_config(self)
91 local t = {}
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.
97 return
98 end
100 -- canalplus.fr
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 c_url =
113 "http://service.canal-plus.com/video/rest/getVideosLiees/cplus/"
114 .. self.id
116 t.data = quvi.fetch(c_url, {fetch_type = 'config'})
118 return t
121 function CanalPlus.iter_formats(self, config, U)
122 local t = {}
124 local p = '<ID>' .. self.id .. '</ID>'
125 .. '.-<INFOS>'
126 .. '.-<TITRAGE>'
127 .. '.-<MEDIA>'
128 .. '.-<IMAGES>'
129 .. '.-<PETIT>(.-)<'
130 .. '.-<VIDEOS>'
131 .. '.-<BAS_DEBIT>(.-)<'
132 .. '.-<HAUT_DEBIT>(.-)<'
133 .. '.-<HD>(.-)<'
135 -- sd = low definition flv
136 -- hd = high definition flv
137 -- hq = high definition mp4
139 local thumb,sd_url,hd_url,hq_url = config.data:match(p)
140 if not sd_url then error("no match: media url") end
142 self.thumbnail_url = thumb or ''
144 table.insert(t, {url=sd_url, quality="sd"})
145 table.insert(t, {url=hd_url, quality="hd"})
146 table.insert(t, {url=hq_url, quality="hq"})
148 return t
151 function CanalPlus.choose_default(t)
152 return t[1] -- Presume the first to be the 'default'.
155 function CanalPlus.choose_best(t)
156 return t[#t] -- Presume the last to be the 'best'.
159 function CanalPlus.to_s(t)
160 return t.quality
163 -- vim: set ts=4 sw=4 tw=72 expandtab: