Add playlist/youtube.lua
[libquvi-scripts.git] / share / lua / playlist / youtube.lua
blobe844a2400a1ddb0146d65dfc84856d7953e4d2f4
1 -- libquvi-scripts
2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 -- 02110-1301 USA
22 local YouTube = {} -- Utility functions unique to this script
24 -- Identify the playlist script.
25 function ident(qargs)
26 local A = require 'quvi/accepts'
27 local r = {
28 accepts = A.accepts(qargs.input_url, {"youtube%.com"}, nil, {"list=%w+"})
30 return r
31 end
33 -- Parse playlist properties.
34 function parse(qargs)
36 qargs.id = qargs.input_url:match('list=(%w+)')
37 while #qargs.id > 16 do -- Strip playlist ID prefix (e.g. "PL")
38 qargs.id = qargs.id:gsub("^%w", "")
39 end
40 if #qargs.id < 16 then
41 error('no match: playlist ID')
42 end
44 local Y = require 'quvi/youtube'
45 local P = require 'lxp.lom'
47 local max_results = 25
48 local start_index = 1
50 qargs.media_url = {}
51 local t = {}
53 repeat -- Get the entire playlist.
54 local u = YouTube.config_url(qargs, start_index, max_results)
55 local c = quvi.fetch(u, {type='playlist'})
56 local r = P.parse(c)
58 YouTube.chk_error_resp(r)
59 t = YouTube.parse_media_urls(r)
61 for _,u in pairs(t) do Y.append_if_unique(qargs, u) end
63 start_index = start_index + #t
64 until #t == 0
66 return qargs
67 end
70 -- Utility functions
73 function YouTube.config_url(qargs, start_index, max_results)
74 return string.format( -- Refer to http://is.gd/0msY8X
75 'http://gdata.youtube.com/feeds/api/playlists/%s?v=2'
76 .. '&start-index=%s&max-results=%s&strict=true',
77 qargs.id, start_index, max_results)
78 end
80 function YouTube.parse_media_urls(t)
81 local r = {}
82 if not t then return r end
83 for i=1, #t do
84 if t[i].tag == 'entry' then
85 for j=1, #t[i] do
86 if t[i][j].tag == 'link' then
87 if t[i][j].attr['rel'] == 'alternate' then
88 table.insert(r, t[i][j].attr['href'])
89 end
90 end
91 end
92 end
93 end
94 return r
95 end
97 function YouTube.chk_error_resp(t)
98 if not t then return end
99 local r = {}
100 for i=1, #t do
101 if t[i].tag == 'error' then
102 for j=1, #t[i] do
103 if t[i][j].tag == 'domain' then
104 r.domain = t[i][j][1]
106 if t[i][j].tag == 'code' then
107 r.code = t[i][j][1]
109 if t[i][j].tag == 'internalReason' then
110 r.reason = t[i][j][1]
112 if t[i][j].tag == 'location' then -- Ignores 'type' attribute.
113 r.location = t[i][j][1]
118 if #r >0 then
119 local m
120 for k,v in pairs(r) do
121 m = m .. string.format("%s=%s ", k,v)
123 error(m)
127 -- vim: set ts=2 sw=2 tw=72 expandtab: