2 -- Copyright (C) 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 Vzaar
= {} -- Utility functions unique to this script
23 -- Identify the media script.
26 can_parse_url
= Vzaar
.can_parse_url(qargs
),
27 domains
= table.concat({'vzaar.com'}, ',')
31 -- Parse the media properties.
33 local U
= require
'socket.url'
34 local r
= Vzaar
.normalize(U
, qargs
.input_url
)
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']
65 function Vzaar
.can_parse_url(qargs
)
66 local U
= require
'socket.url'
67 local u
= Vzaar
.normalize(U
, qargs
.input_url
)
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+$')
79 function Vzaar
.normalize(U
, url
)
85 local u
= U
.parse(url
)
87 if not u
or not u
.host
or not u
.path
then
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', '')
99 u
.path
= u
.path
:gsub('^/(%d+)$', '/videos/%1')
101 -- Rebuild and return the media URL.
105 function Vzaar
.iter_streams(l
)
106 local S
= require
'quvi/stream'
107 return {S
.stream_new(l
['video_content_url'])}
111 local function test_normalize()
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'
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))
131 print((e==0) and 'tests OK' or error('failed tests: '..e))
136 -- vim: set ts=2 sw=2 tw=72 expandtab: