2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 -- "http://dai.ly/cityofscars",
23 -- "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms",
25 local Dailymotion
= {} -- Utility functions unique to this script.
27 -- Identify the media script.
29 local A
= require
'quvi/accepts'
30 local C
= require
'quvi/const'
31 local d
= {"dailymotion%.%w+", "dai.ly"} -- domains
32 local p
= {"/video/", "/%w+$", "/family_filter"} -- paths
34 accepts
= A
.accepts(qargs
.input_url
, d
, p
),
35 categories
= C
.proto_http
40 -- Query available formats.
41 function query_formats(self
)
42 local U
= require
'quvi/util'
43 local page
= Dailymotion
.fetch_page(self
, U
)
44 local formats
= Dailymotion
.iter_formats(page
, U
)
47 for _
,v
in pairs(formats
) do
48 table.insert(t
, Dailymotion
.to_s(v
))
52 self
.formats
= table.concat(t
, "|")
59 self
.host_id
= "dailymotion"
61 local U
= require
'quvi/util'
62 local p
= Dailymotion
.fetch_page(self
, U
)
64 self
.title
= p
:match('title="(.-)"')
65 or error("no match: media title")
67 self
.id
= p
:match("video/([^%?_]+)")
68 or error("no match: media ID")
70 self
.thumbnail_url
= p
:match('"og:image" content="(.-)"') or ''
72 local formats
= Dailymotion
.iter_formats(p
, U
)
73 local format = U
.choose_format(self
, formats
,
74 Dailymotion
.choose_best
,
75 Dailymotion
.choose_default
,
77 or error("unable to choose format")
78 self
.url
= {format.url
or error("no match: media URL")}
86 function Dailymotion
.fetch_page(self
, U
)
87 self
.page_url
= Dailymotion
.normalize(self
.page_url
)
89 local s
= self
.page_url
:match('[%?%&]urlback=(.+)')
91 self
.page_url
= 'http://dailymotion.com' .. U
.unescape(s
)
94 local opts
= {arbitrary_cookie
= 'family_filter=off'}
95 return quvi
.fetch(self
.page_url
, opts
)
98 function Dailymotion
.normalize(page_url
) -- "Normalize" embedded URLs
99 if page_url
:match("/swf/") then
100 page_url
= page_url
:gsub("/swf/", "/")
101 elseif page_url
:match("/embed/") then
102 page_url
= page_url
:gsub("/embed/", "/")
107 function Dailymotion
.iter_formats(page
, U
)
108 local seq
= page
:match('"sequence",%s+"(.-)"')
110 local e
= "no match: sequence"
111 if page
:match("_partnerplayer") then
112 e
= e
.. ": looks like a partner video which we do not support"
117 seq
= U
.unescape(seq
)
120 for url
in seq
:gmatch('%w+URL":"(.-)"') do
121 local c
,w
,h
,cn
= url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
123 url
= url
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
124 table.insert(t
, {width
=tonumber(w
), height
=tonumber(h
),
125 container
=cn
, codec
=string.lower(c
),
126 url
=url
:gsub("\\/", "/")})
132 error("no match: media URL")
138 function Dailymotion
.choose_default(formats
) -- Lowest quality available
139 local r
= {width
=0xffff, height
=0xffff, url
=nil}
140 local U
= require
'quvi/util'
141 for _
,v
in pairs(formats
) do
142 if U
.is_lower_quality(v
,r
) then
146 -- for k,v in pairs(r) do print(k,v) end
150 function Dailymotion
.choose_best(formats
) -- Highest quality available
151 local r
= {width
=0, height
=0, url
=nil}
152 local U
= require
'quvi/util'
153 for _
,v
in pairs(formats
) do
154 if U
.is_higher_quality(v
,r
) then
158 -- for k,v in pairs(r) do print(k,v) end
162 function Dailymotion
.to_s(t
)
163 return string.format("%s_%sp", t
.container
, t
.height
)
166 -- vim: set ts=4 sw=4 tw=72 expandtab: