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
.qmspc_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 C
= require
'quvi/const'
78 local o
= { [C
.qfo_cookie
] = 'family_filter=off' }
79 return quvi
.fetch(qargs
.input_url
, o
)
82 -- Iterates the available streams.
83 function Dailymotion
.iter_streams(page
, U
)
85 local seq
= page
:match('"sequence":"(.-)"')
86 or error('no match: sequence')
90 for q
,u
in seq
:gmatch('"(%w%w)URL":"(.-)"') do
91 table.insert(urls
, {quality
=q
, url
=Dailymotion
.cleanup(U
, u
)})
94 -- Each media page should have at least have this, even if other
95 -- stream qualities are not available.
97 local u
= seq
:match('"video_url":"(.-)"')
98 or error('no match: media stream URL')
99 table.insert(urls
, {url
=Dailymotion
.cleanup(U
, u
)})
102 local S
= require
'quvi/stream'
105 for _
,v
in pairs(urls
) do
106 local c
,w
,h
,cn
= v
.url
:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
109 local t
= S
.stream_new(v
.url
)
111 t
.video
.encoding
= string.lower(c
or '')
112 t
.video
.height
= tonumber(h
)
113 t
.video
.width
= tonumber(w
)
114 t
.container
= cn
or ''
116 -- Must come after we have the video resolution, as the to_id
117 -- function uses the height property.
118 t
.id
= Dailymotion
.to_id(t
, v
.quality
)
125 Dailymotion
.ch_best(S
, r
)
131 -- Sanitizes the URL.
132 function Dailymotion
.cleanup(U
, u
)
134 u
= U
.slash_unescape(u
)
135 u
= u
:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
139 -- Picks the stream with the highest video height property
140 -- as the best in quality.
141 function Dailymotion
.ch_best(S
, t
)
142 local r
= t
[1] -- Make the first one the 'best' by default.
144 for _
,v
in pairs(t
) do
145 if v
.video
.height
> r
.video
.height
then
146 r
= S
.swap_best(r
, v
)
151 -- Return an ID for a stream.
152 function Dailymotion
.to_id(t
, q
)
153 return string.format("%s_%s_%s_%sp",
154 (q
) and q
or 'sd', t
.container
, t
.video
.encoding
, t
.video
.height
)
157 -- vim: set ts=2 sw=2 tw=72 expandtab: