quvi/const: Update notes
[libquvi-scripts.git] / share / playlist / youtube.lua
blob1c3619c2b9cb675b04b77098cb6b6d3fa292290e
1 -- libquvi-scripts
2 -- Copyright (C) 2012-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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.
24 function ident(qargs)
25 return {
26 domains = table.concat({'youtube.com'}, ','),
27 can_parse_url = YouTube.can_parse_url(qargs)
29 end
31 -- Parse playlist properties.
32 function parse(qargs)
34 qargs.id = qargs.input_url:match('list=([%w_-]+)')
35 if #qargs.id <16 then
36 error('no match: playlist ID')
37 end
39 local Y = require 'quvi/youtube'
40 local P = require 'lxp.lom'
42 local max_results = 25
43 local start_index = 1
45 qargs.media = {}
47 local C = require 'quvi/const'
48 local o = { [C.qfo_type] = C.qft_playlist }
50 local r = {}
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
58 local x = P.parse(c)
60 YouTube.chk_error_resp(x)
61 r = YouTube.parse_media_urls(x)
63 for _,u in pairs(r) do
64 local t = {
65 url = u
67 table.insert(qargs.media, t)
68 end
70 start_index = start_index + #r
71 until #r == 0
73 return qargs
74 end
77 -- Utility functions
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_-]+')
86 then
87 return true
88 else
89 return false
90 end
91 end
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)
98 end
100 function YouTube.parse_media_urls(t)
101 -- TODO: Return media duration_ms
102 -- TODO: Return media title
103 local r = {}
104 if not t then return r end
105 for i=1, #t do
106 if t[i].tag == 'entry' then
107 for j=1, #t[i] do
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'])
116 return r
119 function YouTube.chk_error_resp(t)
120 if not t then return end
121 local r = {}
122 for i=1, #t do
123 if t[i].tag == 'error' then
124 for j=1, #t[i] do
125 if t[i][j].tag == 'domain' then
126 r.domain = t[i][j][1]
128 if t[i][j].tag == 'code' then
129 r.code = t[i][j][1]
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]
140 if #r >0 then
141 local m
142 for k,v in pairs(r) do
143 m = m .. string.format("%s=%s ", k,v)
145 error(m)
149 -- vim: set ts=2 sw=2 tw=72 expandtab: