3 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <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%.%w+"
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 p
= Dailymotion
.fetch_page(self
, U
)
66 self
.title
= p
:match('"og:title" content="(.-)"')
67 or error("no match: media title")
69 self
.id
= p
:match("video/([^%?_]+)")
70 or error("no match: media ID")
72 self
.thumbnail_url
= p
:match('"og:image" content="(.-)"') or ''
74 local formats
= Dailymotion
.iter_formats(p
, U
)
75 local format = U
.choose_format(self
, formats
,
76 Dailymotion
.choose_best
,
77 Dailymotion
.choose_default
,
79 or error("unable to choose format")
80 self
.url
= {format.url
or error("no match: media URL")}
88 function Dailymotion
.fetch_page(self
, U
)
89 self
.page_url
= Dailymotion
.normalize(self
.page_url
)
91 local s
= self
.page_url
:match('[%?%&]urlback=(.+)')
93 self
.page_url
= 'http://dailymotion.com' .. U
.unescape(s
)
96 local opts
= {arbitrary_cookie
= 'family_filter=off'}
97 return quvi
.fetch(self
.page_url
, opts
)
100 function Dailymotion
.normalize(page_url
) -- "Normalize" embedded URLs
101 if page_url
:match("/swf/") then
102 page_url
= page_url
:gsub("/swf/", "/")
103 elseif page_url
:match("/embed/") then
104 page_url
= page_url
:gsub("/embed/", "/")
109 function Dailymotion
.iter_formats(page
, U
)
110 local seq
= page
:match('"sequence":"(.-)"')
111 or error('no match: sequence')
113 local e
= "no match: sequence"
114 if page
:match("_partnerplayer") then
115 e
= e
.. ": looks like a partner video which we do not support"
120 seq
= U
.unescape(seq
)
123 for url
in seq
:gmatch('%w+URL":"(.-)"') do
124 local c
,w
,h
,cn
= url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
126 url
= url
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
127 table.insert(t
, {width
=tonumber(w
), height
=tonumber(h
),
128 container
=cn
, codec
=string.lower(c
),
129 url
=url
:gsub("\\/", "/")})
135 error("no match: media URL")
141 function Dailymotion
.choose_default(formats
) -- Lowest quality available
142 local r
= {width
=0xffff, height
=0xffff, url
=nil}
143 local U
= require
'quvi/util'
144 for _
,v
in pairs(formats
) do
145 if U
.is_lower_quality(v
,r
) then
149 -- for k,v in pairs(r) do print(k,v) end
153 function Dailymotion
.choose_best(formats
) -- Highest quality available
154 local r
= {width
=0, height
=0, url
=nil}
155 local U
= require
'quvi/util'
156 for _
,v
in pairs(formats
) do
157 if U
.is_higher_quality(v
,r
) then
161 -- for k,v in pairs(r) do print(k,v) end
165 function Dailymotion
.to_s(t
)
166 return string.format("%s_%sp", t
.container
, t
.height
)
169 -- vim: set ts=4 sw=4 tw=72 expandtab: