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 d
= {"dailymotion%.%w+", "dai.ly"} -- domains
31 local p
= {"/video/", "/%w+$", "/family_filter"} -- paths
33 accepts
= A
.accepts(qargs
.input_url
, d
, p
)
38 -- Parse media properties.
40 local U
= require
'quvi/util'
41 local p
= Dailymotion
.fetch_page(qargs
, U
)
43 qargs
.thumb_url
= p
:match('"og:image" content="(.-)"') or ''
44 qargs
.title
= p
:match('title="(.-)"') or ''
45 qargs
.id
= p
:match("video/([^%?_]+)") or ''
47 qargs
.streams
= Dailymotion
.iter_streams(p
, U
)
56 -- "Normalizes" the embedded URLs.
57 function Dailymotion
.normalize(input_url
)
58 if input_url
:match("/swf/") then
59 input_url
= input_url
:gsub("/swf/", "/")
60 elseif input_url
:match("/embed/") then
61 input_url
= input_url
:gsub("/embed/", "/")
66 -- Fetches the page contents from the media URL.
67 function Dailymotion
.fetch_page(qargs
, U
)
68 qargs
.input_url
= Dailymotion
.normalize(qargs
.input_url
)
70 local s
= qargs
.input_url
:match('[%?%&]urlback=(.+)')
72 qargs
.input_url
= 'http://dailymotion.com' .. U
.unescape(s
)
75 local C
= require
'quvi/const'
76 local o
= { [C
.qfo_cookie
] = 'family_filter=off' }
77 return quvi
.fetch(qargs
.input_url
, o
)
80 -- Iterates the available streams.
81 function Dailymotion
.iter_streams(page
, U
)
83 local seq
= page
:match('"sequence":"(.-)"')
84 or error('no match: sequence')
88 for q
,u
in seq
:gmatch('"(%w%w)URL":"(.-)"') do
89 table.insert(urls
, {quality
=q
, url
=Dailymotion
.cleanup(U
, u
)})
92 -- Each media page should have at least have this, even if other
93 -- stream qualities are not available.
95 local u
= seq
:match('"video_url":"(.-)"')
96 or error('no match: media stream URL')
97 table.insert(urls
, {url
=Dailymotion
.cleanup(U
, u
)})
100 local S
= require
'quvi/stream'
103 for _
,v
in pairs(urls
) do
104 local c
,w
,h
,cn
= v
.url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
107 local t
= S
.stream_new(v
.url
)
109 t
.video
.encoding
= string.lower(c
or '')
110 t
.video
.height
= tonumber(h
)
111 t
.video
.width
= tonumber(w
)
112 t
.container
= cn
or ''
114 -- Must come after we have the video resolution, as the to_id
115 -- function uses the height property.
116 t
.id
= Dailymotion
.to_id(t
, v
.quality
)
123 Dailymotion
.ch_best(S
, r
)
129 -- Sanitizes the URL.
130 function Dailymotion
.cleanup(U
, u
)
132 u
= U
.slash_unescape(u
)
133 u
= u
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
137 -- Picks the stream with the highest video height property
138 -- as the best in quality.
139 function Dailymotion
.ch_best(S
, t
)
140 local r
= t
[1] -- Make the first one the 'best' by default.
142 for _
,v
in pairs(t
) do
143 if v
.video
.height
> r
.video
.height
then
144 r
= S
.swap_best(r
, v
)
149 -- Return an ID for a stream.
150 function Dailymotion
.to_id(t
, q
)
151 return string.format("%s_%s_%s_%sp",
152 (q
) and q
or 'sd', t
.container
, t
.video
.encoding
, t
.video
.height
)
155 -- vim: set ts=2 sw=2 tw=72 expandtab: