2 -- Copyright (C) 2012-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program 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
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local YouTube
= {} -- Utility functions unique to this script
23 -- Identify the playlist script.
26 domains
= table.concat({'youtube.com'}, ','),
27 can_parse_url
= YouTube
.can_parse_url(qargs
)
31 -- Parse playlist properties.
34 qargs
.id
= qargs
.input_url
:match('list=([%w_-]+)')
36 error('no match: playlist ID')
39 local Y
= require
'quvi/youtube'
40 local P
= require
'lxp.lom'
42 local max_results
= 25
47 local C
= require
'quvi/const'
48 local o
= { [C
.qfo_type
] = C
.qft_playlist
}
52 -- TODO: Return playlist thumbnail URL
53 -- TODO: Return playlist title
55 repeat -- Get the entire playlist.
56 local u
= YouTube
.config_url(qargs
, start_index
, max_results
)
57 local c
= quvi
.fetch(u
, o
).data
60 YouTube
.chk_error_resp(x
)
61 r
= YouTube
.parse_media_urls(x
)
63 for _
,u
in pairs(r
) do
67 table.insert(qargs
.media
, t
)
70 start_index
= start_index
+ #r
80 function YouTube
.can_parse_url(qargs
)
81 local U
= require
'socket.url'
82 local t
= U
.parse(qargs
.input_url
)
83 if t
and t
.scheme
and t
.scheme
:lower():match('^https?$')
84 and t
.host
and t
.host
:lower():match('youtube%.com$')
85 and t
.query
and t
.query
:lower():match('list=[%w_-]+')
93 function YouTube
.config_url(qargs
, start_index
, max_results
)
94 return string.format( -- Refer to http://is.gd/0msY8X
95 'http://gdata.youtube.com/feeds/api/playlists/%s?v=2'
96 .. '&start-index=%s&max-results=%s&strict=true',
97 qargs
.id
, start_index
, max_results
)
100 function YouTube
.parse_media_urls(t
)
101 -- TODO: Return media duration_ms
102 -- TODO: Return media title
104 if not t
then return r
end
106 if t
[i
].tag == 'entry' then
108 if t
[i
][j
].tag == 'link' then
109 if t
[i
][j
].attr
['rel'] == 'alternate' then
110 table.insert(r
, t
[i
][j
].attr
['href'])
119 function YouTube
.chk_error_resp(t
)
120 if not t
then return end
123 if t
[i
].tag == 'error' then
125 if t
[i
][j
].tag == 'domain' then
126 r
.domain
= t
[i
][j
][1]
128 if t
[i
][j
].tag == 'code' then
131 if t
[i
][j
].tag == 'internalReason' then
132 r
.reason
= t
[i
][j
][1]
134 if t
[i
][j
].tag == 'location' then -- Ignores 'type' attribute.
135 r
.location
= t
[i
][j
][1]
142 for k
,v
in pairs(r
) do
143 m
= m
.. string.format("%s=%s ", k
,v
)
149 -- vim: set ts=2 sw=2 tw=72 expandtab: