media/tagtele.lua: Relicense under AGPLv3+
[libquvi-scripts.git] / share / media / metacafe.lua
blob089bc13831c93fea53cb541e965f5ec273550738
1 -- libquvi-scripts
2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local Metacafe = {} -- Utility functions unique to this script.
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = Metacafe.can_parse_url(qargs),
28 domains = table.concat({'metacafe.com'}, ',')
30 end
32 -- Parse the media properties.
33 function parse(qargs)
35 if Metacafe.is_affiliate(qargs) then
36 return qargs
37 end
39 local p = quvi.http.fetch(qargs.input_url).data
41 local v = p:match('name="flashvars" value="(.-)"')
42 or error('no match: flashvars')
44 qargs.thumb_url = p:match('"og:image"%s+content="(.-)"') or ''
46 local U = require 'socket.url'
47 local T = require 'quvi/util'
49 local r = T.decode(U.unescape(v))
51 qargs.streams = Metacafe.iter_streams(U, r)
53 qargs.duration_ms = tonumber(r['duration'] or 0) * 1000
55 qargs.title = r['title'] or ''
57 qargs.id = r['itemID'] or ''
59 return qargs
60 end
63 -- Utility functions
66 function Metacafe.can_parse_url(qargs)
67 local U = require 'socket.url'
68 local t = U.parse(qargs.input_url)
69 if t and t.scheme and t.scheme:lower():match('^http$')
70 and t.host and t.host:lower():match('metacafe%.com$')
71 and t.path and (t.path:lower():match('^/watch/%d+/')
72 or t.path:lower():match('^/watch/yt-[^/]+/'))
73 then
74 return true
75 else
76 return false
77 end
78 end
80 function Metacafe.is_affiliate(qargs)
81 local id = qargs.input_url:match('/watch/yt%-([^/]+)/') -- YouTube.
82 if id then
83 qargs.goto_url = table.concat({'http://youtu.be/',id})
84 end
85 return qargs.goto_url ~= nil
86 end
88 function Metacafe.iter_streams(U, r)
89 local S = require 'quvi/stream'
90 local J = require 'json'
92 local j = J.decode(r['mediaData'])
93 local t = {}
95 for k,_ in pairs(j) do
96 local u = U.parse(j[k]['mediaURL'] or error('no match: media stream URL'))
98 local q = {} -- Construct the URL query from the given args.
99 for _,v in pairs(j[k]['access'][1]) do
100 table.insert(q,v)
102 u.query = table.concat(q, '=')
104 local s = S.stream_new(U.build(u)) -- Build stream URL from these elems.
105 Metacafe.to_id(s,k)
107 table.insert(t,s)
110 local r = {} -- Reverse the stream order, SD should be first ('default').
111 for i = #t,1,-1 do
112 table.insert(r, t[i])
115 if #r >1 then
116 Metacafe.ch_best(S, r)
119 return r
122 function Metacafe.ch_best(S, t)
123 t[#t].flags.best = true -- Make the last stream the best one.
126 function Metacafe.to_id(s,k)
127 -- Lack of a better scheme: reuse the internal IDs with minor tweaks.
128 s.id = k:gsub('(%l)(%u)','%1_%2'):lower()
131 -- vim: set ts=2 sw=2 tw=72 expandtab: