]> git.sesse.net Git - vlc/blob - share/luaplaylist/youtube.lua
lua: adds a copyright notice and a GPLv2+ header to each lua script
[vlc] / share / luaplaylist / youtube.lua
1 --[[
2  French humor site: http://lelombrik.net
3
4  $Id$
5
6  Copyright © 2007 the VideoLAN team
7
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 --]]
22
23 -- Helper function to get a parameter's value in a URL
24 function get_url_param( url, name )
25     return string.gsub( url, "^.*[&?]"..name.."=([^&]*).*$", "%1" )
26 end
27
28 function get_arturl( path, video_id )
29     if string.match( vlc.path, "iurl=" ) then
30         return vlc.decode_uri( get_url_param( vlc.path, "iurl" ) )
31     end
32     if not arturl then
33         return "http://img.youtube.com/vi/"..video_id.."/default.jpg"
34     end
35 end
36
37 -- Probe function.
38 function probe()
39     if vlc.access ~= "http" then
40         return false
41     end
42     youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
43     if not youtube_site then
44         -- FIXME we should be using a builtin list of known youtube websites
45         -- like "fr.youtube.com", "uk.youtube.com" etc..
46         youtube_site = string.find( vlc.path, ".youtube.com" )
47         if youtube_site == nil then
48             return false
49         end
50     end
51     return (  string.match( vlc.path, "watch%?v=" ) -- the html page
52             or string.match( vlc.path, "watch_fullscreen%?video_id=" ) -- the fullscreen page
53             or string.match( vlc.path, "p.swf" ) -- the (old?) player url
54             or string.match( vlc.path, "jp.swf" ) -- the (new?) player url (as of 24/08/2007)
55             or string.match( vlc.path, "player2.swf" ) ) -- another player url
56 end
57
58 -- Parse function.
59 function parse()
60     if string.match( vlc.path, "watch%?v=" )
61     then -- This is the HTML page's URL
62         while true do
63             -- Try to find the video's title
64             line = vlc.readline()
65             if not line then break end
66             if string.match( line, "<meta name=\"title\"" ) then
67                 name = string.gsub( line, "^.*content=\"([^\"]*).*$", "%1" )
68             end
69             if string.match( line, "<meta name=\"description\"" ) then
70                 description = string.gsub( line, "^.*content=\"([^\"]*).*$", "%1" )
71             end
72             if string.match( line, "subscribe_to_user=" ) then
73                 artist = string.gsub( line, ".*subscribe_to_user=([^&]*).*", "%1" )
74             end
75             -- var swfArgs = {hl:'en',BASE_YT_URL:'http://youtube.com/',video_id:'XPJ7d8dq0t8',l:'292',t:'OEgsToPDskLFdOYrrlDm3FQPoQBYaCP1',sk:'0gnr-AE6QZJEZmCMd3lq_AC'};
76             if string.match( line, "swfArgs" ) and string.match( line, "video_id" ) then
77                 if string.match( line, "BASE_YT_URL" ) then
78                     base_yt_url = string.gsub( line, ".*BASE_YT_URL:'([^']*)'.*", "%1" )
79                 end
80                 t = string.gsub( line, ".*t:'([^']*)'.*", "%1" )
81                 -- vlc.msg_err( t )
82                 -- video_id = string.gsub( line, ".*&video_id:'([^']*)'.*", "%1" )
83             end
84             if name and description and artist --[[and video_id]] then break end
85         end
86         if not video_id then
87             video_id = get_url_param( vlc.path, "v" )
88         end
89         if not base_yt_url then
90             base_yt_url = "http://youtube.com/"
91         end
92         art_url = get_arturl( vlc.path, video_id )
93         if t then
94             return { { path = base_yt_url .. "get_video?video_id="..video_id.."&t="..t; name = name; description = description; artist = artist; arturl = arturl } }
95         else
96             -- This shouldn't happen ... but keep it as a backup.
97             return { { path = "http://www.youtube.com/v/"..video_id; name = name; description = description; artist = artist; arturl = arturl } }
98         end
99     else -- This is the flash player's URL
100         if string.match( vlc.path, "title=" ) then
101             name = get_url_param( vlc.path, "title" )
102         end
103         video_id = get_url_param( vlc.path, "video_id" )
104         art_url = get_arturl( vlc.path, video_id )
105         if not string.match( vlc.path, "t=" ) then
106             -- This sucks, we're missing "t" which is now mandatory. Let's
107             -- try using another url
108             return { { path = "http://www.youtube.com/v/"..video_id; name = name; arturl = arturl } }
109         end
110         return { { path = "http://www.youtube.com/get_video.php?video_id="..video_id.."&t="..get_url_param( vlc.path, "t" ); name = name; arturl = arturl } }
111     end
112 end