media/dailymotion.lua: Rewrite for can_parse_url
[libquvi-scripts.git] / share / media / dailymotion.lua
blobdbd9a7678127f83ec7561152d6ed8ae4ab939585
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 return {
30 can_parse_url = Dailymotion.can_parse_url(qargs),
31 domains = table.concat({'dailymotion.com'}, ',')
33 end
35 -- Parse media properties.
36 function parse(qargs)
37 local U = require 'quvi/util'
38 local p = Dailymotion.fetch_page(qargs, U)
40 qargs.thumb_url = p:match('"og:image" content="(.-)"') or ''
41 qargs.title = p:match('title="(.-)"') or ''
42 qargs.id = p:match("video/([^%?_]+)") or ''
44 qargs.streams = Dailymotion.iter_streams(p, U)
46 return qargs
47 end
50 -- Utility functions
53 function Dailymotion.can_parse_url(qargs)
54 Dailymotion.normalize(qargs)
55 local U = require 'quvi/url'
56 local t = U.parse(qargs.input_url)
57 if t and t.scheme and t.scheme:lower():match('^http$')
58 and t.host and (
59 t.host:lower():match('dailymotion%.com$')
60 or t.host:lower():match('dai%.ly$')
62 and t.path and (
63 t.path:lower():match('^/video/')
64 or t.path:lower():match('^/%w+$')
65 or t.path:lower():match('/family_filter')
67 then
68 return true
69 else
70 return false
71 end
72 end
74 -- "Normalizes" the embedded URLs.
75 function Dailymotion.normalize(qargs)
76 qargs.input_url = qargs.input_url:gsub("/embed/", "/")
77 qargs.input_url = qargs.input_url:gsub("/swf/", "/")
78 end
80 -- Fetches the page contents from the media URL.
81 function Dailymotion.fetch_page(qargs, U)
82 Dailymotion.normalize(qargs)
84 local s = qargs.input_url:match('[%?%&]urlback=(.+)')
85 if s then
86 qargs.input_url = 'http://dailymotion.com' .. U.unescape(s)
87 end
89 local C = require 'quvi/const'
90 local o = { [C.qfo_cookie] = 'family_filter=off' }
91 return quvi.fetch(qargs.input_url, o)
92 end
94 -- Iterates the available streams.
95 function Dailymotion.iter_streams(page, U)
97 local seq = page:match('"sequence":"(.-)"')
98 or error('no match: sequence')
99 seq = U.unescape(seq)
101 local urls = {}
102 for q,u in seq:gmatch('"(%w%w)URL":"(.-)"') do
103 table.insert(urls, {quality=q, url=Dailymotion.cleanup(U, u)})
106 -- Each media page should have at least have this, even if other
107 -- stream qualities are not available.
108 if #urls ==0 then
109 local u = seq:match('"video_url":"(.-)"')
110 or error('no match: media stream URL')
111 table.insert(urls, {url=Dailymotion.cleanup(U, u)})
114 local S = require 'quvi/stream'
115 local r = {}
117 for _,v in pairs(urls) do
118 local c,w,h,cn = v.url:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
120 if c then
121 local t = S.stream_new(v.url)
123 t.video.encoding = string.lower(c or '')
124 t.video.height = tonumber(h)
125 t.video.width = tonumber(w)
126 t.container = cn or ''
128 -- Must come after we have the video resolution, as the to_id
129 -- function uses the height property.
130 t.id = Dailymotion.to_id(t, v.quality)
132 table.insert(r, t)
136 if #r >1 then
137 Dailymotion.ch_best(S, r)
140 return r
143 -- Sanitizes the URL.
144 function Dailymotion.cleanup(U, u)
145 u = U.unescape(u)
146 u = U.slash_unescape(u)
147 u = u:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
148 return u
151 -- Picks the stream with the highest video height property
152 -- as the best in quality.
153 function Dailymotion.ch_best(S, t)
154 local r = t[1] -- Make the first one the 'best' by default.
155 r.flags.best = true
156 for _,v in pairs(t) do
157 if v.video.height > r.video.height then
158 r = S.swap_best(r, v)
163 -- Return an ID for a stream.
164 function Dailymotion.to_id(t, q)
165 return string.format("%s_%s_%s_%sp",
166 (q) and q or 'sd', t.container, t.video.encoding, t.video.height)
169 -- vim: set ts=2 sw=2 tw=72 expandtab: