2 Allocine Extension for VLC media player 1.1
5 Copyright © 2010 VideoLAN and AUTHORS
7 Authors: Jean-Philippe André (jpeg@videolan.org)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 title = nil -- Text input widget
30 message = nil -- Label
31 list = nil -- List widget
32 okay = nil -- Okay button
33 html = nil -- HTML box
36 -- Extension description
38 return { title = "Allociné (France)" ;
40 author = "Jean-Philippe André" ;
41 url = 'http://www.allocine.fr/';
42 shortdesc = "Allocine.com";
43 description = "<center><b>ALLOCINE.COM</b></center>"
44 .. "Récupère des informations sur le média en cours "
45 .. "de lecture depuis Allocine.fr, telles que :<br />"
46 .. "- Casting,<br />- Résumé,<br />- Note des utilisateurs,"
47 .. "<br />- Lien direct vers la fiche du film sur "
48 .. "<a href=\"http://www.allocine.fr\">allocine.fr</a>." ;
49 capabilities = { "input-listener", "meta-listener" } }
54 vlc.msg.dbg("[ALLOCINE.COM] Welcome on Allocine.fr")
60 vlc.msg.dbg("[ALLOCINE.COM] Bye bye!")
65 -- Deactivate this extension
70 function input_changed()
71 title:set_text(get_title())
75 function meta_changed()
76 title:set_text(get_title())
80 function create_dialog()
81 dlg = vlc.dialog("ALLOCINE.COM")
82 dlg:add_label("<b>Titre du film:</b>", 1, 1, 1, 1)
83 title = dlg:add_text_input(get_title(), 2, 1, 1, 1)
84 dlg:add_button("Rechercher", click_chercher, 3, 1, 1, 1)
87 -- Get clean title from filename
88 function get_title(str)
89 local item = vlc.item or vlc.input.item()
93 local metas = item:metas()
94 if metas["title"] then
97 local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
98 return trim(filename or item:name())
102 -- Remove leading and trailing spaces
104 if not str then return "" end
105 return string.gsub(str, "^%s*(.-)%s*$", "%1")
108 -- Lookup for this movie title
109 function click_chercher()
111 local name = title:get_text()
112 if not name or name == "" then
113 vlc.msg.dbg("[ALLOCINE.COM] No title")
123 -- Transform spaces and dots into +
124 name = string.gsub(string.gsub(name, "[%p%s%c]", "+"), "%++", "+")
127 local url = "http://www.allocine.fr/recherche/?q=" .. name
130 local message_text = "Recherche <a href=\"" .. url .. "\">" .. string.gsub(name, "%+", " ") .. "</a> sur Allociné..."
132 message = dlg:add_label(message_text, 1, 2, 3, 1)
134 message:set_text(message_text)
136 if list then dlg:del_widget(list) end
137 if okay then dlg:del_widget(okay) end
138 if html then dlg:del_widget(html) end
145 local s, msg = vlc.stream(url)
147 vlc.msg.warn("[ALLOCINE.COM] " .. msg)
150 -- Fetch HTML data (max 65 kb)
151 local data = s:read(65535)
154 data = string.gsub(data, "<b>", "")
155 data = string.gsub(data, "</b>", "")
156 data = string.gsub(data, "%s+", " ")
162 for category in string.gmatch(data, "<[hH]2>%s*([^<]+)%s*</[hH]2>") do
163 local category = trim(category)
165 -- Split substring corresponding to this table
166 local _, first = string.find(data, "<[hH]2>%s*" .. category .. "%s*</[hH]2>")
167 first, _ = string.find(data, "<table", first)
168 local _, last = string.find(data, "</table>", first)
170 -- Find movies and TV shows
171 if category == "Films" or category == "Séries TV" then
172 -- Read <table> tag as xml
173 local substring = string.sub(data, first, last or -1)
178 local xml = simplexml.parse_string(substring)
179 for _, tr in ipairs(xml.children) do
180 -- Get film title & year
181 local film_title = nil
182 local film_year = nil
183 local node = tr.children[2] -- td
184 if node then node = node.children[1] end -- div (1)
185 if node then node = node.children[1] end -- div (2)
188 for _, subnode in ipairs(node.children) do
189 if subnode.name == "a" and type(subnode.children[1]) == "string" then
190 film_title = trim(subnode.children[1]) -- content of a tag
191 else if subnode.name == "span" and type(subnode.children[1]) == "string" then
192 film_year = trim(subnode.children[1])
197 -- Get film cover & URL
198 local film_image = nil
200 local node = tr.children[1] -- td
201 if node then node = node.children[1] end -- a
202 if node and node.name == "a" then
203 film_url = node.attributes["href"]
204 node = node.children[1]
205 if node and node.name == "img" then
206 film_image = node.attributes["src"]
210 -- Append fetched information
212 if string.sub(film_url, 1, 4) ~= "http" then
213 film_url = "http://www.allocine.fr" .. film_url
215 films[#films+1] = { url = film_url ; image = film_image ; year = film_year ; title = film_title }
224 message_text = "<center>Aucun résultat trouvé pour <b>" .. string.gsub(name, "%+", " ") .. "</b>.</center>"
225 .. "Vous pouvez aussi chercher directement sur <a href=\"" .. url .. "\">Allociné</a>."
226 message:set_text(message_text)
229 -- Only one movie or TV show matches, let's open its page directly
231 message_text = "<center><a href=\"" .. films[1].url .. "\">" .. films[1].title .. "</a></center>"
232 message:set_text(message_text)
234 open_fiche(films[1].url)
237 -- More than 1 match, display a list
239 message_text = tostring(#films) .. " films ou séries TV trouvés sur Allociné :"
240 message:set_text(message_text)
241 list = dlg:add_list(1, 3, 3, 1)
242 for idx, film in ipairs(films) do
243 local txt = film.title
244 if film.year then txt = txt .. " (" .. film.year .. ")" end
245 list:add_value(txt, idx)
247 okay = dlg:add_button("Voir la fiche", click_okay, 3, 4, 1, 1)
251 -- Click after selection
252 function click_okay()
253 if not films or not #films then return end
254 local selection = list:get_selection()
255 if not selection then return end
258 for idx, selectedItem in pairs(selection) do
263 if not sel then return end
264 message_text = "<center><a href=\"" .. films[sel].url .. "\">" .. films[sel].title .. "</a></center>"
265 message:set_text(message_text)
267 open_fiche(films[sel].url)
270 -- Open a movie's information page
271 function open_fiche(url)
282 html = dlg:add_html("<center><i>Chargement en cours...</i></center>", 1, 3, 3, 1)
287 local s = vlc.stream(url)
288 local data = s:read(65535)
290 -- HACK: use directly HTML data from webpage
291 local first, _ = string.find(data, '<div class="rubric">')
292 local last, _ = string.find(data, '<ul id="link_open"')
294 last, _ = string.find(data, 'notationbar')
296 local subdata = string.sub(data, first, (last or 0)-1)
297 subdata = string.gsub(subdata, "%s+", " ")
298 subdata = string.gsub(subdata, "href=([\"'])/", "href=%1http://www.allocine.fr/")
299 html:set_text(subdata)