dailymotion.lua: Make urlback pattern more liberal
[libquvi-scripts.git] / share / lua / website / dailymotion.lua
blobcd3ce523de9835abe8380f5c1c6c8becdccafcac
2 -- libquvi-scripts
3 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
23 -- "http://dai.ly/cityofscars",
24 -- "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms",
26 local Dailymotion = {} -- Utility functions unique to this script.
28 -- Identify the script.
29 function ident(self)
30 package.path = self.script_dir .. '/?.lua'
31 local C = require 'quvi/const'
32 local r = {}
33 r.domain = "dailymotion%.%w+"
34 r.formats = "default|best"
35 r.categories = C.proto_http
36 local U = require 'quvi/util'
37 r.handles = U.handles(self.page_url, {r.domain, "dai.ly"},
38 {"/video/","/%w+$","/family_filter"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 local U = require 'quvi/util'
45 local page = Dailymotion.fetch_page(self, U)
46 local formats = Dailymotion.iter_formats(page, U)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Dailymotion.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "|")
56 return self
57 end
59 -- Parse media URL.
60 function parse(self)
61 self.host_id = "dailymotion"
63 local U = require 'quvi/util'
64 local p = Dailymotion.fetch_page(self, U)
66 self.title = p:match('title="(.-)"')
67 or error("no match: media title")
69 self.id = p:match("video/([^%?_]+)")
70 or error("no match: media ID")
72 self.thumbnail_url = p:match('"og:image" content="(.-)"') or ''
74 local formats = Dailymotion.iter_formats(p, U)
75 local format = U.choose_format(self, formats,
76 Dailymotion.choose_best,
77 Dailymotion.choose_default,
78 Dailymotion.to_s)
79 or error("unable to choose format")
80 self.url = {format.url or error("no match: media URL")}
81 return self
82 end
85 -- Utility functions
88 function Dailymotion.fetch_page(self, U)
89 self.page_url = Dailymotion.normalize(self.page_url)
91 local s = self.page_url:match('[%?%&]urlback=(.+)')
92 if s then
93 self.page_url = 'http://dailymotion.com' .. U.unescape(s)
94 end
96 local opts = {arbitrary_cookie = 'family_filter=off'}
97 return quvi.fetch(self.page_url, opts)
98 end
100 function Dailymotion.normalize(page_url) -- "Normalize" embedded URLs
101 if page_url:match("/swf/") then
102 page_url = page_url:gsub("/swf/", "/")
103 elseif page_url:match("/embed/") then
104 page_url = page_url:gsub("/embed/", "/")
106 return page_url
109 function Dailymotion.iter_formats(page, U)
110 local seq = page:match('"sequence",%s+"(.-)"')
111 if not seq then
112 local e = "no match: sequence"
113 if page:match("_partnerplayer") then
114 e = e .. ": looks like a partner video which we do not support"
116 error(e)
119 seq = U.unescape(seq)
121 local t = {}
122 for url in seq:gmatch('%w+URL":"(.-)"') do
123 local c,w,h,cn = url:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
124 if c then
125 url = url:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
126 table.insert(t, {width=tonumber(w), height=tonumber(h),
127 container=cn, codec=string.lower(c),
128 url=url:gsub("\\/", "/")})
129 -- print(c,w,h,cn)
133 if #t == 0 then
134 error("no match: media URL")
137 return t
140 function Dailymotion.choose_default(formats) -- Lowest quality available
141 local r = {width=0xffff, height=0xffff, url=nil}
142 local U = require 'quvi/util'
143 for _,v in pairs(formats) do
144 if U.is_lower_quality(v,r) then
145 r = v
148 -- for k,v in pairs(r) do print(k,v) end
149 return r
152 function Dailymotion.choose_best(formats) -- Highest quality available
153 local r = {width=0, height=0, url=nil}
154 local U = require 'quvi/util'
155 for _,v in pairs(formats) do
156 if U.is_higher_quality(v,r) then
157 r = v
160 -- for k,v in pairs(r) do print(k,v) end
161 return r
164 function Dailymotion.to_s(t)
165 return string.format("%s_%sp", t.container, t.height)
168 -- vim: set ts=4 sw=4 tw=72 expandtab: