", "")
local xml = simplexml.parse_string(substring)
for _, tr in ipairs(xml.children) do
-- Get film title & year
local film_title = nil
local film_year = nil
local node = tr.children[2] -- td
if node then node = node.children[1] end -- div (1)
if node then node = node.children[1] end -- div (2)
local subnode = nil
if node then
for _, subnode in ipairs(node.children) do
if subnode.name == "a" and type(subnode.children[1]) == "string" then
film_title = trim(subnode.children[1]) -- content of a tag
else if subnode.name == "span" and type(subnode.children[1]) == "string" then
film_year = trim(subnode.children[1])
end end
end
end
-- Get film cover & URL
local film_image = nil
local film_url = nil
local node = tr.children[1] -- td
if node then node = node.children[1] end -- a
if node and node.name == "a" then
film_url = node.attributes["href"]
node = node.children[1]
if node and node.name == "img" then
film_image = node.attributes["src"]
end
end
-- Append fetched information
if film_title then
if string.sub(film_url, 1, 4) ~= "http" then
film_url = "http://www.allocine.fr" .. film_url
end
films[#films+1] = { url = film_url ; image = film_image ; year = film_year ; title = film_title }
end
end
end
end
-- Print information
-- No results found
if #films == 0 then
message_text = "
Aucun résultat trouvé pour " .. string.gsub(name, "%+", " ") .. "."
.. "Vous pouvez aussi chercher directement sur
Allociné."
message:set_text(message_text)
end
-- Only one movie or TV show matches, let's open its page directly
if #films == 1 then
message_text = "
" .. films[1].title .. ""
message:set_text(message_text)
dlg:update()
open_fiche(films[1].url)
end
-- More than 1 match, display a list
if #films > 1 then
message_text = tostring(#films) .. " films ou séries TV trouvés sur Allociné :"
message:set_text(message_text)
list = dlg:add_list(1, 3, 3, 1)
for idx, film in ipairs(films) do
local txt = film.title
if film.year then txt = txt .. " (" .. film.year .. ")" end
list:add_value(txt, idx)
end
okay = dlg:add_button("Voir la fiche", click_okay, 3, 4, 1, 1)
end
end
-- Click after selection
function click_okay()
if not films or not #films then return end
local selection = list:get_selection()
if not selection then return end
local sel, _ = next(selection, nil)
if not sel then return end
message_text = "
" .. films[sel].title .. ""
message:set_text(message_text)
dlg:update()
open_fiche(films[sel].url)
end
-- Open a movie's information page
function open_fiche(url)
if okay then
dlg:del_widget(okay)
okay = nil
end
if list then
dlg:del_widget(list)
list = nil
end
if not html then
html = dlg:add_html("
Chargement en cours...", 1, 3, 3, 1)
end
dlg:update()
-- Open stream
local s = vlc.stream(url)
-- Read max 500k (Note: 65k is not enough for the average note)
local data = s:read(500000)
-- Buffer & temp variables
local first = nil
local last = nil
local page = nil
local sub = nil
local name = nil
first, _ = string.find(data, '
')
if not first then
message:set_text("
Erreur !
Désolé, une erreur est survenue pendant le chargement de la fiche.
"
.. "
Cliquez ici pour consulter la page sur Allociné.fr.")
dlg:del_widget(html)
return
end
-- Extract information
local last, _ = string.find(data, '
", "
")
sub = string.gsub(sub, "?div[^>]*>", "")
sub = string.gsub(sub, "?span[^>]*>", "")
sub = string.gsub(sub, "<%!%-%-[^%-]+%-%->", "")
sub = string.gsub(sub, "
%s*
", "
")
page = string.gsub(sub, "Synopsis :.*$", "")
-- Style
local synopsis = string.gsub(sub, ".*Synopsis :(.*)", "Synposis
%1")
-- Note
for w in string.gmatch(data, "property=\"v:average\"[^>]*>([^<]+)") do
local note = trim(w)
page = page .. "Note moyenne: " .. note .. " / 4"
for y in string.gmatch(data, "property=\"v:count\"[^>]*>([^<]+)") do
local nbpeople = trim(y)
page = page .. " (" .. nbpeople .. " votes)"
break
end
break
end
-- Synopsis
page = page .. synopsis
-- Movie title
if string.find(data, '.*
') then
name = string.gsub(data, '^.*%s*(.*)%s*
.*$', '%1')
name = trim(name)
end
page = page .. "Source
"
if name then
page = page .. name .. " sur Allociné"
else
page = page .. "Allociné"
end
page = string.gsub(page, "href=([\"'])/", "href=%1http://www.allocine.fr/")
html:set_text(page)
end