From 9bf82da421455739f16b7186ecef0da4c3e9aea4 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Wed, 1 Aug 2012 10:44:35 +0300 Subject: [PATCH] Add playlist/youtube.lua --- share/Makefile.am | 3 +- share/lua/playlist/youtube.lua | 127 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 share/lua/playlist/youtube.lua diff --git a/share/Makefile.am b/share/Makefile.am index 40beb34..7bae6d5 100644 --- a/share/Makefile.am +++ b/share/Makefile.am @@ -46,7 +46,8 @@ endif # WITH_FIXME # Playlist property scripts. DIST_lua+=\ - lua/playlist/soundcloud.lua + lua/playlist/soundcloud.lua\ + lua/playlist/youtube.lua nobase_dist_pkgdata_DATA= $(DIST_lua) EXTRA_DIST= $(DIST_lua) diff --git a/share/lua/playlist/youtube.lua b/share/lua/playlist/youtube.lua new file mode 100644 index 0000000..e844a24 --- /dev/null +++ b/share/lua/playlist/youtube.lua @@ -0,0 +1,127 @@ +-- libquvi-scripts +-- Copyright (C) 2012 Toni Gundogdu +-- +-- This file is part of libquvi-scripts . +-- +-- This library is free software; you can redistribute it and/or +-- modify it under the terms of the GNU Lesser General Public +-- License as published by the Free Software Foundation; either +-- version 2.1 of the License, or (at your option) any later version. +-- +-- This library 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 +-- Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public +-- License along with this library; if not, write to the Free Software +-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +-- 02110-1301 USA +-- + +local YouTube = {} -- Utility functions unique to this script + +-- Identify the playlist script. +function ident(qargs) + local A = require 'quvi/accepts' + local r = { + accepts = A.accepts(qargs.input_url, {"youtube%.com"}, nil, {"list=%w+"}) + } + return r +end + +-- Parse playlist properties. +function parse(qargs) + + qargs.id = qargs.input_url:match('list=(%w+)') + while #qargs.id > 16 do -- Strip playlist ID prefix (e.g. "PL") + qargs.id = qargs.id:gsub("^%w", "") + end + if #qargs.id < 16 then + error('no match: playlist ID') + end + + local Y = require 'quvi/youtube' + local P = require 'lxp.lom' + + local max_results = 25 + local start_index = 1 + + qargs.media_url = {} + local t = {} + + repeat -- Get the entire playlist. + local u = YouTube.config_url(qargs, start_index, max_results) + local c = quvi.fetch(u, {type='playlist'}) + local r = P.parse(c) + + YouTube.chk_error_resp(r) + t = YouTube.parse_media_urls(r) + + for _,u in pairs(t) do Y.append_if_unique(qargs, u) end + + start_index = start_index + #t + until #t == 0 + + return qargs +end + +-- +-- Utility functions +-- + +function YouTube.config_url(qargs, start_index, max_results) + return string.format( -- Refer to http://is.gd/0msY8X + 'http://gdata.youtube.com/feeds/api/playlists/%s?v=2' + .. '&start-index=%s&max-results=%s&strict=true', + qargs.id, start_index, max_results) +end + +function YouTube.parse_media_urls(t) + local r = {} + if not t then return r end + for i=1, #t do + if t[i].tag == 'entry' then + for j=1, #t[i] do + if t[i][j].tag == 'link' then + if t[i][j].attr['rel'] == 'alternate' then + table.insert(r, t[i][j].attr['href']) + end + end + end + end + end + return r +end + +function YouTube.chk_error_resp(t) + if not t then return end + local r = {} + for i=1, #t do + if t[i].tag == 'error' then + for j=1, #t[i] do + if t[i][j].tag == 'domain' then + r.domain = t[i][j][1] + end + if t[i][j].tag == 'code' then + r.code = t[i][j][1] + end + if t[i][j].tag == 'internalReason' then + r.reason = t[i][j][1] + end + if t[i][j].tag == 'location' then -- Ignores 'type' attribute. + r.location = t[i][j][1] + end + end + end + end + if #r >0 then + local m + for k,v in pairs(r) do + m = m .. string.format("%s=%s ", k,v) + end + error(m) + end +end + +-- vim: set ts=2 sw=2 tw=72 expandtab: -- 2.11.4.GIT