FIX: media/spiegel.lua: title pattern (BACKPORTpt4)
[libquvi-scripts.git] / share / playlist / youtube.lua
blob627292c796bf0134e717c40bf280219ce7d27bd9
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.path and t.path:lower():match('^/playlist$')
77 and t.query and t.query:lower():match('^list=[%w_-]+')
78 then
79 return true
80 else
81 return false
82 end
83 end
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)
90 end
92 function YouTube.entry_avail(x)
93 --[[
94 -- app:control may contain, for example:
95 -- yt:state name='restricted' reasonCode='private'
96 ]]--
97 for i=1, #x do
98 if x[i].tag == 'app:control' then return false end
99 end
100 return true
103 function YouTube.parse_entry(qargs, L, x, i, r)
104 for j=1, #x[i] do
105 if x[i][j].tag == 'link' then
106 if x[i][j].attr['rel'] == 'alternate' then
107 local t = {
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
120 local n = 0
121 for i=1, #x do
122 if x[i].tag == 'entry' then
123 if YouTube.entry_avail(x[i]) then
124 YouTube.parse_entry(qargs, L, x, i, r)
126 n = n+1
129 return n
132 function YouTube.chk_error_resp(t)
133 if not t then return end
134 local r = {}
135 for i=1, #t do
136 if t[i].tag == 'error' then
137 for j=1, #t[i] do
138 if t[i][j].tag == 'domain' then
139 r.domain = t[i][j][1]
141 if t[i][j].tag == 'code' then
142 r.code = t[i][j][1]
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]
153 if #r >0 then
154 local m
155 for k,v in pairs(r) do
156 m = m .. string.format("%s=%s ", k,v)
158 error(m)
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')
171 for i=1, #g do
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']
175 break
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: