Split from git://repo.or.cz/quvi.git
[libquvi-scripts.git] / share / lua / website / megavideo.lua
blob53111b471aa53061564e1c03ac712db2c754f808
2 -- libquvi-scripts
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
6 --
7 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
8 --
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
22 -- 02110-1301 USA
25 local Megavideo = {} -- Utility functions unique to this script.
27 -- Identify the script.
28 function ident(self)
29 package.path = self.script_dir .. '/?.lua'
30 local C = require 'quvi/const'
31 local r = {}
32 r.domain = "megavideo%.com"
33 r.formats = "default"
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]+'})
37 return r
38 end
40 -- Query available formats.
41 function query_formats(self)
42 self.formats = 'default'
43 return self
44 end
46 -- Parse media URL.
47 function parse(self)
48 self.host_id = "megavideo"
50 local _,_,s = self.page_url:find('?v=([^&]+)')
51 self.id = s or error ("no match: media id")
53 local videolink_url =
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")')
76 local err = s
78 if err then
79 local _,_,s = videolink_url_page:find('errortext="([^"]+)"')
80 local err_txt = s
82 error (err_txt)
83 end
85 local loc1 = {}
86 local loc6 = {}
87 local loc7 = {}
88 local loc3
89 local loc12
90 local loc9
91 local loc2 = {}
93 for loc3 = 1,string.len(str)
95 local s = "000"
96 .. Megavideo.decimal2binary(
97 Megavideo.hexchar2decimal(string.sub(str,loc3,loc3)))
98 table.insert(loc1, string.sub(s, string.len(s)-3))
99 end
101 loc1 = Megavideo.splitall(table.concat(loc1))
103 for loc3 = 0, 384-1
105 k1 = (k1 * 11 + 77213) % 81371;
106 k2 = (k2 * 17 + 92717) % 192811;
107 loc6[loc3+1] = (k1 + k2) % 128;
110 loc3 = 256
111 while loc3 >= 0
113 local loc5 = loc6[loc3+1];
114 local loc4 = loc3 % 128;
115 local loc8 = loc1[loc5+1];
116 loc1[loc5+1] = loc1[loc4+1];
117 loc1[loc4+1] = loc8;
119 loc3=loc3-1
122 for loc3=0, 128-1
124 loc1[loc3+1] =
125 Megavideo.binaryxor(loc1[loc3 + 1], loc6[loc3 + 1 + 256]) % 2
128 loc12 = table.concat(loc1)
130 loc3 = 0
131 while loc3 < string.len(loc12)
133 loc9 = string.sub(loc12, loc3+1, loc3+4)
134 table.insert(loc7, loc9)
135 loc3 = loc3+4
138 for loc3 = 0,table.getn(loc7)-1
140 table.insert(loc2, Megavideo.decimal2hex(
141 Megavideo.binary2decimal(tonumber(loc7[loc3+1]))) )
144 self.url = {
145 string.format("http://www%s.megavideo.com/files/%s/video.flv",
146 ser, table.concat(loc2))
149 return self
153 -- Utility functions
156 function Megavideo.hexchar2decimal(s)
157 local n = tonumber(s) or string.byte(string.upper(s)) - 55
159 if n > 15 then
160 error("Hexadecimal value is too high.")
163 return n
166 function Megavideo.decimal2hex(s)
167 return string.format("%x", s)
170 function Megavideo.decimal2binary(s)
171 local bin = 0
172 local i = 0
174 while s > 0
176 bin=bin + (s%2) * math.pow(10, i)
177 s=(s-s%2)/2
178 i=i+1
181 return bin
184 function Megavideo.binary2decimal(s)
185 local dec = 0
186 local i = 0
188 while s > 0
190 dec = dec + (s%10) * math.pow(2, i)
191 s=(s-(s % 10)) / 10
192 i=i+1
195 return dec
198 function Megavideo.splitall(s)
199 local c = ""
200 local i = 1
201 local t = {}
203 while i <= string.len(s)
205 c = string.sub(s, i, i)
206 table.insert(t, c)
207 i=i+1
210 return t
213 function Megavideo.binaryxor(s1, s2)
214 local r = 0
215 local t = 1
216 local i = 0
217 local l = math.abs(s1 - s2)
219 while t > 0
221 if ((s1%2) + (s2%2)) == 1
222 then
223 r=r+math.pow(2, i)
226 s1=(s1-s1%2)/2
227 s2=(s2-s2%2)/2
229 i=i+1
230 t=s1+s2
233 return r
236 -- vim: set ts=4 sw=4 tw=72 expandtab: