Move website/collegehumor.lua to media/
[libquvi-scripts.git] / share / media / vzaar.lua
blob96688382fa5f2df741a1a75c87c2b1451c11dc6d
1 -- libquvi-scripts
2 -- Copyright (C) 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 Vzaar = {} -- Utility functions unique to this script
23 -- Identify the media script.
24 function ident(qargs)
25 return {
26 can_parse_url = Vzaar.can_parse_url(qargs),
27 domains = table.concat({'vzaar.com'}, ',')
29 end
31 -- Parse the media properties.
32 function parse(qargs)
33 local U = require 'socket.url'
34 local r = Vzaar.normalize(U, qargs.input_url)
35 local u = U.parse(r)
37 -- Make ID mandatory: It is required to fetch
38 qargs.id = u.path:match('^/videos/(%d+)$') or error('no match: media ID')
40 local t = {u.scheme, '://view.vzaar.com/', qargs.id, '/player'}
41 local p = quvi.http.fetch(table.concat(t)).data
43 local d = p:match('data%-setup="(.-)"') or
44 error('no match: data-setup')
46 local E = require 'quvi/entity'
47 local J = require 'json'
49 local j = J.decode(E.convert_html(d))
50 local l = j['playlist'][1] or error('no data: playlist')
52 qargs.streams = Vzaar.iter_streams(l)
54 qargs.thumb_url = l['thumb_url']
56 qargs.title = l['title']
58 return qargs
59 end
62 -- Utility functions.
65 function Vzaar.can_parse_url(qargs)
66 local U = require 'socket.url'
67 local u = Vzaar.normalize(U, qargs.input_url)
68 local t = U.parse(u)
69 if t and t.scheme and t.scheme:lower():match('^https?$')
70 and t.host and t.host:lower():match('^vzaar%.com$')
71 and t.path and t.path:lower():match('^/videos/%d+$')
72 then
73 return true
74 else
75 return false
76 end
77 end
79 function Vzaar.normalize(U, url)
80 -- Sanity checks.
81 if not url then
82 return url
83 end
85 local u = U.parse(url)
87 if not u or not u.host or not u.path then
88 return url
89 end
91 -- Shortened URLs (http://vzaar.tv/:id:).
92 u.host = u.host:gsub('vzaar%.tv', 'vzaar.com')
94 -- Embedded media URLs (http://view.vzaar.com/:id:/player).
95 u.host = u.host:gsub('^view%.', '')
96 u.path = u.path:gsub('/player', '')
98 -- Fix path.
99 u.path = u.path:gsub('^/(%d+)$', '/videos/%1')
101 -- Rebuild and return the media URL.
102 return U.build(u)
105 function Vzaar.iter_streams(l)
106 local S = require 'quvi/stream'
107 return {S.stream_new(l['video_content_url'])}
110 --[[
111 local function test_normalize()
112 local test_cases = {
113 {url='http://vzaar.tv/1020181', -- Shortened URL.
114 expect='http://vzaar.com/videos/1020181'},
115 {url='http://view.vzaar.com/1020181/player', -- Embedded URL.
116 expect='http://vzaar.com/videos/1020181'},
117 {url='http://vzaar.com/videos/1020181', -- Media URL.
118 expect='http://vzaar.com/videos/1020181'},
120 local U = require 'socket.url'
121 local i,e = 0,0
122 for _,v in pairs(test_cases) do
123 local r = Vzaar.normalize(U, v.url)
124 if r ~= v.expect then
125 print(string.format('input: %s (#%s)\nexpected: %s\ngot: %s',
126 v.url, i, v.expect, r))
127 e = e+1
129 i = i+1
131 print((e==0) and 'tests OK' or error('failed tests: '..e))
133 test_normalize()
134 ]]--
136 -- vim: set ts=2 sw=2 tw=72 expandtab: