2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program 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
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 -- "http://dai.ly/cityofscars",
22 -- "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms",
24 local Dailymotion
= {} -- Utility functions unique to this script.
26 -- Identify the media script.
29 can_parse_url
= Dailymotion
.can_parse_url(qargs
),
30 domains
= table.concat({'dailymotion.com'}, ',')
34 -- Parse media properties.
36 local U
= require
'quvi/util'
37 local p
= Dailymotion
.fetch_page(qargs
, U
)
39 qargs
.thumb_url
= p
:match('"og:image" content="(.-)"') or ''
40 qargs
.title
= p
:match('"og:title" content="(.-)"') or ''
41 qargs
.id
= p
:match("video/([^%?_]+)") or ''
43 qargs
.streams
= Dailymotion
.iter_streams(p
, U
)
52 function Dailymotion
.can_parse_url(qargs
)
53 Dailymotion
.normalize(qargs
)
54 local U
= require
'socket.url'
55 local t
= U
.parse(qargs
.input_url
)
56 if t
and t
.scheme
and t
.scheme
:lower():match('^http$')
58 t
.host
:lower():match('dailymotion%.com$')
59 or t
.host
:lower():match('dai%.ly$')
62 t
.path
:lower():match('^/video/')
63 or t
.path
:lower():match('^/%w+$')
64 or t
.path
:lower():match('/family_filter')
73 -- "Normalizes" the embedded URLs.
74 function Dailymotion
.normalize(qargs
)
75 qargs
.input_url
= qargs
.input_url
:gsub("/embed/", "/")
76 qargs
.input_url
= qargs
.input_url
:gsub("/swf/", "/")
79 -- Fetches the page contents from the media URL.
80 function Dailymotion
.fetch_page(qargs
, U
)
81 Dailymotion
.normalize(qargs
)
83 local s
= qargs
.input_url
:match('[%?%&]urlback=(.+)')
85 qargs
.input_url
= 'http://dailymotion.com' .. U
.unescape(s
)
88 quvi
.http
.header('Cookie: family_filter=off')
89 return quvi
.http
.fetch(qargs
.input_url
).data
92 -- Iterates the available streams.
93 function Dailymotion
.iter_streams(page
, U
)
95 local seq
= page
:match('"sequence":"(.-)"')
96 or error('no match: sequence')
100 for q
,u
in seq
:gmatch('"(%w%w)URL":"(.-)"') do
101 table.insert(urls
, {quality
=q
, url
=Dailymotion
.cleanup(U
, u
)})
104 -- Each media page should have at least have this, even if other
105 -- stream qualities are not available.
107 local u
= seq
:match('"video_url":"(.-)"')
108 or error('no match: media stream URL')
109 table.insert(urls
, {url
=Dailymotion
.cleanup(U
, u
)})
112 local S
= require
'quvi/stream'
115 for _
,v
in pairs(urls
) do
116 local c
,w
,h
,cn
= v
.url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
119 local t
= S
.stream_new(v
.url
)
121 t
.video
.encoding
= string.lower(c
or '')
122 t
.video
.height
= tonumber(h
)
123 t
.video
.width
= tonumber(w
)
124 t
.container
= cn
or ''
126 -- Must come after we have the video resolution, as the to_id
127 -- function uses the height property.
128 t
.id
= Dailymotion
.to_id(t
, v
.quality
)
135 Dailymotion
.ch_best(S
, r
)
141 -- Sanitizes the URL.
142 function Dailymotion
.cleanup(U
, u
)
144 u
= U
.slash_unescape(u
)
145 u
= u
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
149 -- Picks the stream with the highest video height property
150 -- as the best in quality.
151 function Dailymotion
.ch_best(S
, t
)
152 local r
= t
[1] -- Make the first one the 'best' by default.
154 for _
,v
in pairs(t
) do
155 if v
.video
.height
> r
.video
.height
then
156 r
= S
.swap_best(r
, v
)
161 -- Return an ID for a stream.
162 function Dailymotion
.to_id(t
, q
)
163 return string.format("%s_%s_%s_%sp",
164 (q
) and q
or 'sd', t
.container
, t
.video
.encoding
, t
.video
.height
)
167 -- vim: set ts=2 sw=2 tw=72 expandtab: