canalplus.lua: media ID: Remove if-then-block
[libquvi-scripts.git] / share / lua / website / canalplus.lua
blob9381acf72e8417523416c794ded123a8d5c593a6
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)
45 local U = require 'quvi/util'
46 local formats = CanalPlus.iter_formats(self, config, U)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, CanalPlus.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "|")
56 return self
57 end
59 -- Parse media URL.
60 function parse(self)
61 self.host_id = 'canalplus'
62 local config = CanalPlus.get_config(self)
63 local U = require 'quvi/util'
65 local formats = CanalPlus.iter_formats(self, config, U)
66 local format = U.choose_format(self, formats,
67 CanalPlus.choose_best,
68 CanalPlus.choose_default,
69 CanalPlus.to_s)
70 or error("unable to choose format")
71 self.url = {format.url or error("no match: media url")}
73 return self
74 end
77 -- Utility functions
80 function CanalPlus.get_config(self)
81 local t = {}
82 local page = quvi.fetch(self.page_url)
84 -- canalplus.fr
85 self.title = page:match('videoTitre%s-=%s-"(.-)"')
86 if not self.title then
87 -- presidentielle2012.canalplus.fr
88 self.title = page:match('property="og:title"%s+content="(.-)"')
89 or error('no match: media title')
90 end
92 self.id = page:match('videoId=(%d+)')
93 or page:match('videoId%s+=%s+"(%d+)"')
94 or error('no match: media ID')
96 local c_url =
97 "http://service.canal-plus.com/video/rest/getVideosLiees/cplus/"
98 .. self.id
100 t.data = quvi.fetch(c_url, {fetch_type = 'config'})
102 return t
105 function CanalPlus.iter_formats(self, config, U)
106 local t = {}
108 local p = '<ID>' .. self.id .. '</ID>'
109 .. '.-<INFOS>'
110 .. '.-<TITRAGE>'
111 .. '.-<MEDIA>'
112 .. '.-<IMAGES>'
113 .. '.-<PETIT>(.-)<'
114 .. '.-<VIDEOS>'
115 .. '.-<BAS_DEBIT>(.-)<'
116 .. '.-<HAUT_DEBIT>(.-)<'
117 .. '.-<HD>(.-)<'
119 -- sd = low definition flv
120 -- hd = high definition flv
121 -- hq = high definition mp4
123 local thumb,sd_url,hd_url,hq_url = config.data:match(p)
124 if not sd_url then error("no match: media url") end
126 self.thumbnail_url = thumb or ''
128 table.insert(t, {url=sd_url, quality="sd"})
129 table.insert(t, {url=hd_url, quality="hd"})
130 table.insert(t, {url=hq_url, quality="hq"})
132 return t
135 function CanalPlus.choose_best(formats) -- Whatever matches 'hd' first
136 local r
137 for _,v in pairs(formats) do
138 if CanalPlus.to_s(v):find('hd') then
139 return v
142 return r
145 function CanalPlus.choose_default(formats) -- Whatever matches 'sd' first
146 local r
147 for _,v in pairs(formats) do
148 if CanalPlus.to_s(v):find('sd') then
149 return v
152 return r
155 function CanalPlus.to_s(t)
156 return t.quality
159 -- vim: set ts=4 sw=4 tw=72 expandtab: