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
52 local C
= require
'quvi/const'
53 local o
= { [C
.qfo_type
] = C
.qft_playlist
}
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
)
65 YouTube
.chk_error_resp(x
)
66 r
= YouTube
.parse_media_urls(x
)
68 for _
,u
in pairs(r
) do
72 table.insert(qargs
.media
, t
)
75 start_index
= start_index
+ #r
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
)
92 function YouTube
.parse_media_urls(t
)
93 -- TODO: Return media duration_ms
94 -- TODO: Return media title
96 if not t
then return r
end
98 if t
[i
].tag == 'entry' then
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'])
111 function YouTube
.chk_error_resp(t
)
112 if not t
then return end
115 if t
[i
].tag == 'error' then
117 if t
[i
][j
].tag == 'domain' then
118 r
.domain
= t
[i
][j
][1]
120 if t
[i
][j
].tag == 'code' then
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]
134 for k
,v
in pairs(r
) do
135 m
= m
.. string.format("%s=%s ", k
,v
)
141 -- vim: set ts=2 sw=2 tw=72 expandtab: