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 L
= require
'quvi/lxph'
41 local P
= require
'lxp.lom'
43 local max_results
= 25
49 repeat -- Get the entire playlist.
50 local u
= YouTube
.config_url(qargs
, start_index
, max_results
)
51 local c
= quvi
.http
.fetch(u
).data
54 YouTube
.chk_error_resp(x
)
56 YouTube
.parse_thumb_url(qargs
, L
, x
)
57 YouTube
.parse_title(qargs
, L
, x
)
59 local n
= YouTube
.parse_media_urls(qargs
, L
, x
)
61 start_index
= start_index
+ n
71 function YouTube
.can_parse_url(qargs
)
72 local U
= require
'socket.url'
73 local t
= U
.parse(qargs
.input_url
)
74 if t
and t
.scheme
and t
.scheme
:lower():match('^https?$')
75 and t
.host
and t
.host
:lower():match('youtube%.com$')
76 and t
.path
and t
.path
:lower():match('^/playlist$')
77 and t
.query
and t
.query
:lower():match('^list=[%w_-]+')
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
.entry_avail(x
)
94 -- app:control may contain, for example:
95 -- yt:state name='restricted' reasonCode='private'
98 if x
[i
].tag == 'app:control' then return false end
103 function YouTube
.parse_entry(qargs
, L
, x
, i
, r
)
105 if x
[i
][j
].tag == 'link' then
106 if x
[i
][j
].attr
['rel'] == 'alternate' then
108 title
= L
.find_first_tag(x
[i
], 'title')[1],
109 duration_ms
= YouTube
.parse_duration(L
, x
[i
]),
110 url
= x
[i
][j
].attr
['href']
112 table.insert(qargs
.media
, t
)
118 function YouTube
.parse_media_urls(qargs
, L
, x
)
119 if not x
then return 0 end
122 if x
[i
].tag == 'entry' then
123 if YouTube
.entry_avail(x
[i
]) then
124 YouTube
.parse_entry(qargs
, L
, x
, i
, r
)
132 function YouTube
.chk_error_resp(t
)
133 if not t
then return end
136 if t
[i
].tag == 'error' then
138 if t
[i
][j
].tag == 'domain' then
139 r
.domain
= t
[i
][j
][1]
141 if t
[i
][j
].tag == 'code' then
144 if t
[i
][j
].tag == 'internalReason' then
145 r
.reason
= t
[i
][j
][1]
147 if t
[i
][j
].tag == 'location' then -- Ignores 'type' attribute.
148 r
.location
= t
[i
][j
][1]
155 for k
,v
in pairs(r
) do
156 m
= m
.. string.format("%s=%s ", k
,v
)
162 function YouTube
.parse_title(qargs
, L
, x
)
163 if not qargs
.title
then
164 qargs
.title
= L
.find_first_tag(x
, 'title')[1]
168 function YouTube
.parse_thumb_url(qargs
, L
, x
)
169 if qargs
.thumb_url
then return end
170 local g
= L
.find_first_tag(x
, 'media:group')
172 if g
[i
].tag == 'media:thumbnail' then
173 if g
[i
].attr
['yt:name'] == 'hqdefault' then
174 qargs
.thumb_url
= g
[i
].attr
['url']
181 function YouTube
.parse_duration(L
, x
)
182 local g
= L
.find_first_tag(x
, 'media:group')
183 local d
= L
.find_first_tag(g
, 'yt:duration')
184 return tonumber(d
.attr
['seconds']) * 1000
187 -- vim: set ts=2 sw=2 tw=72 expandtab: