]> git.sesse.net Git - vlc/blob - share/lua/extensions/allocine-fr.lua
5d154a2ecd8a52b8b7107e039733267295358b2f
[vlc] / share / lua / extensions / allocine-fr.lua
1 --[[
2  Allocine Extension for VLC media player 1.1
3  French website only
4
5  Copyright © 2010 VideoLAN and AUTHORS
6
7  Authors:  Jean-Philippe André (jpeg@videolan.org)
8
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.
13
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.
18
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.
22 --]]
23
24 -- Lua modules
25 require "simplexml"
26
27 -- Global variables
28 dlg = nil     -- Dialog
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
34 films = {}
35
36 -- Extension description
37 function descriptor()
38     return { title = "Allociné (France)" ;
39              version = "1.0" ;
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" } }
50 end
51
52 -- Activation hook
53 function activate()
54     vlc.msg.dbg("[ALLOCINE.COM] Welcome on Allocine.fr")
55     create_dialog()
56 end
57
58 -- Deactivation hook
59 function deactivate()
60     vlc.msg.dbg("[ALLOCINE.COM] Bye bye!")
61 end
62
63 -- Dialog close hook
64 function close()
65     -- Deactivate this extension
66     vlc.deactivate()
67 end
68
69 -- Input change hook
70 function input_changed()
71     title:set_text(get_title())
72 end
73
74 -- Meta change hook
75 function meta_changed()
76     title:set_text(get_title())
77 end
78
79 -- Create the dialog
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)
85 end
86
87 -- Get clean title from filename
88 function get_title(str)
89     local item = vlc.item or vlc.input.item()
90     if not item then
91         return ""
92     end
93     local metas = item:metas()
94     if metas["title"] then
95         return metas["title"]
96     else
97         local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
98         return trim(filename or item:name())
99     end
100 end
101
102 -- Remove leading and trailing spaces
103 function trim(str)
104     if not str then return "" end
105     return string.gsub(str, "^%s*(.-)%s*$", "%1")
106 end
107
108 -- Lookup for this movie title
109 function click_chercher()
110     -- Get title
111     local name = title:get_text()
112     if not name or name == "" then
113         vlc.msg.dbg("[ALLOCINE.COM] No title")
114         return
115     end
116
117     -- Update dialog
118     if list then
119         dlg:del_widget(list)
120         list = nil
121     end
122
123     -- Transform spaces and dots into +
124     name = string.gsub(string.gsub(name, "[%p%s%c]", "+"), "%++", "+")
125
126     -- Build URL
127     local url = "http://www.allocine.fr/recherche/?q=" .. name
128
129     -- Please wait...
130     local message_text = "Recherche <a href=\"" .. url .. "\">" .. string.gsub(name, "%+", " ") .. "</a> sur Allociné..."
131     if not message then
132         message = dlg:add_label(message_text, 1, 2, 3, 1)
133     else
134         message:set_text(message_text)
135     end
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
139     list = nil
140     okay = nil
141     html = nil
142     dlg:update()
143
144     -- Open URL
145     local s, msg = vlc.stream(url)
146     if not s then
147         vlc.msg.warn("[ALLOCINE.COM] " .. msg)
148     end
149
150     -- Fetch HTML data (max 65 kb)
151     local data = s:read(65535)
152
153     -- Clean data
154     data = string.gsub(data, "<b>", "")
155     data = string.gsub(data, "</b>", "")
156     data = string.gsub(data, "%s+", " ")
157
158     -- Data storage
159     films = {}
160
161     -- Find categories
162     for category in string.gmatch(data, "<[hH]2>%s*([^<]+)%s*</[hH]2>") do
163         local category = trim(category)
164
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)
169
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)
174             print("\n")
175             print(substring)
176             print("\n")
177
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)
186                 local subnode = nil
187                 if node then
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])
193                         end end
194                     end
195                 end
196
197                 -- Get film cover & URL
198                 local film_image = nil
199                 local film_url = 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"]
207                     end
208                 end
209
210                 -- Append fetched information
211                 if film_title then
212                     if string.sub(film_url, 1, 4) ~= "http" then
213                         film_url = "http://www.allocine.fr" .. film_url
214                     end
215                     films[#films+1] = { url = film_url ; image = film_image ; year = film_year ; title = film_title }
216                 end
217             end
218         end
219     end
220
221     -- Print information
222     -- No results found
223     if #films == 0 then
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)
227     end
228
229     -- Only one movie or TV show matches, let's open its page directly
230     if #films == 1 then
231         message_text = "<center><a href=\"" .. films[1].url .. "\">" .. films[1].title .. "</a></center>"
232         message:set_text(message_text)
233         dlg:update()
234         open_fiche(films[1].url)
235     end
236
237     -- More than 1 match, display a list
238     if #films > 1 then
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)
246         end
247         okay = dlg:add_button("Voir la fiche", click_okay, 3, 4, 1, 1)
248     end
249 end
250
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
256
257     local sel = nil
258     for idx, selectedItem in pairs(selection) do
259         sel = idx
260         break
261     end
262
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)
266     dlg:update()
267     open_fiche(films[sel].url)
268 end
269
270 -- Open a movie's information page
271 function open_fiche(url)
272     if okay then
273         dlg:del_widget(okay)
274         okay = nil
275     end
276     if list then
277         dlg:del_widget(list)
278         list = nil
279     end
280
281     if not html then
282         html = dlg:add_html("<center><i>Chargement en cours...</i></center>", 1, 3, 3, 1)
283     end
284     dlg:update()
285
286     -- Open stream
287     local s = vlc.stream(url)
288     local data = s:read(65535)
289
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"')
293     if not last then
294         last, _ = string.find(data, 'notationbar')
295     end
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)
300 end