Update NEWS for v0.9.20130520
[libquvi-scripts.git] / share / lua / website / metacafe.lua
1
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.
12 --
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.
17 --
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
22 --
23
24 local Metacafe = {} -- Utility functions unique to this script.
25
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
39
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
47
48 -- Parse media URL.
49 function parse(self)
50     self.host_id = "metacafe"
51
52     if Metacafe.redirectp(self) then
53         return self
54     end
55
56     local U = require 'quvi/util'
57     local p = Metacafe.fetch_page(self, U)
58
59     local v = p:match('name="flashvars" value="(.-)"')
60                 or error('no match: flashvars')
61
62     v = U.slash_unescape(U.unescape(v))
63
64     self.thumbnail_url = p:match('rel="image_src" href="(.-)"') or ''
65
66     self.title = v:match('title=(.-)&') or error('no match: media title')
67
68     self.id = v:match('itemID=(%d+)') or error('no match: media ID')
69
70     local u = v:match('"mediaURL":"(.-)"')
71                 or error('no match: media stream URL')
72
73     local k = v:match('"key":"__gda__","value":"(.-)"')
74                 or error('no match: key')
75
76     self.url = {string.format("%s?__gda__=%s", u, k)}
77
78     return self
79 end
80
81 --
82 -- Utility functions
83 --
84
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
93
94 function Metacafe.fetch_page(self, U)
95     self.page_url = Metacafe.normalize(self.page_url)
96
97     return quvi.fetch(self.page_url)
98 end
99
100 function Metacafe.normalize(page_url) -- "Normalize" embedded URLs
101     return page_url
102 end
103
104 -- vim: set ts=4 sw=4 tw=72 expandtab: