PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/VLC-1.1.0-iOS/share/lua/extensions/allocine-fr.lua

https://bitbucket.org/megadevs/vlc-ios
Lua | 352 lines | 240 code | 46 blank | 66 comment | 54 complexity | ab689704e4b7a50df2d0e77ffddbab9b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. --[[
  2. Allocine Extension for VLC media player 1.1
  3. French website only
  4. Copyright © 2010 VideoLAN and AUTHORS
  5. Authors: Jean-Philippe André (jpeg@videolan.org)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. --]]
  18. -- Lua modules
  19. require "simplexml"
  20. -- Global variables
  21. dlg = nil -- Dialog
  22. title = nil -- Text input widget
  23. message = nil -- Label
  24. list = nil -- List widget
  25. okay = nil -- Okay button
  26. html = nil -- HTML box
  27. films = {}
  28. -- Extension description
  29. function descriptor()
  30. return { title = "Allociné (France)" ;
  31. version = "1.0" ;
  32. author = "VideoLAN" ;
  33. url = 'http://www.allocine.fr/';
  34. shortdesc = "Allocine.com";
  35. description = "<center><b>ALLOCINE.COM</b></center>"
  36. .. "Récupère des informations sur le média en cours "
  37. .. "de lecture depuis Allocine.fr, telles que :<br />"
  38. .. "- Casting,<br />- Résumé,<br />- Note des utilisateurs,"
  39. .. "<br />- Lien direct vers la fiche du film sur "
  40. .. "<a href=\"http://www.allocine.fr\">allocine.fr</a>." ;
  41. capabilities = { "input-listener", "meta-listener" } }
  42. end
  43. -- Activation hook
  44. function activate()
  45. vlc.msg.dbg("[ALLOCINE.COM] Welcome on Allocine.fr")
  46. create_dialog()
  47. end
  48. -- Deactivation hook
  49. function deactivate()
  50. vlc.msg.dbg("[ALLOCINE.COM] Bye bye!")
  51. end
  52. -- Dialog close hook
  53. function close()
  54. -- Deactivate this extension
  55. vlc.deactivate()
  56. end
  57. -- Input change hook
  58. function input_changed()
  59. title:set_text(get_title())
  60. end
  61. -- Meta change hook
  62. function meta_changed()
  63. title:set_text(get_title())
  64. end
  65. -- Create the dialog
  66. function create_dialog()
  67. dlg = vlc.dialog("ALLOCINE.COM")
  68. dlg:add_label("<b>Titre du film:</b>", 1, 1, 1, 1)
  69. title = dlg:add_text_input(get_title(), 2, 1, 1, 1)
  70. dlg:add_button("Rechercher", click_chercher, 3, 1, 1, 1)
  71. end
  72. -- Get clean title from filename
  73. function get_title(str)
  74. local item = vlc.item or vlc.input.item()
  75. if not item then
  76. return ""
  77. end
  78. local metas = item:metas()
  79. if metas["title"] then
  80. return metas["title"]
  81. else
  82. local filename = string.gsub(item:name(), "^(.+)%.%w+$", "%1")
  83. return trim(filename or item:name())
  84. end
  85. end
  86. -- Remove leading and trailing spaces
  87. function trim(str)
  88. if not str then return "" end
  89. return string.gsub(str, "^%s*(.-)%s*$", "%1")
  90. end
  91. -- Lookup for this movie title
  92. function click_chercher()
  93. -- Get title
  94. local name = title:get_text()
  95. if not name or name == "" then
  96. vlc.msg.dbg("[ALLOCINE.COM] No title")
  97. return
  98. end
  99. -- Update dialog
  100. if list then
  101. dlg:del_widget(list)
  102. list = nil
  103. end
  104. -- Transform spaces and dots into +
  105. name = string.gsub(string.gsub(name, "[%p%s%c]", "+"), "%++", "+")
  106. -- Build URL
  107. local url = "http://www.allocine.fr/recherche/?q=" .. name
  108. -- Please wait...
  109. local message_text = "Recherche <a href=\"" .. url .. "\">" .. string.gsub(name, "%+", " ") .. "</a> sur Allociné..."
  110. if not message then
  111. message = dlg:add_label(message_text, 1, 2, 3, 1)
  112. else
  113. message:set_text(message_text)
  114. end
  115. if list then dlg:del_widget(list) end
  116. if okay then dlg:del_widget(okay) end
  117. if html then dlg:del_widget(html) end
  118. list = nil
  119. okay = nil
  120. html = nil
  121. dlg:update()
  122. -- Open URL
  123. local s, msg = vlc.stream(url)
  124. if not s then
  125. vlc.msg.warn("[ALLOCINE.COM] " .. msg)
  126. end
  127. -- Fetch HTML data (max 65 kb)
  128. local data = s:read(65535)
  129. -- Clean data
  130. data = string.gsub(data, "<b>", "")
  131. data = string.gsub(data, "</b>", "")
  132. data = string.gsub(data, "%s+", " ")
  133. -- Data storage
  134. films = {}
  135. -- Find categories
  136. for category in string.gmatch(data, "<[hH]2>%s*([^<]+)%s*</[hH]2>") do
  137. local category = trim(category)
  138. -- Split substring corresponding to this table
  139. local _, first = string.find(data, "<[hH]2>%s*" .. category .. "%s*</[hH]2>")
  140. first, _ = string.find(data, "<table", first)
  141. local _, last = string.find(data, "</table>", first)
  142. -- Find movies and TV shows
  143. if category == "Films" or category == "Séries TV" then
  144. -- Read <table> tag as xml
  145. local substring = string.sub(data, first, last or -1)
  146. local xml = simplexml.parse_string(substring)
  147. for _, tr in ipairs(xml.children) do
  148. -- Get film title & year
  149. local film_title = nil
  150. local film_year = nil
  151. local node = tr.children[2] -- td
  152. if node then node = node.children[1] end -- div (1)
  153. if node then node = node.children[1] end -- div (2)
  154. local subnode = nil
  155. if node then
  156. for _, subnode in ipairs(node.children) do
  157. if subnode.name == "a" and type(subnode.children[1]) == "string" then
  158. film_title = trim(subnode.children[1]) -- content of a tag
  159. else if subnode.name == "span" and type(subnode.children[1]) == "string" then
  160. film_year = trim(subnode.children[1])
  161. end end
  162. end
  163. end
  164. -- Get film cover & URL
  165. local film_image = nil
  166. local film_url = nil
  167. local node = tr.children[1] -- td
  168. if node then node = node.children[1] end -- a
  169. if node and node.name == "a" then
  170. film_url = node.attributes["href"]
  171. node = node.children[1]
  172. if node and node.name == "img" then
  173. film_image = node.attributes["src"]
  174. end
  175. end
  176. -- Append fetched information
  177. if film_title then
  178. if string.sub(film_url, 1, 4) ~= "http" then
  179. film_url = "http://www.allocine.fr" .. film_url
  180. end
  181. films[#films+1] = { url = film_url ; image = film_image ; year = film_year ; title = film_title }
  182. end
  183. end
  184. end
  185. end
  186. -- Print information
  187. -- No results found
  188. if #films == 0 then
  189. message_text = "<center>Aucun résultat trouvé pour <b>" .. string.gsub(name, "%+", " ") .. "</b>.</center>"
  190. .. "Vous pouvez aussi chercher directement sur <a href=\"" .. url .. "\">Allociné</a>."
  191. message:set_text(message_text)
  192. end
  193. -- Only one movie or TV show matches, let's open its page directly
  194. if #films == 1 then
  195. message_text = "<center><a href=\"" .. films[1].url .. "\">" .. films[1].title .. "</a></center>"
  196. message:set_text(message_text)
  197. dlg:update()
  198. open_fiche(films[1].url)
  199. end
  200. -- More than 1 match, display a list
  201. if #films > 1 then
  202. message_text = tostring(#films) .. " films ou séries TV trouvés sur Allociné :"
  203. message:set_text(message_text)
  204. list = dlg:add_list(1, 3, 3, 1)
  205. for idx, film in ipairs(films) do
  206. local txt = film.title
  207. if film.year then txt = txt .. " (" .. film.year .. ")" end
  208. list:add_value(txt, idx)
  209. end
  210. okay = dlg:add_button("Voir la fiche", click_okay, 3, 4, 1, 1)
  211. end
  212. end
  213. -- Click after selection
  214. function click_okay()
  215. if not films or not #films then return end
  216. local selection = list:get_selection()
  217. if not selection then return end
  218. local sel, _ = next(selection, nil)
  219. if not sel then return end
  220. message_text = "<center><a href=\"" .. films[sel].url .. "\">" .. films[sel].title .. "</a></center>"
  221. message:set_text(message_text)
  222. dlg:update()
  223. open_fiche(films[sel].url)
  224. end
  225. -- Open a movie's information page
  226. function open_fiche(url)
  227. if okay then
  228. dlg:del_widget(okay)
  229. okay = nil
  230. end
  231. if list then
  232. dlg:del_widget(list)
  233. list = nil
  234. end
  235. if not html then
  236. html = dlg:add_html("<center><i>Chargement en cours...</i></center>", 1, 3, 3, 1)
  237. end
  238. dlg:update()
  239. -- Open stream
  240. local s = vlc.stream(url)
  241. -- Read max 500k (Note: 65k is not enough for the average note)
  242. local data = s:read(500000)
  243. -- Buffer & temp variables
  244. local first = nil
  245. local last = nil
  246. local page = nil
  247. local sub = nil
  248. local name = nil
  249. first, _ = string.find(data, '<div class="rubric">')
  250. if not first then
  251. message:set_text("<h2>Erreur !</h2>Désolé, une erreur est survenue pendant le chargement de la fiche.<br />"
  252. .. "<a href=\"" .. url .. "\">Cliquez ici pour consulter la page sur Allociné.fr</a>.")
  253. dlg:del_widget(html)
  254. return
  255. end
  256. -- Extract information
  257. local last, _ = string.find(data, '<ul id="link_open"')
  258. if not last then
  259. last, _ = string.find(data, 'notationbar')
  260. end
  261. sub = string.sub(data, first, last-1)
  262. -- Clean data
  263. sub = string.gsub(sub, "%s+", " ")
  264. sub = string.gsub(sub, "</?p>", "<br/>")
  265. sub = string.gsub(sub, "</?div[^>]*>", "")
  266. sub = string.gsub(sub, "</?span[^>]*>", "")
  267. sub = string.gsub(sub, "<%!%-%-[^%-]+%-%->", "")
  268. sub = string.gsub(sub, "<br%s*/>%s*<br%s*/>", "<br/>")
  269. page = string.gsub(sub, "Synopsis :.*$", "")
  270. -- Style
  271. local synopsis = string.gsub(sub, ".*Synopsis :(.*)", "<h2>Synposis</h2>%1")
  272. -- Note
  273. first, _ = string.find(data, "Note Moyenne:")
  274. if first then
  275. local _, note = string.find(data, "span class=\"lighten\">%(", first)
  276. if note then
  277. note = string.sub(data, note+1, note+3)
  278. note = string.gsub(note, "%).*$", "")
  279. page = page .. "Note moyenne: <b>" .. note .. " / 4</b>"
  280. local nbpeople = string.gsub(data, ".*pour (%d+) notes.*", "%1")
  281. if nbpeople then
  282. page = page .. " (" .. nbpeople .. " votes)"
  283. end
  284. end
  285. end
  286. -- Synopsis
  287. page = page .. synopsis
  288. -- Movie title
  289. if string.find(data, '<h1>.*</h1>') then
  290. name = string.gsub(data, '^.*<h1>%s*(.*)%s*</h1>.*$', '%1')
  291. name = trim(name)
  292. end
  293. page = page .. "<h2>Source</h2>"
  294. if name then
  295. page = page .. name .. " sur <a href='" .. url .. "'>Allociné</a>"
  296. else
  297. page = page .. "<a href='" .. url .. "'>Allociné</a>"
  298. end
  299. page = string.gsub(page, "href=([\"'])/", "href=%1http://www.allocine.fr/")
  300. html:set_text(page)
  301. end