forked from Geograph-us/Cloud-Mail.Ru-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
162 lines (132 loc) · 5.15 KB
/
[email protected]_downloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
$links_file = "links.txt";
$storage_path = "downloads";
$file4aria = "input.txt";
$aria2c = "aria2c";
$current_dir = dirname(__FILE__);
// ======================================================================================================== //
$file4aria = pathcombine($current_dir, $file4aria);
$aria2c = pathcombine($current_dir, $aria2c);
if (file_exists($file4aria)) unlink($file4aria);
$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)
{
$base_url = "";
$id = "";
if($files = GetAllFiles($link))
{
foreach ($files as $file)
{
$line = $file->download_link . PHP_EOL;
$line .= " out=" . $file->output . PHP_EOL;
$line .= " referer=" . $link . PHP_EOL;
$line .= " dir=" . $storage_path . PHP_EOL;
file_put_contents($file4aria, $line, FILE_APPEND);
}
}
}
echo "Running Aria2c for download..." . PHP_EOL;
StartDownload();
@unlink($file4aria);
echo "Done!" . PHP_EOL;
// ======================================================================================================== //
class CMFile
{
public $name = "";
public $output = "";
public $link = "";
public $download_link = "";
function __construct($name, $output, $link, $download_link)
{
$this->name = $name;
$this->output = $output;
$this->link = $link;
$this->download_link = $download_link;
}
}
// ======================================================================================================== //
function GetAllFiles($link, $folder = "")
{
global $base_url, $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);
if (!$id && preg_match('~\/public\/(.*)~', $link, $match)) $id = $match[1];
$cmfiles = array();
if ($mainfolder["name"] == "/") $mainfolder["name"] = "";
foreach ($mainfolder["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"] . "/" . $file->output;
}
$cmfiles = array_merge($cmfiles, $files_from_folder);
}
}
else
{
$fileurl = pathcombine($folder, rawurlencode($item["name"]));
// Старые ссылки содержат название файла в id
if (strpos($id, $fileurl) !== false) $fileurl = "";
$cmfiles[] = new CMFile($item["name"],
pathcombine($mainfolder["name"], $item["name"]),
pathcombine($link, $fileurl),
pathcombine($base_url, $id, $fileurl));
}
}
return $cmfiles;
}
// ======================================================================================================== //
function StartDownload()
{
global $aria2c, $file4aria;
$command = "\"{$aria2c}\" --file-allocation=none --min-tls-version=TLSv1 --max-connection-per-server=10 --split=10 --max-concurrent-downloads=10 --summary-interval=0 --continue --user-agent=\"Mozilla/5.0 (compatible; Firefox/3.6; Linux)\" --input-file=\"{$file4aria}\"";
passthru("{$command}");
}
// ======================================================================================================== //
function GetMainFolder($page)
{
if (preg_match('~"folder":\s+(\{.*?"id":\s+"[^"]+"\s+\})\s+}~s', $page, $match)) return json_decode($match[1], true);
else return false;
}
// ======================================================================================================== //
function GetBaseUrl($page)
{
if (preg_match('~"weblink_get":.*?"url":\s*"(https:[^"]+)~s', $page, $match)) return $match[1];
else return false;
}
// ======================================================================================================== //
function get($url)
{
$proxy = null; //"127.0.0.1:8888";
$http["method"] = "GET";
if ($proxy) { $http["proxy"] = "tcp://" . $proxy; $http["request_fulluri"] = true; }
$options['http'] = $http;
$context = stream_context_create($options);
$body = @file_get_contents($url, NULL, $context);
return $body;
}
// ======================================================================================================== //
function pathcombine()
{
$result = "";
foreach (func_get_args() as $arg)
{
if ($arg !== '')
{
if ($result && substr($result, -1) != "/") $result .= "/";
$result .= $arg;
}
}
return $result;
}
// ======================================================================================================== //
?>