Set release details for 0.2.13
[quvi.git] / share / lua / website / dailymotion.lua
blob92ae8094f16adbf65f932c3c2a35a61dc1bebd32
2 -- quvi
3 -- Copyright (C) 2010 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of quvi <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 local domains = {
24 "dailymotion.",
25 "dai.ly"
28 -- Check whether the domain is handled
29 function is_handled (page_url)
30 if page_url == nil then
31 return false
32 end
33 for k,domain in pairs(domains) do
34 if page_url:find(domain) ~= nil then
35 return true
36 end
37 end
38 return false
39 end
41 -- Identify the script.
42 function ident (self)
43 package.path = self.script_dir .. '/?.lua'
44 local C = require 'quvi/const'
45 local r = {}
46 r.domain = "dailymotion."
47 r.formats = "default|best|hq|hd"
48 r.categories = C.proto_http
49 r.handles = is_handled (self.page_url)
50 return r
51 end
53 -- Parse video URL.
54 function parse (self)
55 self.host_id = "dailymotion"
56 self.page_url = normalize (self.page_url)
58 local _,_,s = self.page_url:find ('/family_filter%?urlback=(.+)')
59 if (s ~= nil) then
60 self.page_url = 'http://dailymotion.com' .. quvi.unescape (s)
61 -- We set family_filter arbitrary cookie below.
62 end
64 local opts = { arbitrary_cookie = 'family_filter=off' }
65 local page = quvi.fetch(self.page_url, opts)
67 if (page:find('SWFObject("http:")') ~= nil) then
68 error ("Looks like a partner video. Refusing to continue.")
69 end
71 local _,_,s = page:find('title="(.-)"')
72 self.title = s or error ("no match: video title")
74 local _,_,s = page:find("video/(.-)_")
75 self.id = s or error ("no match: video id")
77 -- Some videos have only one link available.
78 local _,_,s = page:find ('"video", "(.-)"')
79 if (s ~= nil) then
80 self.url = {s}
81 else
82 parse_video_url (self)
83 end
85 if (self.url == nil) then
86 error ('no match: video url')
87 end
89 return self
90 end
92 function normalize (url)
93 if (url:find ("/swf/")) then
94 url = url:gsub ("/video/", "/")
95 url = url:gsub ("/swf/", "/video/")
96 end
97 return url
98 end
100 function parse_video_url (self)
102 local best = nil
103 local req_fmt = self.requested_format
105 if (req_fmt == "default") then
106 req_fmt = "sd" -- Dailymotion defaults to this.
109 for id,path in page:gfind("%%22(%w%w)URL%%22%%3A%%22(.-)%%22") do
110 path = path:gsub("%%5C","")
111 path = quvi.unescape(path)
112 best = path
113 if (req_fmt == id) then
114 self.url = {path}
115 break
119 if (best ~= nil and self.requested_format == "best") then
120 self.url = {best}
123 return self.url
126 -- vim: set ts=4 sw=4 tw=72 expandtab: