Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created April 10, 2016 04:50
Show Gist options
  • Select an option

  • Save deoxxa/2e795259b6ffd4a91d9d67734786e418 to your computer and use it in GitHub Desktop.

Select an option

Save deoxxa/2e795259b6ffd4a91d9d67734786e418 to your computer and use it in GitHub Desktop.
--[[
Youtube playlist importer for VLC media player 1.1 and 2.0
Copyright 2012 Guillaume Le Maout
Authors: Guillaume Le Maout
Contact: http://addons.videolan.org/messages/?action=newmessage&username=exebetche
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.
--]]
--[[
MODified by Kai Gillmann, 19.01.2013, [email protected]:
VLC HAS already a youtube importer, but not for playlists. IMO this mentioned one is
better than this one, because it opens the video in the best possible video resolution.
So i decided to remove all parts of the code which is not responsible for list handling.
Now this lua script parses the list, as wanted, but for each video opened, the vlc default
Youtube script is used, so the videos will be displayed properly.
--]]
--[[
Modified again by Conrad Pankoff <[email protected]>, 2016-04-10
Switched to the JSON interface for youtube, removing the necessity for the
weird XML parsing function. This makes the code much simpler.
--]]
-- Helper function to get a parameter's value in a URL
function get_url_param(url, name)
local _, _, res = string.find(url, "[&?]"..name.."=([^&]*)")
return res
end
-- Probe function.
function probe()
if vlc.access ~= "http" and vlc.access ~= "https" then
return false
end
return string.match(vlc.path, "youtube.com") and string.match(vlc.path, "list=")
end
-- Parse function.
function parse()
if not string.match(vlc.path, "list=") then
return
end
local json = require("dkjson")
local playlistID = get_url_param(vlc.path, "list")
local playlistURL = "http://www.youtube.com/list_ajax?action_get_list=1&style=json&list="..playlistID
local p = {}
local id_ref = {}
local index = 1
while true do
local s = vlc.stream(playlistURL.."&index="..index)
local line = ""
local jsonData = ""
while line do
jsonData = jsonData..line
line = s:readline()
end
local data, _, err = json.decode(jsonData)
if not err == nil then
return p
end
for i = 1,#data.video do
local video = data.video[i]
if id_ref[video.encrypted_id] == i then
return p
else
id_ref[video.encrypted_id] = i
end
local item = {}
if video.title then
item.title = video.title
end
if video.artist then
item.artist = video.artist
end
if video.thumbnail then
item.arturl = video.thumbnail
end
if video.description then
item.description = video.description
end
if video.encrypted_id then
item.path = "http://www.youtube.com/watch?v="..video.encrypted_id
table.insert (p, item)
end
end
if #data.video == 100 then
index = index + 100
else
return p
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment