2 -- Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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
22 local YouTube
= {} -- Utility functions unique to this script
24 -- Identify the playlist script.
26 local A
= require
'quvi/accepts'
28 accepts
= A
.accepts(qargs
.input_url
, {"youtube%.com"}, nil, {"list=%w+"})
33 -- Parse playlist properties.
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", "")
40 if #qargs
.id
< 16 then
41 error('no match: playlist ID')
44 local Y
= require
'quvi/youtube'
45 local P
= require
'lxp.lom'
47 local max_results
= 25
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'})
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
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
)
80 function YouTube
.parse_media_urls(t
)
82 if not t
then return r
end
84 if t
[i
].tag == 'entry' then
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'])
97 function YouTube
.chk_error_resp(t
)
98 if not t
then return end
101 if t
[i
].tag == 'error' then
103 if t
[i
][j
].tag == 'domain' then
104 r
.domain
= t
[i
][j
][1]
106 if t
[i
][j
].tag == 'code' then
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]
120 for k
,v
in pairs(r
) do
121 m
= m
.. string.format("%s=%s ", k
,v
)
127 -- vim: set ts=2 sw=2 tw=72 expandtab: