3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of quvi <http://quvi.sourceforge.net/>.
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
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.
30 package
.path
= self
.script_dir
.. '/?.lua'
31 local C
= require
'quvi/const'
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"})
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
)
49 for _
,v
in pairs(formats
) do
50 table.insert(t
, Dailymotion
.to_s(v
))
54 self
.formats
= table.concat(t
, "|")
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
,
80 or error("no match: media url")}
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=(.+)')
94 self
.page_url
= 'http://dailymotion.com' .. U
.unescape(s
)
97 local opts
= {arbitrary_cookie
= 'family_filter=off'}
98 return quvi
.fetch(self
.page_url
, opts
)
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/", "/")
110 function Dailymotion
.iter_formats(page
, U
)
111 local _
,_
,seq
= page
:find('"sequence",%s+"(.-)"')
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"
119 seq
= U
.unescape(seq
)
121 local _
,_
,msg
= seq
:find('"message":"(.-)[<"]')
123 msg
= msg
:gsub('+',' ')
124 error(msg
:gsub('\\',''))
127 local _
,_
,vpp
= seq
:find('"videoPluginParameters":{(.-)}')
129 -- See also <http://sourceforge.net/apps/trac/clive/ticket/4>
130 error("no match: video plugin params")
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+)')
138 error('no match: codec, width, height, container')
141 table.insert(t
, {width
=tonumber(w
), height
=tonumber(h
),
142 container
=cn
, codec
=string.lower(c
),
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
157 -- for k,v in pairs(r) do print(k,v) end
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
169 -- for k,v in pairs(r) do print(k,v) end
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: