youtube.lua 5.65 KB
Newer Older
1 2 3
--[[
 $Id$

Pierre Ynard's avatar
Pierre Ynard committed
4
 Copyright © 2007-2011 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

 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.
--]]
20 21 22

-- Helper function to get a parameter's value in a URL
function get_url_param( url, name )
23 24
    local _, _, res = string.find( url, "[&?]"..name.."=([^&]*)" )
    return res
25 26 27 28
end

function get_arturl( path, video_id )
    if string.match( vlc.path, "iurl=" ) then
Rafaël Carré's avatar
Rafaël Carré committed
29
        return vlc.strings( get_url_param( vlc.path, "iurl" ) )
30 31 32 33
    end
    if not arturl then
        return "http://img.youtube.com/vi/"..video_id.."/default.jpg"
    end
34 35 36 37
end

-- Probe function.
function probe()
38
    if vlc.access ~= "http" and vlc.access ~= "https" then
39 40 41 42 43 44 45 46 47 48 49 50
        return false
    end
    youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
    if not youtube_site then
        -- FIXME we should be using a builtin list of known youtube websites
        -- like "fr.youtube.com", "uk.youtube.com" etc..
        youtube_site = string.find( vlc.path, ".youtube.com" )
        if youtube_site == nil then
            return false
        end
    end
    return (  string.match( vlc.path, "watch%?v=" ) -- the html page
51 52 53
            or string.match( vlc.path, "watch_fullscreen%?video_id=" ) -- the fullscreen page
            or string.match( vlc.path, "p.swf" ) -- the (old?) player url
            or string.match( vlc.path, "jp.swf" ) -- the (new?) player url (as of 24/08/2007)
54
            or string.match( vlc.path, "player2.swf" ) ) -- another player url
55 56 57 58 59 60
end

-- Parse function.
function parse()
    if string.match( vlc.path, "watch%?v=" )
    then -- This is the HTML page's URL
Pierre Ynard's avatar
Pierre Ynard committed
61 62
        -- fmt is the format of the video
        -- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs)
63
        fmt = get_url_param( vlc.path, "fmt" )
64 65 66 67 68
        while true do
            -- Try to find the video's title
            line = vlc.readline()
            if not line then break end
            if string.match( line, "<meta name=\"title\"" ) then
69
                _,_,name = string.find( line, "content=\"(.-)\"" )
70
                name = vlc.strings.resolve_xml_special_chars( name )
71
                name = vlc.strings.resolve_xml_special_chars( name )
72
            end
73
            if string.match( line, "<meta name=\"description\"" ) then
74
               -- Don't ask me why they double encode ...
Pierre Ynard's avatar
Pierre Ynard committed
75 76 77
                _,_,description = string.find( line, "content=\"(.-)\"" )
                description = vlc.strings.resolve_xml_special_chars( description )
                description = vlc.strings.resolve_xml_special_chars( description )
78
            end
79 80
            if string.match( line, " rel=\"author\"" ) then
                _,_,artist = string.find( line, "href=\"/user/([^\"]*)\"" )
81
            end
Pierre Ynard's avatar
Pierre Ynard committed
82 83 84 85 86
            -- JSON parameters, also formerly known as "swfConfig",
            -- "SWF_ARGS", "swfArgs" ...
            if string.match( line, "PLAYER_CONFIG" ) then
                url_map = string.match( line, "\"url_encoded_fmt_stream_map\": \"(.-)\"" )
                if url_map then
87
                    -- FIXME: do this properly
Pierre Ynard's avatar
Pierre Ynard committed
88 89
                    url_map = string.gsub( url_map, "\\u0026", "&" )
                    for url,itag in string.gmatch( url_map, "url=([^&,]+).-&itag=(%d+)" ) do
90 91 92
                        -- Apparently formats are listed in quality order,
                        -- so we can afford to simply take the first one
                        if not fmt or tonumber( itag ) == tonumber( fmt ) then
93
                            url = vlc.strings.decode_uri( url )
94 95 96 97 98
                            path = url
                            break
                        end
                    end
                end
Pierre Ynard's avatar
Pierre Ynard committed
99 100 101
            -- There is also another version of the parameters, encoded
            -- differently, as an HTML attribute of an <object> or <embed>
            -- tag; but we don't need it now
Rafaël Carré's avatar
Rafaël Carré committed
102
            end
103
        end
104

Pierre Ynard's avatar
Pierre Ynard committed
105
        video_id = get_url_param( vlc.path, "v" )
106
        arturl = get_arturl( vlc.path, video_id )
107 108

        if not path then
Pierre Ynard's avatar
Pierre Ynard committed
109 110
            vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
            return { }
111
        end
112
        return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
113 114
    else -- This is the flash player's URL
        if string.match( vlc.path, "title=" ) then
115
            name = vlc.strings.decode_uri(get_url_param( vlc.path, "title" ))
116
        end
117
        video_id = get_url_param( vlc.path, "video_id" )
118
        arturl = get_arturl( vlc.path, video_id )
119 120 121 122 123 124
        fmt = get_url_param( vlc.path, "fmt" )
        if fmt then
            format = "&fmt=" .. fmt
        else
            format = ""
        end
125 126 127
        if not string.match( vlc.path, "t=" ) then
            -- This sucks, we're missing "t" which is now mandatory. Let's
            -- try using another url
128
            return { { path = "http://www.youtube.com/v/"..video_id; name = name; arturl = arturl } }
129
        end
130
        return { { path = "http://www.youtube.com/get_video.php?video_id="..video_id.."&t="..get_url_param( vlc.path, "t" )..format; name = name; arturl = arturl } }
131 132
    end
end