Add Dailymotion.cleanup function
[libquvi-scripts.git] / share / lua / media / dailymotion.lua
bloba66b0246bb2bf2835243dfe8dbfb2c7556e07317
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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
19 -- 02110-1301 USA
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.
28 function ident(qargs)
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
33 local r = {
34 accepts = A.accepts(qargs.input_url, d, p),
35 categories = C.proto_http
37 return r
38 end
40 -- Parse media properties.
41 function parse(qargs)
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)
51 return qargs
52 end
55 -- Utility functions
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/", "/")
64 end
65 return input_url
66 end
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=(.+)')
73 if s then
74 qargs.input_url = 'http://dailymotion.com' .. U.unescape(s)
75 end
77 local o = {arbitrary_cookie = 'family_filter=off'}
78 return quvi.fetch(qargs.input_url, o)
79 end
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')
86 seq = U.unescape(seq)
88 local urls = {}
89 for q,u in seq:gmatch('"(%w%w)URL":"(.-)"') do
90 table.insert(urls, {quality=q, url=Dailymotion.cleanup(U, u)})
91 end
93 -- Each media page should have at least have this, even if other
94 -- stream qualities are not available.
95 if #urls ==0 then
96 local u = seq:match('"video_url":"(.-)"')
97 or error('no match: media stream URL')
98 table.insert(urls, {url=Dailymotion.cleanup(U, u)})
99 end
101 local S = require 'quvi/stream'
102 local r = {}
104 for _,v in pairs(urls) do
105 local c,w,h,cn = v.url:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
107 if c then
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)
119 table.insert(r, t)
123 if #r >1 then
124 Dailymotion.ch_best(S, r)
127 return r
130 -- Sanitizes the URL.
131 function Dailymotion.cleanup(U, u)
132 u = U.unescape(u)
133 u = U.slash_unescape(u)
134 u = u:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
135 return u
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
143 r = v
146 -- for k,v in pairs(r) do print(k,v) end
147 return r
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
155 r = v
158 -- for k,v in pairs(r) do print(k,v) end
159 return r
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: