media/metacafe.lua: Rewrite ident function
[libquvi-scripts.git] / share / media / metacafe.lua
blobe226239a186981e9947a8794c43deffed4449210
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 -- Query available formats.
33 function query_formats(self)
34 if not Metacafe.redirectp(self) then
35 self.formats = 'default'
36 end
37 return self
38 end
40 -- Parse media URL.
41 function parse(self)
42 self.host_id = "metacafe"
44 if Metacafe.redirectp(self) then
45 return self
46 end
48 local U = require 'quvi/util'
49 local p = Metacafe.fetch_page(self, U)
51 local v = p:match('name="flashvars" value="(.-)"')
52 or error('no match: flashvars')
54 v = U.slash_unescape(U.unescape(v))
56 self.thumbnail_url = p:match('rel="image_src" href="(.-)"') or ''
58 self.title = v:match('title=(.-)&') or error('no match: media title')
60 self.id = v:match('itemID=(%d+)') or error('no match: media ID')
62 local u = v:match('"mediaURL":"(.-)"')
63 or error('no match: media stream URL')
65 local k = v:match('"key":"__gda__","value":"(.-)"')
66 or error('no match: key')
68 self.url = {string.format("%s?__gda__=%s", u, k)}
70 return self
71 end
74 -- Utility functions
77 function Metacafe.can_parse_url(qargs)
78 local U = require 'socket.url'
79 local t = U.parse(qargs.input_url)
80 if t and t.scheme and t.scheme:lower():match('^http$')
81 and t.host and t.host:lower():match('metacafe%.com$')
82 and t.path and (t.path:lower():match('^/watch/%d+/')
83 or t.path:lower():match('^/watch/yt-[^/]+/'))
84 then
85 return true
86 else
87 return false
88 end
89 end
91 function Metacafe.redirectp(self)
92 local s = self.page_url:match('/watch/yt%-([^/]+)/')
93 if s then -- Hand over to youtube.lua
94 self.redirect_url = 'http://youtube.com/watch?v=' .. s
95 return true
96 end
97 return false
98 end
100 function Metacafe.fetch_page(self, U)
101 self.page_url = Metacafe.normalize(self.page_url)
103 return quvi.fetch(self.page_url)
106 function Metacafe.normalize(page_url) -- "Normalize" embedded URLs
107 return page_url
110 -- vim: set ts=4 sw=4 tw=72 expandtab: