]> git.sesse.net Git - vlc/blob - share/lua/playlist/dailymotion.lua
847ca31d4046272a1afa59300ac88bcd9716c8b4
[vlc] / share / lua / playlist / dailymotion.lua
1 --[[
2     Translate Daily Motion video webpages URLs to the corresponding
3     FLV URL.
4
5  $Id$
6
7  Copyright © 2007-2011 the VideoLAN team
8
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]]
23
24 function get_prefres()
25     local prefres = -1
26     if vlc.var and vlc.var.inherit then
27         prefres = vlc.var.inherit(nil, "preferred-resolution")
28         if prefres == nil then
29             prefres = -1
30         end
31     end
32     return prefres
33 end
34
35 -- Probe function.
36 function probe()
37     return vlc.access == "http"
38         and string.match( vlc.path, "www.dailymotion.com/video" )
39 end
40
41 function find( haystack, needle )
42     local _,_,ret = string.find( haystack, needle )
43     return ret
44 end
45
46 -- Parse function.
47 function parse()
48     prefres = get_prefres()
49     while true
50     do
51         line = vlc.readline()
52         if not line then
53             break
54         end
55         if string.match( line, "sequence=")
56         then
57             line = vlc.strings.decode_uri(line):gsub("\\/", "/")
58
59             arturl = find( line, "\"videoPreviewURL\":\"([^\"]*)\"")
60             name = find( line, "\"videoTitle\":\"([^\"]*)\"")
61             if name then
62                 name = string.gsub( name, "+", " " )
63             end
64             description = find( line, "\"videoDescription\":\"([^\"]*)\"")
65             if description then
66                 description = string.gsub( description, "+", " " )
67             end
68
69             for _,param in ipairs({ "hd1080URL", "hd720URL", "hqURL", "sdURL", "video_url" }) do
70                 path = string.match( line, "\""..param.."\":\"([^\"]*)\"" )
71                 if path then
72                     path = vlc.strings.decode_uri(path)
73                     if prefres < 0 then
74                         break
75                     end
76                     height = string.match( path, "/cdn/%w+%-%d+x(%d+)/video/" )
77                     if not height then
78                         height = string.match( param, "(%d+)" )
79                     end
80                     if not height or tonumber(height) <= prefres then
81                         break
82                     end
83                 end
84             end
85
86             if not path then
87                 break
88             end
89
90             return { { path = path; name = name; description = description; url = vlc.path; arturl = arturl } }
91         end
92     end
93
94     vlc.msg.err("Couldn't extract the video URL from dailymotion")
95     return { }
96 end