home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / vlc-1.1.7-win32.exe / lua / extensions / allocine-fr.lua next >
Encoding:
Text File  |  2011-01-30  |  11.1 KB  |  353 lines

  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 = "VideoLAN" ;
  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.  
  175.             -- Fix Allocine's broken XML (!!!)
  176.             substring = string.gsub(substring, "<div class=\"spacer vmargin10\">", "")
  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, _ = next(selection, nil)
  258.     if not sel then return end
  259.  
  260.     message_text = "<center><a href=\"" .. films[sel].url .. "\">" .. films[sel].title .. "</a></center>"
  261.     message:set_text(message_text)
  262.     dlg:update()
  263.     open_fiche(films[sel].url)
  264. end
  265.  
  266. -- Open a movie's information page
  267. function open_fiche(url)
  268.     if okay then
  269.         dlg:del_widget(okay)
  270.         okay = nil
  271.     end
  272.     if list then
  273.         dlg:del_widget(list)
  274.         list = nil
  275.     end
  276.  
  277.     if not html then
  278.         html = dlg:add_html("<center><i>Chargement en cours...</i></center>", 1, 3, 3, 1)
  279.     end
  280.     dlg:update()
  281.  
  282.     -- Open stream
  283.     local s = vlc.stream(url)
  284.     -- Read max 500k (Note: 65k is not enough for the average note)
  285.     local data = s:read(500000)
  286.  
  287.     -- Buffer & temp variables
  288.     local first = nil
  289.     local last = nil
  290.     local page = nil
  291.     local sub = nil
  292.     local name = nil
  293.  
  294.     first, _ = string.find(data, '<div class="rubric">')
  295.  
  296.     if not first then
  297.         message:set_text("<h2>Erreur !</h2>D├⌐sol├⌐, une erreur est survenue pendant le chargement de la fiche.<br />"
  298.                       .. "<a href=\"" .. url .. "\">Cliquez ici pour consulter la page sur Allocin├⌐.fr</a>.")
  299.         dlg:del_widget(html)
  300.         return
  301.     end
  302.  
  303.     -- Extract information
  304.     local last, _ = string.find(data, '<ul id="link_open"')
  305.     if not last then
  306.         last, _ = string.find(data, 'notationbar')
  307.     end
  308.     sub = string.sub(data, first, last-1)
  309.  
  310.     -- Clean data
  311.     sub = string.gsub(sub, "%s+", " ")
  312.     sub = string.gsub(sub, "</?p>", "<br/>")
  313.     sub = string.gsub(sub, "</?div[^>]*>", "")
  314.     sub = string.gsub(sub, "</?span[^>]*>", "")
  315.     sub = string.gsub(sub, "<%!%-%-[^%-]+%-%->", "")
  316.     sub = string.gsub(sub, "<br%s*/>%s*<br%s*/>", "<br/>")
  317.     page = string.gsub(sub, "Synopsis :.*$", "")
  318.  
  319.     -- Style
  320.     local synopsis = string.gsub(sub, ".*Synopsis :(.*)", "<h2>Synposis</h2>%1")
  321.  
  322.     -- Note
  323.     for w in string.gmatch(data, "property=\"v:average\"[^>]*>([^<]+)</span>") do
  324.         local note = trim(w)
  325.         page = page .. "Note moyenne: <b>" .. note .. " / 4</b>"
  326.         for y in string.gmatch(data, "property=\"v:count\"[^>]*>([^<]+)</span>") do
  327.            local nbpeople = trim(y)
  328.            page = page .. " (" .. nbpeople .. " votes)"
  329.            break
  330.         end
  331.         break
  332.     end
  333.  
  334.     -- Synopsis
  335.     page = page .. synopsis
  336.  
  337.     -- Movie title
  338.     if string.find(data, '<h1>.*</h1>') then
  339.         name = string.gsub(data, '^.*<h1>%s*(.*)%s*</h1>.*$', '%1')
  340.         name = trim(name)
  341.     end
  342.  
  343.     page = page .. "<h2>Source</h2>"
  344.     if name then
  345.         page = page .. name .. " sur <a href='" .. url .. "'>Allocin├⌐</a>"
  346.     else
  347.         page = page .. "<a href='" .. url .. "'>Allocin├⌐</a>"
  348.     end
  349.  
  350.     page = string.gsub(page, "href=([\"'])/", "href=%1http://www.allocine.fr/")
  351.     html:set_text(page)
  352. end
  353.