Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / dailymotion.lua
blob325fd11f07ee779f998e82584f83c2f6e9591ced
2 -- quvi
3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of quvi <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 -- "http://dai.ly/cityofscars",
24 -- "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms",
26 local Dailymotion = {} -- Utility functions unique to this script.
28 -- Identify the script.
29 function ident(self)
30 package.path = self.script_dir .. '/?.lua'
31 local C = require 'quvi/const'
32 local r = {}
33 r.domain = "dailymotion."
34 r.formats = "default|best"
35 r.categories = C.proto_http
36 local U = require 'quvi/util'
37 r.handles = U.handles(self.page_url, {r.domain, "dai.ly"},
38 {"/video/","/%w+$","/family_filter"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 local U = require 'quvi/util'
45 local page = Dailymotion.fetch_page(self, U)
46 local formats = Dailymotion.iter_formats(page, U)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Dailymotion.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "|")
56 return self
57 end
59 -- Parse media URL.
60 function parse(self)
61 self.host_id = "dailymotion"
63 local U = require 'quvi/util'
64 local page = Dailymotion.fetch_page(self, U)
66 local _,_,s = page:find('title="(.-)"')
67 self.title = s or error("no match: media title")
69 local _,_,s = page:find("video/(.-)_")
70 self.id = s or error("no match: media id")
72 local _,_,s = page:find('"og:image" content="(.-)"')
73 self.thumbnail_url = s or ''
75 local formats = Dailymotion.iter_formats(page, U)
76 self.url = {U.choose_format(self, formats,
77 Dailymotion.choose_best,
78 Dailymotion.choose_default,
79 Dailymotion.to_s).url
80 or error("no match: media url")}
82 return self
83 end
86 -- Utility functions
89 function Dailymotion.fetch_page(self, U)
90 self.page_url = Dailymotion.normalize(self.page_url)
92 local _,_,s = self.page_url:find('/family_filter%?urlback=(.+)')
93 if s then
94 self.page_url = 'http://dailymotion.com' .. U.unescape(s)
95 end
97 local opts = {arbitrary_cookie = 'family_filter=off'}
98 return quvi.fetch(self.page_url, opts)
99 end
101 function Dailymotion.normalize(page_url) -- "Normalize" embedded URLs
102 if page_url:find("/swf/") then
103 page_url = page_url:gsub("/swf/", "/")
104 elseif page_url:find("/embed/") then
105 page_url = page_url:gsub("/embed/", "/")
107 return page_url
110 function Dailymotion.iter_formats(page, U)
111 local _,_,seq = page:find('"sequence",%s+"(.-)"')
112 if not seq then
113 local e = "no match: sequence"
114 if page:find("_partnerplayer") then
115 e = e .. ": looks like a partner video which we do not support"
117 error(e)
119 seq = U.unescape(seq)
121 local _,_,msg = seq:find('"message":"(.-)[<"]')
122 if msg then
123 msg = msg:gsub('+',' ')
124 error(msg:gsub('\\',''))
127 local _,_,vpp = seq:find('"videoPluginParameters":{(.-)}')
128 if not vpp then
129 -- See also <http://sourceforge.net/apps/trac/clive/ticket/4>
130 error("no match: video plugin params")
133 local t = {}
134 for url in vpp:gfind('%w+URL":"(.-)"') do
135 url = url:gsub("\\/", "/")
136 local _,_,c,w,h,cn = url:find('(%w+)%-(%d+)x(%d+).-%.(%w+)')
137 if not c then
138 error('no match: codec, width, height, container')
140 -- print(c,w,h,cn)
141 table.insert(t, {width=tonumber(w), height=tonumber(h),
142 container=cn, codec=string.lower(c),
143 url=url})
146 return t
149 function Dailymotion.choose_default(formats) -- Lowest quality available
150 local r = {width=0xffff, height=0xffff, url=nil}
151 local U = require 'quvi/util'
152 for _,v in pairs(formats) do
153 if U.is_lower_quality(v,r) then
154 r = v
157 -- for k,v in pairs(r) do print(k,v) end
158 return r
161 function Dailymotion.choose_best(formats) -- Highest quality available
162 local r = {width=0, height=0, url=nil}
163 local U = require 'quvi/util'
164 for _,v in pairs(formats) do
165 if U.is_higher_quality(v,r) then
166 r = v
169 -- for k,v in pairs(r) do print(k,v) end
170 return r
173 function Dailymotion.to_s(t)
174 return string.format("%s_%sp", t.container, t.height)
177 -- vim: set ts=4 sw=4 tw=72 expandtab: