ident: Rewrite using the "accepts" function
[libquvi-scripts.git] / share / lua / media / dailymotion.lua
blob295305218e8f9f33e5610d65a46fc9a020601273
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 -- Query available formats.
41 function query_formats(self)
42 local U = require 'quvi/util'
43 local page = Dailymotion.fetch_page(self, U)
44 local formats = Dailymotion.iter_formats(page, U)
46 local t = {}
47 for _,v in pairs(formats) do
48 table.insert(t, Dailymotion.to_s(v))
49 end
51 table.sort(t)
52 self.formats = table.concat(t, "|")
54 return self
55 end
57 -- Parse media URL.
58 function parse(self)
59 self.host_id = "dailymotion"
61 local U = require 'quvi/util'
62 local p = Dailymotion.fetch_page(self, U)
64 self.title = p:match('title="(.-)"')
65 or error("no match: media title")
67 self.id = p:match("video/([^%?_]+)")
68 or error("no match: media ID")
70 self.thumbnail_url = p:match('"og:image" content="(.-)"') or ''
72 local formats = Dailymotion.iter_formats(p, U)
73 local format = U.choose_format(self, formats,
74 Dailymotion.choose_best,
75 Dailymotion.choose_default,
76 Dailymotion.to_s)
77 or error("unable to choose format")
78 self.url = {format.url or error("no match: media URL")}
79 return self
80 end
83 -- Utility functions
86 function Dailymotion.fetch_page(self, U)
87 self.page_url = Dailymotion.normalize(self.page_url)
89 local s = self.page_url:match('[%?%&]urlback=(.+)')
90 if s then
91 self.page_url = 'http://dailymotion.com' .. U.unescape(s)
92 end
94 local opts = {arbitrary_cookie = 'family_filter=off'}
95 return quvi.fetch(self.page_url, opts)
96 end
98 function Dailymotion.normalize(page_url) -- "Normalize" embedded URLs
99 if page_url:match("/swf/") then
100 page_url = page_url:gsub("/swf/", "/")
101 elseif page_url:match("/embed/") then
102 page_url = page_url:gsub("/embed/", "/")
104 return page_url
107 function Dailymotion.iter_formats(page, U)
108 local seq = page:match('"sequence",%s+"(.-)"')
109 if not seq then
110 local e = "no match: sequence"
111 if page:match("_partnerplayer") then
112 e = e .. ": looks like a partner video which we do not support"
114 error(e)
117 seq = U.unescape(seq)
119 local t = {}
120 for url in seq:gmatch('%w+URL":"(.-)"') do
121 local c,w,h,cn = url:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
122 if c then
123 url = url:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
124 table.insert(t, {width=tonumber(w), height=tonumber(h),
125 container=cn, codec=string.lower(c),
126 url=url:gsub("\\/", "/")})
127 -- print(c,w,h,cn)
131 if #t == 0 then
132 error("no match: media URL")
135 return t
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: