dailymotion.lua 3.58 KB
Newer Older
Antoine Cellerier's avatar
Antoine Cellerier committed
1 2 3
--[[
    Translate Daily Motion video webpages URLs to the corresponding
    FLV URL.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

 $Id$

 Copyright © 2007 the VideoLAN team

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Antoine Cellerier's avatar
Antoine Cellerier committed
22 23
--]]

24 25 26 27
-- Probe function.
function probe()
    return vlc.access == "http"
        and string.match( vlc.path, "dailymotion.com" ) 
28
        and string.match( vlc.peek( 256 ), "<!DOCTYPE.*<title>Video " )
29 30 31 32 33 34 35 36
end

-- Parse function.
function parse()
    while true
    do 
        line = vlc.readline()
        if not line then break end
37
        if string.match( line, "param name=\"flashvars\" value=\".*video=" )
38
        then
39 40
            arturl = vlc.strings.decode_uri( string.gsub( line, "^.*param name=\"flashvars\" value=\".*preview=([^&]*).*$", "%1" ) )
            videos = vlc.strings.decode_uri( string.gsub( line, "^.*param name=\"flashvars\" value=\".*video=([^&]*).*$", "%1" ) )
41 42 43 44
       --[[ we get a list of different streams available, at various codecs
            and resolutions:
            /A@@spark||/B@@spark-mini||/C@@vp6-hd||/D@@vp6||/E@@h264
            Not everybody can decode HD, not everybody has a 80x60 screen,
45
            H264/MP4 is buggy , so i choose VP6 as the highest priority
46 47 48

            Ideally, VLC would propose the different streams available, codecs
            and resolutions (the resolutions are part of the URL)
49 50 51

            For now we just built a list of preferred codecs : lowest value
            means highest priority
52
         ]]
53 54
            local pref = { ["vp6"]=0, ["spark"]=1, ["h264"]=2, ["vp6-hd"]=3, ["spark-mini"]=4 }
            local available = {}
55 56 57
            for n in string.gmatch(videos, "[^|]+") do
                i = string.find(n, "@@")
                if i then
58 59 60 61 62 63
                    available[string.sub(n, i+2)] = string.sub(n, 0, i-1)
                end
            end
            local score = 666
            local bestcodec
            for codec,_ in pairs(available) do
64
                if pref[codec] == nil then
65
                    vlc.msg.warn( "Unknown codec: " .. codec )
66 67
                    pref[codec] = 42 -- try the 1st unknown codec if other fail
                end
68 69 70
                if pref[codec] < score then
                    bestcodec = codec
                    score = pref[codec]
71 72
                end
            end
73 74 75
            if bestcodec then
                path = "http://dailymotion.com" .. available[bestcodec]
            end
76
        end
77 78
        if string.match( line, "<meta name=\"description\"" )
        then
79 80
            name = vlc.strings.resolve_xml_special_chars( string.gsub( line, "^.*name=\"description\" content=\"%w+ (.*) %w+ %w+ %w+ %w+ Videos\..*$", "%1" ) )
            description = vlc.strings.resolve_xml_special_chars( string.gsub( line, "^.*name=\"description\" content=\"%w+ .* %w+ %w+ %w+ %w+ Videos\. ([^\"]*)\".*$", "%1" ) )
81 82
        end
        if path and name and description and arturl then break end
83
    end
84
    return { { path = path; name = name; description = description; url = vlc.path; arturl = arturl } }
85
end