2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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.
27 can_parse_url
= Metacafe
.can_parse_url(qargs
),
28 domains
= table.concat({'metacafe.com'}, ',')
32 -- Parse the media properties.
35 if Metacafe
.is_affiliate(qargs
) then
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 ''
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-[^/]+/'))
80 function Metacafe
.is_affiliate(qargs
)
81 local id
= qargs
.input_url
:match('/watch/yt%-([^/]+)/') -- YouTube.
83 qargs
.goto_url
= table.concat({'http://youtu.be/',id
})
85 return qargs
.goto_url
~= nil
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'])
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
102 u
.query
= table.concat(q
, '=')
104 local s
= S
.stream_new(U
.build(u
)) -- Build stream URL from these elems.
110 local r
= {} -- Reverse the stream order, SD should be first ('default').
112 table.insert(r
, t
[i
])
116 Metacafe
.ch_best(S
, 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: