]> git.sesse.net Git - vlc/blob - share/lua/playlist/dailymotion.lua
fd8802852d33906bac9a328c15f3ccb308cee13a
[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 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 -- Probe function.
25 function probe()
26     return vlc.access == "http"
27         and string.match( vlc.path, "dailymotion." ) 
28         and string.match( vlc.peek( 2048 ), "<!DOCTYPE.*video_type" )
29 end
30
31 function find( haystack, needle )
32     local _,_,ret = string.find( haystack, needle )
33     return ret
34 end
35
36 -- Parse function.
37 function parse()
38     while true
39     do 
40         line = vlc.readline()
41         if not line then break end
42         if string.match( line, "param name=\"flashvars\" value=\".*video=" )
43         then
44             arturl = find( line, "param name=\"flashvars\" value=\".*preview=([^&]*)" )
45             videos = vlc.strings.decode_uri( find( line, "param name=\"flashvars\" value=\".*video=([^&]*)" ) )
46        --[[ we get a list of different streams available, at various codecs
47             and resolutions:
48             /A@@spark||/B@@spark-mini||/C@@vp6-hd||/D@@vp6||/E@@h264
49             Not everybody can decode HD, not everybody has a 80x60 screen,
50             H264/MP4 is buggy , so i choose VP6 as the highest priority
51
52             Ideally, VLC would propose the different streams available, codecs
53             and resolutions (the resolutions are part of the URL)
54
55             For now we just built a list of preferred codecs : lowest value
56             means highest priority
57          ]]
58             local pref = { ["vp6"]=0, ["spark"]=1, ["h264"]=2, ["vp6-hd"]=3, ["spark-mini"]=4 }
59             local available = {}
60             for n in string.gmatch(videos, "[^|]+") do
61                 i = string.find(n, "@@")
62                 if i then
63                     available[string.sub(n, i+2)] = string.sub(n, 0, i-1)
64                 end
65             end
66             local score = 666
67             local bestcodec
68             for codec,_ in pairs(available) do
69                 if pref[codec] == nil then
70                     vlc.msg.warn( "Unknown codec: " .. codec )
71                     pref[codec] = 42 -- try the 1st unknown codec if other fail
72                 end
73                 if pref[codec] < score then
74                     bestcodec = codec
75                     score = pref[codec]
76                 end
77             end
78             if bestcodec then
79                 path = "http://dailymotion.com" .. available[bestcodec]
80             end
81         end
82         if string.match( line, "<meta name=\"title\"" )
83         then
84             name = vlc.strings.resolve_xml_special_chars( find( line, "name=\"title\" content=\"(.-)\"" ) )
85         end
86         if string.match( line, "<meta name=\"description\"" )
87         then
88             description = vlc.strings.resolve_xml_special_chars( vlc.strings.resolve_xml_special_chars( find( line, "name=\"description\" lang=\".-\" content=\"(.-)\"" ) ) )
89         end
90         if path and name and description and arturl then break end
91     end
92     return { { path = path; name = name; description = description; url = vlc.path; arturl = arturl } }
93 end