3 -- Copyright (C) 2011 Paul Kocialkowski <contact@paulk.fr>
4 -- Based on http://userscripts.org/scripts/show/87011
5 -- Which is (C) sebaro, released under the GNU GPL
7 -- This file is part of quvi <http://quvi.sourceforge.net/>.
9 -- This library is free software; you can redistribute it and/or
10 -- modify it under the terms of the GNU Lesser General Public
11 -- License as published by the Free Software Foundation; either
12 -- version 2.1 of the License, or (at your option) any later version.
14 -- This library is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 -- Lesser General Public License for more details.
19 -- You should have received a copy of the GNU Lesser General Public
20 -- License along with this library; if not, write to the Free Software
21 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 local Megavideo
= {} -- Utility functions unique to this script.
27 -- Identify the script.
29 package
.path
= self
.script_dir
.. '/?.lua'
30 local C
= require
'quvi/const'
32 r
.domain
= "megavideo.com"
34 r
.categories
= C
.proto_http
35 local U
= require
'quvi/util'
36 r
.handles
= U
.handles(self
.page_url
, {r
.domain
}, nil, {'v=[%w]+'})
40 -- Query available formats.
41 function query_formats(self
)
42 self
.formats
= 'default'
48 self
.host_id
= "megavideo"
50 local _
,_
,s
= self
.page_url
:find('?v=([^&]+)')
51 self
.id
= s
or error ("no match: media id")
54 "http://www.megavideo.com/xml/videolink.php?v=" .. self
.id
56 local videolink_url_page
=
57 quvi
.fetch(videolink_url
, {fetch_type
= 'config'})
59 local _
,_
,s
= videolink_url_page
:find(' title="([^"]+)"')
60 self
.title
= s
or error ("no match: media title")
61 self
.title
= string.gsub(self
.title
, "+", " ")
63 local _
,_
,s
= videolink_url_page
:find(' un="([^"]+)"')
64 local str
= s
or error ("no match: str")
66 local _
,_
,s
= videolink_url_page
:find(' k1="([^"]+)"')
67 local k1
= s
or error ("no match: k1")
69 local _
,_
,s
= videolink_url_page
:find(' k2="([^"]+)"')
70 local k2
= s
or error ("no match: k2")
72 local _
,_
,s
= videolink_url_page
:find(' s="([^"]+)"')
73 local ser
= s
or error ("no match: s")
75 local _
,_
,s
= videolink_url_page
:find('(error="1")')
79 local _
,_
,s
= videolink_url_page
:find('errortext="([^"]+)"')
93 for loc3
= 1,string.len(str
)
96 .. Megavideo
.decimal2binary(
97 Megavideo
.hexchar2decimal(string.sub(str
,loc3
,loc3
)))
98 table.insert(loc1
, string.sub(s
, string.len(s
)-3))
101 loc1
= Megavideo
.splitall(table.concat(loc1
))
105 k1
= (k1
* 11 + 77213) % 81371;
106 k2
= (k2
* 17 + 92717) % 192811;
107 loc6
[loc3
+1] = (k1
+ k2
) % 128;
113 local loc5
= loc6
[loc3
+1];
114 local loc4
= loc3
% 128;
115 local loc8
= loc1
[loc5
+1];
116 loc1
[loc5
+1] = loc1
[loc4
+1];
125 Megavideo
.binaryxor(loc1
[loc3
+ 1], loc6
[loc3
+ 1 + 256]) % 2
128 loc12
= table.concat(loc1
)
131 while loc3
< string.len(loc12
)
133 loc9
= string.sub(loc12
, loc3
+1, loc3
+4)
134 table.insert(loc7
, loc9
)
138 for loc3
= 0,table.getn(loc7
)-1
140 table.insert(loc2
, Megavideo
.decimal2hex(
141 Megavideo
.binary2decimal(tonumber(loc7
[loc3
+1]))) )
145 string.format("http://www%s.megavideo.com/files/%s/video.flv",
146 ser
, table.concat(loc2
))
156 function Megavideo
.hexchar2decimal(s
)
157 local n
= tonumber(s
) or string.byte(string.upper(s
)) - 55
160 error("Hexadecimal value is too high.")
166 function Megavideo
.decimal2hex(s
)
167 return string.format("%x", s
)
170 function Megavideo
.decimal2binary(s
)
176 bin
=bin
+ (s
%2) * math
.pow(10, i
)
184 function Megavideo
.binary2decimal(s
)
190 dec
= dec
+ (s
%10) * math
.pow(2, i
)
198 function Megavideo
.splitall(s
)
203 while i
<= string.len(s
)
205 c
= string.sub(s
, i
, i
)
213 function Megavideo
.binaryxor(s1
, s2
)
217 local l
= math
.abs(s1
- s2
)
221 if ((s1
%2) + (s2
%2)) == 1
236 -- vim: set ts=4 sw=4 tw=72 expandtab: