Port website/dorkly.lua from the 0.4 series
[libquvi-scripts.git] / share / media / dorkly.lua
blob4f4176dc4fb33497cd6e6e4e54a5d0f885847b6e
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 Dorkly = {} -- Utility functions unique to this script
23 -- Identify the media script.
24 function ident(qargs)
25 return {
26 can_parse_url = Dorkly.can_parse_url(qargs),
27 domains = table.concat({'dorkly.com'}, ',')
29 end
31 -- Parse the media properties.
32 function parse(qargs)
34 if Dorkly.is_affiliate(qargs) then
35 return qargs
36 end
38 -- Make mandatory: the ID is required to fetch the stream data
39 qargs.id = qargs.input_url:match('/video/(%d+)/')
40 or qargs.input_url:match('/embed/(%d+)/')
41 or error('no match: media ID')
43 local t = {'http://www.dorkly.com/moogaloop/video/', qargs.id}
44 local d = quvi.http.fetch(table.concat(t)).data
46 local L = require 'quvi/lxph'
47 local P = require 'lxp.lom'
49 local x = P.parse(d)
50 local v = L.find_first_tag(x, 'video')
52 qargs.duration_ms = tonumber(L.find_first_tag(v, 'duration')[1])
54 qargs.thumb_url = L.find_first_tag(v, 'thumbnail')[1]
56 qargs.title = L.find_first_tag(v, 'caption')[1]
58 qargs.streams = Dorkly.iter_streams(L, v)
60 return qargs
61 end
64 -- Utility functions
67 function Dorkly.can_parse_url(qargs)
68 local U = require 'socket.url'
69 local t = U.parse(qargs.input_url)
70 if t and t.scheme and t.scheme:lower():match('^http$')
71 and t.host and t.host:lower():match('dorkly%.com$')
72 and t.path and (t.path:lower():match('^/video/%d+/')
73 or t.path:lower():match('^/embed/%d+/'))
74 then
75 return true
76 else
77 return false
78 end
79 end
81 function Dorkly.is_affiliate(qargs)
82 if not qargs.input_url:match('/embed/') then
83 return false
84 end
86 local p = quvi.http.fetch(qargs.input_url).data
88 local u = p:match('iframe.-src="(.-)"')
89 or error('no match: iframe: src')
91 if not u:match('^http:') then
92 u = table.concat({'http:',u}) -- Fix URL if the scheme is missing.
93 end
95 qargs.goto_url = u
96 return true
97 end
99 function Dorkly.iter_streams(L, v)
100 local u = L.find_first_tag(v, 'file')[1]
101 local S = require 'quvi/stream'
102 return {S.stream_new(u)}
105 -- vim: set ts=2 sw=2 tw=72 expandtab: