Merge branch 'tg/next__media_port_website/ardmediathek.lua' into next
[libquvi-scripts.git] / share / lua / website / metacafe.lua
blob0f6efbcc4ca3db00b7976bcce77e336c21abe62e
2 -- libquvi-scripts
3 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
5 --
6 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
7 --
8 -- This library is free software; you can redistribute it and/or
9 -- modify it under the terms of the GNU Lesser General Public
10 -- License as published by the Free Software Foundation; either
11 -- version 2.1 of the License, or (at your option) any later version.
13 -- This library is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 -- Lesser General Public License for more details.
18 -- You should have received a copy of the GNU Lesser General Public
19 -- License along with this library; if not, write to the Free Software
20 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 -- 02110-1301 USA
24 local Metacafe = {} -- Utility functions unique to this script.
26 -- Identify the script.
27 function ident(self)
28 package.path = self.script_dir .. '/?.lua'
29 local C = require 'quvi/const'
30 local r = {}
31 r.domain = "metacafe%.com"
32 r.formats = "default"
33 r.categories = C.proto_http
34 local U = require 'quvi/util'
35 r.handles = U.handles(self.page_url, {r.domain},
36 {"/watch/%d+/", "/watch/yt-[^/]+/"})
37 return r
38 end
40 -- Query available formats.
41 function query_formats(self)
42 if not Metacafe.redirectp(self) then
43 self.formats = 'default'
44 end
45 return self
46 end
48 -- Parse media URL.
49 function parse(self)
50 self.host_id = "metacafe"
52 if Metacafe.redirectp(self) then
53 return self
54 end
56 local U = require 'quvi/util'
57 local p = Metacafe.fetch_page(self, U)
59 local v = p:match('name="flashvars" value="(.-)"')
60 or error('no match: flashvars')
62 v = U.slash_unescape(U.unescape(v))
64 self.thumbnail_url = p:match('rel="image_src" href="(.-)"') or ''
66 self.title = v:match('title=(.-)&') or error('no match: media title')
68 self.id = v:match('itemID=(%d+)') or error('no match: media ID')
70 local u = v:match('"mediaURL":"(.-)"')
71 or error('no match: media stream URL')
73 local k = v:match('"key":"__gda__","value":"(.-)"')
74 or error('no match: key')
76 self.url = {string.format("%s?__gda__=%s", u, k)}
78 return self
79 end
82 -- Utility functions
85 function Metacafe.redirectp(self)
86 local s = self.page_url:match('/watch/yt%-([^/]+)/')
87 if s then -- Hand over to youtube.lua
88 self.redirect_url = 'http://youtube.com/watch?v=' .. s
89 return true
90 end
91 return false
92 end
94 function Metacafe.fetch_page(self, U)
95 self.page_url = Metacafe.normalize(self.page_url)
97 return quvi.fetch(self.page_url)
98 end
100 function Metacafe.normalize(page_url) -- "Normalize" embedded URLs
101 return page_url
104 -- vim: set ts=4 sw=4 tw=72 expandtab: