]> git.sesse.net Git - vlc/blob - share/luaplaylist/youtube.lua
* luaplaylist.c: First usable version of the luaplaylist module
[vlc] / share / luaplaylist / youtube.lua
1 -- $Id$
2
3 --[[
4 VLC Lua playlist modules should define two functions:
5  * probe(): returns true if we want to handle the playlist in this script
6  * parse(): read the incoming data and return playlist item(s)
7             The playlist is a table of playlist objects.
8             A playlist object has the following members:
9                 .url: the item's full URL
10                 .title: the item's title (OPTIONAL)
11             Invalid playlist items will be discarded by VLC.
12
13 VLC defines a global vlc object with the following members:
14  * vlc.path: the URL string (without the leading http:// or file:// element)
15  * vlc.access: the access used ("http" for http://, "file" for file://, etc.)
16  * vlc.peek( <int> ): return the first <int> characters from the playlist file.
17  * vlc.readline(): return a new line of playlist data on each call.
18                    THIS FUNCTION SHOULD NOT BE USED IN peek().
19  * vlc.decode_uri( <string> ): decode %xy characters in a string.
20  * vlc.resolve_xml_special_chars( <string> ): decode &abc; characters in a string.
21
22 Lua scripts are tried in alphabetical order in the luaplaylist/ directory.
23
24 Lua documentation is available on http://www.lua.org .
25 VLC uses Lua 5.1
26 All the Lua standard libraries are available.
27 --]]
28
29 -- Helper function to get a parameter's value in a URL
30 function get_url_param( url, name )
31     return string.gsub( vlc.path, "^.*"..name.."=([^&]*).*$", "%1" )
32 end
33
34 -- Probe function.
35 function probe()
36     return vlc.access == "http"
37         and string.match( vlc.path, "youtube.com" ) 
38         and (  string.match( vlc.path, "watch%?v=" )
39             or string.match( vlc.path, "watch_fullscreen%?video_id=" )
40             or string.match( vlc.path, "p.swf" )
41             or string.match( vlc.path, "player2.swf" ) )
42 end
43
44 -- Parse function.
45 function parse()
46     local p = {}
47     if string.match( vlc.path, "watch%?v=" )
48     then -- This is the HTML page's URL
49         p[1] = { url = string.gsub( vlc.path, "^(.*)watch%?v=([^&]*).*$", "http://%1v/%2" ) }
50         while true do
51             -- Try to find the video's title
52             line = vlc.readline()
53             if not line then break end
54             if string.match( line, "<meta name=\"title\"" ) then
55                 p[1].title = string.gsub( line, "^.*content=\"([^\"]*).*$", "%1" )
56                 break
57             end
58         end
59     else -- This is the flash player's URL
60         p[1] = { url = "http://www.youtube.com/get_video.php?video_id="..get_url_param( vlc.path, "video_id" ).."&t="..get_url_param( vlc.patch, "t" ) }
61         if string.match( vlc.path, "title=" ) then
62             p[1].title = get_url_param( vlc.path, "title" )
63         end
64     end
65     return p
66 end