FIX: lego.lua: All patterns
[libquvi-scripts.git] / share / lua / website / dailymotion.lua
blob5a6079dbcc93f59f157f3f58cc88138ccc838584
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('"og:title" content="(.-)"')
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":"(.-)"')
111 or error('no match: sequence')
112 if not seq then
113 local e = "no match: sequence"
114 if page:match("_partnerplayer") then
115 e = e .. ": looks like a partner video which we do not support"
117 error(e)
120 seq = U.unescape(seq)
122 local t = {}
123 for url in seq:gmatch('%w+URL":"(.-)"') do
124 local c,w,h,cn = url:match('(%w+)%-(%d+)x(%d+).-%.(%w+)')
125 if c then
126 url = url:gsub('cell=secure%-vod&', '') -- http://is.gd/BzYPZJ
127 table.insert(t, {width=tonumber(w), height=tonumber(h),
128 container=cn, codec=string.lower(c),
129 url=url:gsub("\\/", "/")})
130 -- print(c,w,h,cn)
134 if #t == 0 then
135 error("no match: media URL")
138 return t
141 function Dailymotion.choose_default(formats) -- Lowest quality available
142 local r = {width=0xffff, height=0xffff, url=nil}
143 local U = require 'quvi/util'
144 for _,v in pairs(formats) do
145 if U.is_lower_quality(v,r) then
146 r = v
149 -- for k,v in pairs(r) do print(k,v) end
150 return r
153 function Dailymotion.choose_best(formats) -- Highest quality available
154 local r = {width=0, height=0, url=nil}
155 local U = require 'quvi/util'
156 for _,v in pairs(formats) do
157 if U.is_higher_quality(v,r) then
158 r = v
161 -- for k,v in pairs(r) do print(k,v) end
162 return r
165 function Dailymotion.to_s(t)
166 return string.format("%s_%sp", t.container, t.height)
169 -- vim: set ts=4 sw=4 tw=72 expandtab: