Update NEWS for v0.9.20130520
[libquvi-scripts.git] / share / lua / website / canalplus.lua
1
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.
11 --
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.
16 --
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
21
22 local CanalPlus = {} -- Utility functions unique to to this script.
23
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"})
38
39     return r
40 end
41
42 -- Query available formats.
43 function query_formats(self)
44     local config  = CanalPlus.get_config(self)
45
46     if #self.redirect_url >0 then
47         return self
48     end
49
50     local U       = require 'quvi/util'
51     local formats = CanalPlus.iter_formats(self, config, U)
52
53     local t = {}
54     for _,v in pairs(formats) do
55         table.insert(t, CanalPlus.to_s(v))
56     end
57
58     table.sort(t)
59     self.formats = table.concat(t, "|")
60
61     return self
62 end
63
64 -- Parse media URL.
65 function parse(self)
66     self.host_id  = 'canalplus'
67
68     local config  = CanalPlus.get_config(self)
69
70     if #self.redirect_url >0 then
71         return self
72     end
73
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")}
82
83     return self
84 end
85
86 --
87 -- Utility functions
88 --
89
90 function CanalPlus.get_config(self)
91     local t    = {}
92     local page = quvi.fetch(self.page_url)
93
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
99
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')
106     end
107
108     self.id = page:match('videoId=(%d+)')
109                 or page:match('videoId%s+=%s+"(%d+)"')
110                 or error('no match: media ID')
111
112     local u = "http://service.canal-plus.com/video/rest/getVideosLiees/cplus/"
113                 .. self.id
114
115     return quvi.fetch(u, {fetch_type = 'config'})
116 end
117
118 function CanalPlus.iter_formats(self, config, U)
119
120     local id = config:match('<ID>(.-)</ID>')
121     if id and id == '-1' then
122         error('Media is no longer available (expired)')
123     end
124
125     local p = '<ID>' .. self.id .. '</ID>'
126            .. '.-<INFOS>'
127            .. '.-<TITRAGE>'
128            .. '.-<MEDIA>'
129            .. '.-<IMAGES>'
130            .. '.-<PETIT>(.-)<'
131            .. '.-<VIDEOS>'
132            .. '.-<BAS_DEBIT>(.-)<'
133            .. '.-<HAUT_DEBIT>(.-)<'
134            .. '.-<HD>(.-)<'
135
136     -- sd = low definition flv
137     -- hd = high definition flv
138     -- hq = high definition mp4
139
140     local thumb,sd_url,hd_url,hq_url = config:match(p)
141     if not sd_url then error("no match: media url") end
142
143     self.thumbnail_url = thumb or ''
144
145     local t = {}
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"})
149     return t
150 end
151
152 function CanalPlus.choose_default(t)
153     return t[1] -- Presume the first to be the 'default'.
154 end
155
156 function CanalPlus.choose_best(t)
157     return t[#t] -- Presume the last to be the 'best'.
158 end
159
160 function CanalPlus.to_s(t)
161     return t.quality
162 end
163
164 -- vim: set ts=4 sw=4 tw=72 expandtab: