]> git.sesse.net Git - vlc/blob - share/luaplaylist/youtube.lua
Fixes youtube parsing
[vlc] / share / luaplaylist / youtube.lua
1 -- $Id$
2
3 -- Helper function to get a parameter's value in a URL
4 function get_url_param( url, name )
5     return string.gsub( vlc.path, "^.*"..name.."=([^&]*).*$", "%1" )
6 end
7
8 -- Probe function.
9 function probe()
10     return vlc.access == "http"
11         and string.match( vlc.path, "youtube.com" ) 
12         and (  string.match( vlc.path, "watch%?v=" )
13             or string.match( vlc.path, "watch_fullscreen%?video_id=" )
14             or string.match( vlc.path, "p.swf" )
15             or string.match( vlc.path, "player2.swf" ) )
16 end
17
18 -- Parse function.
19 function parse()
20     if string.match( vlc.path, "watch%?v=" )
21     then -- This is the HTML page's URL
22         while true do
23             -- Try to find the video's title
24             line = vlc.readline()
25             if not line then break end
26             if string.match( line, "<meta name=\"title\"" ) then
27                 name = string.gsub( line, "^.*content=\"([^\"]*).*$", "%1" )
28             end
29             if string.match( line, "<meta name=\"description\"" ) then
30                 description = string.gsub( line, "^.*content=\"([^\"]*).*$", "%1" )
31             end
32             if string.match( line, "subscribe_to_user=" ) then
33                 artist = string.gsub( line, ".*subscribe_to_user=([^&]*).*", "%1" )
34             end
35             if string.match( line, "player2.swf" ) then
36                 video_id = string.gsub( line, ".*BASE_YT_URL=http://youtube.com/&video_id=([^\"]*).*", "%1" )
37             end
38             if name and description and artist and video_id then break end
39         end
40         return { { path = "http://www.youtube.com/get_video.php?video_id="..video_id; name = name; description = description; artist = artist } }
41     else -- This is the flash player's URL
42         if string.match( vlc.path, "title=" ) then
43             name = get_url_param( vlc.path, "title" )
44         end
45         return { { path = "http://www.youtube.com/get_video.php?video_id="..get_url_param( vlc.path, "video_id" ).."&t="..get_url_param( vlc.patch, "t" ); name = name } }
46     end
47 end