share/Makefile.am: Reorganize blocks, comments
[libquvi-scripts.git] / share / lua / playlist / youtube.lua
blobabe81c4178f295e0eeeefb9c914dfbed88977bb4
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 = {}
52 local C = require 'quvi/const'
53 local o = { [C.qfo_type] = C.qft_playlist }
55 local r = {}
57 -- TODO: Return playlist thumbnail URL
58 -- TODO: Return playlist title
60 repeat -- Get the entire playlist.
61 local u = YouTube.config_url(qargs, start_index, max_results)
62 local c = quvi.fetch(u, o)
63 local x = P.parse(c)
65 YouTube.chk_error_resp(x)
66 r = YouTube.parse_media_urls(x)
68 for _,u in pairs(r) do
69 local t = {
70 url = u
72 table.insert(qargs.media, t)
73 end
75 start_index = start_index + #r
76 until #r == 0
78 return qargs
79 end
82 -- Utility functions
85 function YouTube.config_url(qargs, start_index, max_results)
86 return string.format( -- Refer to http://is.gd/0msY8X
87 'http://gdata.youtube.com/feeds/api/playlists/%s?v=2'
88 .. '&start-index=%s&max-results=%s&strict=true',
89 qargs.id, start_index, max_results)
90 end
92 function YouTube.parse_media_urls(t)
93 -- TODO: Return media duration_ms
94 -- TODO: Return media title
95 local r = {}
96 if not t then return r end
97 for i=1, #t do
98 if t[i].tag == 'entry' then
99 for j=1, #t[i] do
100 if t[i][j].tag == 'link' then
101 if t[i][j].attr['rel'] == 'alternate' then
102 table.insert(r, t[i][j].attr['href'])
108 return r
111 function YouTube.chk_error_resp(t)
112 if not t then return end
113 local r = {}
114 for i=1, #t do
115 if t[i].tag == 'error' then
116 for j=1, #t[i] do
117 if t[i][j].tag == 'domain' then
118 r.domain = t[i][j][1]
120 if t[i][j].tag == 'code' then
121 r.code = t[i][j][1]
123 if t[i][j].tag == 'internalReason' then
124 r.reason = t[i][j][1]
126 if t[i][j].tag == 'location' then -- Ignores 'type' attribute.
127 r.location = t[i][j][1]
132 if #r >0 then
133 local m
134 for k,v in pairs(r) do
135 m = m .. string.format("%s=%s ", k,v)
137 error(m)
141 -- vim: set ts=2 sw=2 tw=72 expandtab: