ard.lua: Cleanup Ard.iter_formats
[libquvi-scripts.git] / share / lua / website / imdb.lua
blob0a932ab74881ebe52a68c225066e711ec978b3fd
1 --
2 -- libquvi-scripts
3 -- Copyright (C) 2011 quvi project
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
23 local IMDB = {} -- Utility functions unique to this script
25 -- Identify the script.
26 function ident(self)
27 package.path = self.script_dir .. '/?.lua'
28 local C = require 'quvi/const'
29 local r = {}
30 r.domain = 'imdb%.com'
31 r.formats = 'default|best'
32 local B = require 'quvi/bit'
33 r.categories = B.bit_or(C.proto_http, C.proto_rtmp)
34 local U = require 'quvi/util'
35 r.handles = U.handles(self.page_url,
36 {r.domain},
37 {"/video/.+"})
38 return r
39 end
41 -- Query available formats.
42 function query_formats(self)
43 local page = quvi.fetch(self.page_url)
44 local formats = IMDB.iter_formats(page)
46 local t = {}
47 for _,v in pairs(formats) do
48 table.insert(t, IMDB.to_s(v))
49 end
51 table.sort(t)
52 self.formats = table.concat(t, "|")
54 return self
55 end
57 -- Parse media URL.
58 function parse(self)
59 self.host_id = 'imdb'
61 self.id = self.page_url:match('/video/%w+/(vi%d-)/')
63 local page = quvi.fetch(self.page_url)
64 self.title = page:match('<title>(.-) %- IMDb</title>')
67 -- Format codes (for most videos):
68 -- uff=1 - 480p RTMP
69 -- uff=2 - 480p HTTP
70 -- uff=3 - 720p HTTP
72 local formats = IMDB.iter_formats(page)
73 local U = require 'quvi/util'
74 local format = U.choose_format(self, formats,
75 IMDB.choose_best,
76 IMDB.choose_default,
77 IMDB.to_s)
78 or error("unable to choose format")
79 if not format.path then
80 error("no match: media url")
81 end
83 local url = 'http://www.imdb.com' .. format.path
84 local iframe = quvi.fetch(url, {fetch_type = 'config'})
85 local file = iframe:match('so%.addVariable%("file", "(.-)"%);')
86 file = U.unescape(file)
88 if file:match('^http.-') then
89 self.url = {file}
90 else
91 local path = iframe:match('so%.addVariable%("id", "(.-)"%);')
92 path = U.unescape(path)
93 self.url = {file .. path}
94 end
96 -- Optional: we can live without these
98 local thumb = iframe:match('so%.addVariable%("image", "(.-)"%);')
99 self.thumbnail_url = thumb or ''
101 return self
105 -- Utility functions
108 function IMDB.iter_formats(page)
109 local p = "case '(.-)' :.-url = '(.-)';"
110 local t = {}
111 for c,u in page:gmatch(p) do
112 table.insert(t, {path=u, container=c})
113 --print(u,c)
116 -- First item is ad, so remove it
117 table.remove(t, 1)
119 return t
122 function IMDB.choose_best(t) -- Expect the last to be the 'best'
123 return t[#t]
126 function IMDB.choose_default(t) -- Expect the second to be the 'default'
127 return t[2]
130 function IMDB.to_s(t)
131 return t.container
134 -- vim: set ts=4 sw=4 tw=72 expandtab: