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 -- Parse media properties.
42 local U
= require
'quvi/util'
43 local p
= Dailymotion
.fetch_page(qargs
, U
)
45 qargs
.thumb_url
= p
:match('"og:image" content="(.-)"') or ''
46 qargs
.title
= p
:match('title="(.-)"') or ''
47 qargs
.id
= p
:match("video/([^%?_]+)") or ''
49 qargs
.streams
= Dailymotion
.iter_streams(p
, U
)
58 -- "Normalizes" the embedded URLs.
59 function Dailymotion
.normalize(input_url
)
60 if input_url
:match("/swf/") then
61 input_url
= input_url
:gsub("/swf/", "/")
62 elseif input_url
:match("/embed/") then
63 input_url
= input_url
:gsub("/embed/", "/")
68 -- Fetches the page contents from the media URL.
69 function Dailymotion
.fetch_page(qargs
, U
)
70 qargs
.input_url
= Dailymotion
.normalize(qargs
.input_url
)
72 local s
= qargs
.input_url
:match('[%?%&]urlback=(.+)')
74 qargs
.input_url
= 'http://dailymotion.com' .. U
.unescape(s
)
77 local o
= {arbitrary_cookie
= 'family_filter=off'}
78 return quvi
.fetch(qargs
.input_url
, o
)
81 -- Iterates the available streams.
82 function Dailymotion
.iter_streams(page
, U
)
84 local seq
= page
:match('"sequence",%s+"(.-)"')
85 or error('no match: sequence')
89 for q
,u
in seq
:gmatch('"(%w%w)URL":"(.-)"') do
90 table.insert(urls
, {quality
=q
, url
=Dailymotion
.cleanup(U
, u
)})
93 -- Each media page should have at least have this, even if other
94 -- stream qualities are not available.
96 local u
= seq
:match('"video_url":"(.-)"')
97 or error('no match: media stream URL')
98 table.insert(urls
, {url
=Dailymotion
.cleanup(U
, u
)})
101 local S
= require
'quvi/stream'
104 for _
,v
in pairs(urls
) do
105 local c
,w
,h
,cn
= v
.url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
108 local t
= S
.stream_new(v
.url
)
110 t
.video
.encoding
= string.lower(c
or '')
111 t
.video
.height
= tonumber(h
)
112 t
.video
.width
= tonumber(w
)
113 t
.container
= cn
or ''
115 -- Must come after we have the video resolution, as the to_fmt_id
116 -- function uses the height property.
117 t
.fmt_id
= Dailymotion
.to_fmt_id(t
, v
.quality
)
124 Dailymotion
.ch_best(S
, r
)
130 -- Sanitizes the URL.
131 function Dailymotion
.cleanup(U
, u
)
133 u
= U
.slash_unescape(u
)
134 u
= u
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
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: