2 Get information about a movie from IMDb
4 Copyright © 2009-2010 VideoLAN and AUTHORS
6 Authors: Jean-Philippe André (jpeg@videolan.org)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 return { title = "IMDb - The Internet Movie Database" ;
28 author = "Jean-Philippe André" ;
29 url = 'http://www.imdb.org/';
30 description = "<center><b>The Internet Movie Database</b></center>\n"
31 .. "Get information about movies from the Internet "
32 .. "Movie Database (IMDb).\nThis Extension will show "
33 .. "you the cast, a short plot summary and a link to "
34 .. "the web page on imdb.org." ;
38 -- Update title text field. Removes file extensions.
39 function update_title()
40 local item = vlc.item()
41 local title = item and item:name()
43 title = string.gsub(title, "(.*)(%.%w+)$", "%1")
50 function create_dialog()
51 dlg = vlc.dialog("IMDb Search")
52 dlg:add_label("The Internet Movie Database", 1, 1, 4, 1)
53 dlg:add_label("<b>Movie Title</b>", 1, 2, 1, 1)
54 local item = vlc.item()
55 txt = dlg:add_text_input(item and item:name() or "", 2, 2, 1, 1)
56 dlg:add_button("Okay", "click_okay", 3, 2, 1, 1)
57 dlg:add_button("*", "update_title", 4, 2, 1, 1)
58 dlg:show() -- Show, if not already visible
70 -- Deactivate this extension
74 -- Some global variables: widgets
81 vlc.msg.dbg("Searching for " .. txt:get_text() .. " on IMDb")
89 list = dlg:add_list(1, 3, 4, 1)
90 button_open = dlg:add_button("Open", "click_open", 1, 4, 4, 1)
93 -- Clear previous results
97 local url = "http://www.imdb.com/find?s=all&q="
98 local title = string.gsub(txt:get_text(), " ", "+")
99 local s = vlc.stream(url .. title)
102 local data = s:read(65000)
109 while idxEnd ~= nil do
111 _, idxEnd, titleType = string.find(data, "<b>([^<]*Titles[^<]*)</b>", idxEnd)
112 _, _, nextTitle = string.find(data, "<b>([^<]*Titles[^<]*)</b>", idxEnd)
113 if not titleType then
116 -- Find current scope
117 if not nextTitle then
118 _, _, table = string.find(data, "<table>(.*)</table>", idxEnd)
120 nextTitle = string.gsub(nextTitle, "%(", "%%(")
121 nextTitle = string.gsub(nextTitle, "%)", "%%)")
122 _, _, table = string.find(data, "<table>(.*)</table>.*"..nextTitle, idxEnd)
124 -- Find all titles in this scope
125 if not table then break end
128 _, _, link = string.find(table, "<a href=\"([^\"]+title[^\"]+)\"", pos)
129 if not link then break end -- this would not be normal behavior...
130 _, pos, title = string.find(table, "<a href=\"" .. link .. "\"[^>]*>([^<]+)</a>", pos)
131 if not title then break end -- this would not be normal behavior...
132 _, _, year = string.find(table, "\((%d+)\)", pos)
133 -- Add this title to the list
135 _, _, imdbID = string.find(link, "/([^/]+)/$")
136 title = replace_html_chars(title)
137 titles[count] = { id = imdbID ; title = title ; year = year ; link = link }
142 for idx, title in ipairs(titles) do
143 list:add_value("[" .. title.id .. "] " .. title.title .. " (" .. title.year .. ")", idx)
147 function click_open()
148 selection = list:get_selection()
149 if not selection then return 1 end
151 html = dlg:add_html("Loading IMDb page...", 1, 3, 4, 1)
152 -- userLink = dlg:add_label("", 1, 4, 5, 1)
156 dlg:del_widget(button_open)
161 for idx, selectedItem in pairs(selection) do
165 imdbID = titles[sel].id
166 url = "http://www.imdb.org/title/" .. imdbID .. "/"
168 -- userLink:set_text("<a href=\"url\">" .. url .. "</a>")
170 local s = vlc.stream(url)
173 text = "<h1>" .. titles[sel].title .. " (" .. titles[sel].year .. ")</h1>"
174 text = text .. "<h2>Overview</h2><table>"
178 _, nextIdx, _ = string.find(data, "<div id=\"director-info\"", 1, true)
180 _, _, director = string.find(data, "<a href[^>]+>([%w%s]+)</a>", nextIdx)
183 director = "(Unknown)"
185 text = text .. "<tr><td><b>Director</b></td><td>" .. director .. "</td></tr>"
188 local genres = "<tr><td><b>Genres</b></td>"
190 for genre, _ in string.gmatch(data, "/Sections/Genres/(%w+)/\">") do
192 genres = genres .. "<td>" .. genre .. "</td></tr>"
194 genres = genres .. "<tr><td /><td>" .. genre .. "</td></tr>"
198 text = text .. genres
201 local actors = "<tr><td><b>Cast</b></td>"
203 for nm, char in string.gmatch(data, "<td class=\"nm\"><a[^>]+>([%w%s]+)</a></td><td class=\"ddd\"> ... </td><td class=\"char\"><a[^>]+>([%w%s]+)</a>") do
205 actors = actors .. "<tr><td />"
207 actors = actors .. "<td>" .. nm .. "</td><td><i>" .. char .. "</i></td></tr>"
210 text = text .. actors .. "</table>"
212 text = text .. "<h2>Plot Summary</h2>"
213 s = vlc.stream(url .. "plotsummary")
216 -- We read only the first summary
217 _, _, summary = string.find(data, "<p class=\"plotpar\">([^<]+)")
219 summary = "(Unknown)"
221 text = text .. "<p>" .. summary .. "</p>"
222 text = text .. "<p><h2>Source IMDb</h2><a href=\"" .. url .. "\">" .. url .. "</a></p>"
227 -- Convert some HTML characters into UTF8
228 function replace_html_chars(txt)
229 if not txt then return nil end
230 -- return vlc.strings.resolve_xml_special_chars(txt)
231 for num in string.gmatch(txt, "&#x(%x+);") do
232 -- Convert to decimal (any better way?)
234 for c in string.gmatch(num, "%x") do
235 cc = string.byte(c) - string.byte("0")
236 if (cc >= 10 or cc < 0) then
237 cc = string.byte(string.lower(c)) - string.byte("a") + 10
241 txt = string.gsub(txt, "&#x" .. num .. ";", string.char(dec))