Skip to content
This repository was archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Geograph-us authored May 22, 2021
1 parent 05f4125 commit a793b79
Showing 1 changed file with 33 additions and 51 deletions.
84 changes: 33 additions & 51 deletions cloud_mail_downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@

// ======================================================================================================== //

$aria2c = "aria2c";
$main_url = "https://cloud.mail.ru/api/v2/";
$current_dir = dirname(__FILE__);
$aria2c = pathcombine($current_dir, $aria2c);
$aria2c = pathcombine($current_dir, "aria2c");
$links = file($links_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

echo "Start create input file for Aria2c Downloader..." . PHP_EOL;
foreach($links as $link)
{
$link = trim($link);
if(strpos($link, 'http') !== 0) continue;
if (strpos($link, 'http') !== 0) continue;

$file4aria = pathcombine($current_dir, "input" . time() . ".txt");
if (file_exists($file4aria)) unlink($file4aria);

$base_url = "";
if($files = GetAllFiles($link))
if (!preg_match('~/public/([^/]+/[^/]+)~', $link, $match)) die("Wrong link " . $link . PHP_EOL);
$link_id = $match[1];
$page_id = GetPageId($link);
if (!$page_id) die("Page ID not found " . $link . PHP_EOL);
$base_url = GetBaseUrl($page_id);
if (!$base_url) die("Can't get base URL " . $link . PHP_EOL);

if ($files = GetAllFiles($link_id))
{
foreach ($files as $file)
{
$line = $file->link . PHP_EOL;
$line .= " out=" . $file->output . PHP_EOL;
$line .= " referer=" . $link . PHP_EOL;
$line .= " dir=" . $download_to_folder . PHP_EOL;

$line .= "\tout=" . $file->output . PHP_EOL;
$line .= "\tdir=" . $download_to_folder . PHP_EOL;
file_put_contents($file4aria, $line, FILE_APPEND);
}
echo "Running Aria2c for download " . count($files) . " files..." . PHP_EOL;
Expand All @@ -47,7 +51,6 @@
die("Can't find any file" . PHP_EOL);
}
}

echo "Done!" . PHP_EOL;

// ======================================================================================================== //
Expand All @@ -56,57 +59,36 @@ class CMFile
{
public $link = "";
public $output = "";

function __construct($link, $output)
{
$this->output = $output;
$this->link = $link;
$this->output = $output;
}
}

// ======================================================================================================== //

function GetAllFiles($link, $folder = "")
{
global $base_url, $download_to_folder, $current_dir;
global $main_url, $base_url, $page_id;

$page = get(pathcombine($link, $folder));
if ($page === false) { echo "Error $link\r\n"; return false; }
if (($mainfolder = GetMainFolder($page)) == false) { echo "Cannot get main folder $link\r\n"; return false; }

if (!$base_url) $base_url = GetBaseUrl($page);
$json = json_decode(get("{$main_url}folder?weblink=" . $link . "&x-page-id={$page_id}"), true);

$cmfiles = array();
if ($mainfolder["name"] == "/") $mainfolder["name"] = "";
foreach ($mainfolder["list"] as $item)
if (!isset($json["body"]["list"])) return $cmfiles;
$folder = pathcombine($folder, $json["body"]["name"]);
foreach ($json["body"]["list"] as $item)
{
if ($item["type"] == "folder")
{
$files_from_folder = GetAllFiles($link, pathcombine($folder, rawurlencode(basename($item["name"]))));

if (is_array($files_from_folder))
{
foreach ($files_from_folder as $file)
{
if ($mainfolder["name"] != "")
{
$file->output = $mainfolder["name"] . "/" . windowsbadpath($file->output);
}
}
$cmfiles = array_merge($cmfiles, $files_from_folder);
}
$files_from_folder = GetAllFiles(pathcombine($link, rawurlencode($item["name"])), $folder);
$cmfiles = array_merge($cmfiles, $files_from_folder);
}
else
{
$fileurl = $item["weblink"];
$file_output = windowsbadpath(pathcombine($mainfolder["name"], $item["name"]));
$full_path = pathcombine($current_dir, $download_to_folder, $file_output);

// fix for windows 10
// reg add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f
if (strlen($full_path) >= 260) echo "WARNING: path too long " . strlen($full_path) . " > 260 chars: " . $full_path;

$cmfiles[] = new CMFile(pathcombine($base_url, $fileurl), $file_output);
$file_output = windowsbadpath(pathcombine($folder != "/" ? $folder : "", $item["name"]));
$download_url = pathcombine($base_url, $link, $folder != "/" ? rawurlencode($item["name"]) : "");
$cmfiles[] = new CMFile($download_url, $file_output);
}
}

Expand All @@ -126,21 +108,21 @@ function StartDownload()

// ======================================================================================================== //

function GetMainFolder($page)
function GetPageId($url)
{
if (preg_match('~"folder":\s+(\{.*?\}\s+\]\s+\})\s+\}~s', $page, $match))
{
return json_decode($match[1], true);
}
else return false;
$page = get($url);
if (preg_match('~pageId:"([^"]+)"~s', $page, $match)) return $match[1];
return false;
}

// ======================================================================================================== //

function GetBaseUrl($page)
function GetBaseUrl($page_id)
{
if (preg_match('~"weblink_get":.*?"url":\s*"(https:[^"]+)~s', $page, $match)) return $match[1];
else return false;
global $main_url;
$json = json_decode(get("{$main_url}dispatcher?x-page-id={$page_id}"), true);
if (isset($json["body"]["weblink_get"][0]["url"])) return $json["body"]["weblink_get"][0]["url"];
return false;
}

// ======================================================================================================== //
Expand Down

0 comments on commit a793b79

Please sign in to comment.