crypt of decay - исключительно вредности ради [entries|archive|friends|userinfo]
ketmar

[ userinfo | ljr userinfo ]
[ archive | journal archive ]

исключительно вредности ради [Aug. 26th, 2008|09:53 pm]
Previous Entry Add to Memories Tell A Friend Next Entry
попросили помочь. downloader с www.redtube.com. вряд ли вам оно надо, там одна порнуха. но тем не менее. %-)


код на Lua. требует наличия моего syren и строковой библиотеки, но тривиально переписыватся на любимом языке. основная «изюминка» — добывание кода файла из его id.

#!/usr/bin/lua

require "strutil";


function GenPath (id)
  id = tonumber(id) or 1;
  local s = tostring(id); s = string.rep("0", 7-#s)..s;
  local pathnr = tostring(math.floor(id/1000)); pathnr = string.rep("0", 7-#pathnr)..pathnr;
  local xc = {
    [0]="R","1","5","3","4","2","O","7","K","9","H","B","C","D","X","F","G","A",
    "I","J","8","L","M","Z","6","P","Q","0","S","T","U","V","W","E","Y","N"};
  local code = "";
  local qsum = 0;
  for f = 1, 7 do qsum = qsum+(tonumber(s:sub(f, f))*f); end;
  local s1 = tostring(qsum);
  qsum = 0;
  for f = 1, #s1 do qsum = qsum+tonumber(s1:sub(f, f)); end;
  local qstr = string.format("%02i", qsum);
  code = code..xc[s:byte(4, 4)-48+qsum+3];
  code = code..qstr:sub(2, 2);
  code = code..xc[s:byte(1, 1)-48+qsum+2];
  code = code..xc[s:byte(3, 3)-48+qsum+1];
  code = code..xc[s:byte(6, 6)-48+qsum+6];
  code = code..xc[s:byte(2, 2)-48+qsum+5];
  code = code..qstr:sub(1, 1);
  code = code..xc[s:byte(5, 5)-48+qsum+7];
  code = code..xc[s:byte(7, 7)-48+qsum+4];
  local res = pathnr.."/"..code..".flv";
  return res;
end;


local function DLFile (url, lineProcFn, ...)
  local fl = io.popen("syren -Q "..url:ShQuote().." -o -");
  if not fl then return false; end;
  print("downloading "..url);
  if lineProcFn then
    for s in fl:lines() do
      if not lineProcFn(s, ...) then break; end;
    end;
    fl:close();
    io.write("\r\27[K"); io.flush();
    return true;
  else
    local res = fl:read("*a");
    fl:close();
    io.write("\r\27[K"); io.flush();
    return res;
  end;
end;


local function ExtractVideoVars (id)
  id = tonumber(id);
  if not id then return false; end;
  local res = {};

  local function Extractor (s)
    --if not s:match('so%.addVariable("') return true; end;
    s = " "..s;
    local n, v = s:match('[%s;]so%.addVariable%s*%(%s*"([^"]+)",%s*"([^"]+)"%s*%)');
    if not n or not v then return true; end;
    if n == "styleURL" then res.style = v;
    elseif n == "id" then res.id = id;
    end;
    return not res.style or not res.id;
  end;

  if not DLFile("http://www.redtube.com/"..id.."/", Extractor) then return false; end;
  if not res.style or not res.id then return false; end;
  return res;
--[[
  so.addVariable("styleURL", "/_playerx/flash/styles/va6.css");
  so.addVariable("id", "12504");
]]
end;


local function ExtractVideoServer (url)
  local res = false;

  local function Extractor (s)
    --if not s:match('so%.addVariable("') return true; end;
    s = " "..s;
    --print(s);
    local v = s:match('[%s;]videoPath%s*:%s*([^;]+);');
    if not v then return true; end;
    v = v:match("^%s*(.-)%s*$");
    if v == "" then return true; end;
    if v:match('^".*"$') then
      v = v:sub(2, -2);
      v = v:match("^%s*(.-)%s*$");
      if v == "" then return true; end;
    end;
    if v:sub(-1, -1) ~= "/" then v = v.."/"; end;
    res = v;
    return false;
  end;

  if not DLFile("http://www.redtube.com/"..url, Extractor) then return false; end;
  return res;
--[[
  videoPath: http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/;
]]
end;


local function DownloadVideo (id)
  id = tonumber(id);
  if not id then return false; end;
  local name = string.format("%08i.flv", id);

  local vars = ExtractVideoVars(id);
  if not vars then io.stderr:write("vars not found!\n"); return false; end;
  --print(vars.style.."|");
  --print(vars.id.."|");
  local srv = ExtractVideoServer(vars.style);
  if not srv then  io.stderr:write("videoserver not found!\n"); return false; end;
  --print(srv.."|");
  local path = srv..GenPath(id);

  os.execute("syren "..path:ShQuote().." -o "..name:ShQuote());
  return true;
end;


if #arg < 1 then
  print("usage: rtdl.lua id");
  os.exit(1);
end;

if not DownloadVideo(arg[1]) then os.exit(1); end;
print("video downloading complete.");
Linkmeow!

Comments:
[User Picture]
From:[info]koperfild
Date:August 26th, 2008 - 09:21 pm
(Link)
открываю(для себя) Америку(для других). Lua крут! воистину.
и код класен. Надо покурить мануалы.
[User Picture]
From:[info]ketmar
Date:August 27th, 2008 - 02:43 pm
(Link)
очень приятный язык. кстати, чем-то перекликается с JavaScript, но как по мне, Lua лучше. а тут, собственно, красивого кода и нет, пожалуй, кроме использования closures. %-)
From:(Anonymous)
Date:August 27th, 2008 - 12:27 pm
(Link)
на автомате прочел и только потом подумал что это израиль какой-то )
[User Picture]
From:[info]ketmar
Date:August 27th, 2008 - 02:42 pm
(Link)
а это шоп все помнили, знали и боялись мощи моего аццкава интеллехту. я, например, боюсь.