Merge branch 'tg/next__media_port_website/clipfish.lua' into next
[libquvi-scripts.git] / share / playlist / youtube.lua
blobadf649e824dfb161224b580412d63468280cc54d
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 L = require 'quvi/lxph'
41 local P = require 'lxp.lom'
43 local max_results = 25
44 local start_index = 1
46 qargs.media = {}
47 local r = {}
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
52 local x = P.parse(c)
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
62 until n == 0
64 return qargs
65 end
68 -- Utility functions
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.query and t.query:lower():match('list=[%w_-]+')
77 then
78 return true
79 else
80 return false
81 end
82 end
84 function YouTube.config_url(qargs, start_index, max_results)
85 return string.format( -- Refer to http://is.gd/0msY8X
86 'http://gdata.youtube.com/feeds/api/playlists/%s?v=2'
87 .. '&start-index=%s&max-results=%s&strict=true',
88 qargs.id, start_index, max_results)
89 end
91 function YouTube.entry_avail(x)
92 --[[
93 -- app:control may contain, for example:
94 -- yt:state name='restricted' reasonCode='private'
95 ]]--
96 for i=1, #x do
97 if x[i].tag == 'app:control' then return false end
98 end
99 return true
102 function YouTube.parse_entry(qargs, L, x, i, r)
103 for j=1, #x[i] do
104 if x[i][j].tag == 'link' then
105 if x[i][j].attr['rel'] == 'alternate' then
106 local t = {
107 title = L.find_first_tag(x[i], 'title')[1],
108 duration_ms = YouTube.parse_duration(L, x[i]),
109 url = x[i][j].attr['href']
111 table.insert(qargs.media, t)
117 function YouTube.parse_media_urls(qargs, L, x)
118 if not x then return 0 end
119 local n = 0
120 for i=1, #x do
121 if x[i].tag == 'entry' then
122 if YouTube.entry_avail(x[i]) then
123 YouTube.parse_entry(qargs, L, x, i, r)
125 n = n+1
128 return n
131 function YouTube.chk_error_resp(t)
132 if not t then return end
133 local r = {}
134 for i=1, #t do
135 if t[i].tag == 'error' then
136 for j=1, #t[i] do
137 if t[i][j].tag == 'domain' then
138 r.domain = t[i][j][1]
140 if t[i][j].tag == 'code' then
141 r.code = t[i][j][1]
143 if t[i][j].tag == 'internalReason' then
144 r.reason = t[i][j][1]
146 if t[i][j].tag == 'location' then -- Ignores 'type' attribute.
147 r.location = t[i][j][1]
152 if #r >0 then
153 local m
154 for k,v in pairs(r) do
155 m = m .. string.format("%s=%s ", k,v)
157 error(m)
161 function YouTube.parse_title(qargs, L, x)
162 if not qargs.title then
163 qargs.title = L.find_first_tag(x, 'title')[1]
167 function YouTube.parse_thumb_url(qargs, L, x)
168 if qargs.thumb_url then return end
169 local g = L.find_first_tag(x, 'media:group')
170 for i=1, #g do
171 if g[i].tag == 'media:thumbnail' then
172 if g[i].attr['yt:name'] == 'hqdefault' then
173 qargs.thumb_url = g[i].attr['url']
174 break
180 function YouTube.parse_duration(L, x)
181 local g = L.find_first_tag(x, 'media:group')
182 local d = L.find_first_tag(g, 'yt:duration')
183 return tonumber(d.attr['seconds']) * 1000
186 -- vim: set ts=2 sw=2 tw=72 expandtab: