1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KriKrixs\functions; |
4
|
|
|
|
5
|
|
|
use KriKrixs\object\beatmap\BeatMap; |
6
|
|
|
use KriKrixs\object\response\ResponseDownload; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* THIS CODE IS FROM WALK THOSE BRACKETS AND WAS MAINLY MADE BY HARDCPP |
10
|
|
|
*/ |
11
|
|
|
class MultiQuery { |
12
|
|
|
private string $apiUrl; |
13
|
|
|
private string $userAgent; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MultiQuery Constructor |
17
|
|
|
* @param string $apiUrl |
18
|
|
|
* @param string $userAgent |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $apiUrl, string $userAgent) |
21
|
|
|
{ |
22
|
|
|
$this->apiUrl = $apiUrl; |
23
|
|
|
$this->userAgent = $userAgent; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $p_URLs |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function DoMultiQuery($p_URLs, bool $isHash): array |
31
|
|
|
{ |
32
|
|
|
$l_Multi = curl_multi_init(); |
33
|
|
|
$apiUrlExt = $isHash ? "/maps/hash/" : "/maps/id/"; |
34
|
|
|
$l_Handles = []; |
35
|
|
|
foreach ($p_URLs as $l_URL) |
36
|
|
|
{ |
37
|
|
|
$l_CURL = curl_init($this->apiUrl.$apiUrlExt.$l_URL); |
38
|
|
|
curl_setopt($l_CURL, CURLOPT_USERAGENT, $this->userAgent); |
39
|
|
|
curl_setopt($l_CURL, CURLOPT_RETURNTRANSFER, true); |
40
|
|
|
|
41
|
|
|
$l_Handles[] = $l_CURL; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($l_Handles as $l_Current) |
45
|
|
|
curl_multi_add_handle($l_Multi, $l_Current); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$l_RunningHandles = null; |
48
|
|
|
|
49
|
|
|
do |
50
|
|
|
{ |
51
|
|
|
curl_multi_exec($l_Multi, $l_RunningHandles); |
|
|
|
|
52
|
|
|
curl_multi_select($l_Multi); |
|
|
|
|
53
|
|
|
} while ($l_RunningHandles > 0); |
54
|
|
|
|
55
|
|
|
$l_Result = []; |
56
|
|
|
foreach ($l_Handles as $l_Current) |
57
|
|
|
{ |
58
|
|
|
$l_Error = curl_error($l_Current); |
59
|
|
|
|
60
|
|
|
if (!empty($l_Error) || curl_getinfo($l_Current, CURLINFO_HTTP_CODE) !== 200) |
61
|
|
|
$l_Result[] = false; |
62
|
|
|
else |
63
|
|
|
$l_Result[] = new BeatMap(json_decode(curl_multi_getcontent($l_Current))); |
64
|
|
|
|
65
|
|
|
curl_multi_remove_handle($l_Multi, $l_Current); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
curl_multi_close($l_Multi); |
|
|
|
|
69
|
|
|
return $l_Result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function downloadMapZipAndCover(array $p_URLs, string $targetDir): ResponseDownload |
73
|
|
|
{ |
74
|
|
|
$response = new ResponseDownload(); |
75
|
|
|
|
76
|
|
|
foreach ($p_URLs as $hash => $l_URLs) { |
77
|
|
|
echo $hash . ": "; |
78
|
|
|
|
79
|
|
|
$error = false; |
80
|
|
|
|
81
|
|
|
foreach ($l_URLs as $type => $l_URL) { |
82
|
|
|
echo $type . ": "; |
83
|
|
|
|
84
|
|
|
$extension = $type === "map" ? '.zip' : '.jpg'; |
85
|
|
|
|
86
|
|
|
if (!file_exists($targetDir)) { |
87
|
|
|
mkdir($targetDir, 0777, true); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if(substr($targetDir, -1) !== "/") |
91
|
|
|
$targetDir .= "/"; |
92
|
|
|
|
93
|
|
|
//The path & filename to save to. |
94
|
|
|
$saveTo = $targetDir . $hash . $extension; |
95
|
|
|
|
96
|
|
|
$ch = curl_init($l_URL); |
97
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
98
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
99
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, ""); |
100
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); |
101
|
|
|
|
102
|
|
|
$result = curl_exec($ch); |
103
|
|
|
|
104
|
|
|
if(curl_errno($ch) === 0) { |
105
|
|
|
$response->pushDownloadMapHash($hash); |
106
|
|
|
echo "Ok "; |
107
|
|
|
} else { |
108
|
|
|
$response->pushFailMapHash($hash)->setErrorStatus(true)->setErrorMessage("Something went wrong with some maps"); |
109
|
|
|
$error = true; |
110
|
|
|
echo "Error "; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
file_put_contents($saveTo, $result); |
114
|
|
|
|
115
|
|
|
curl_close($ch); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
echo "save: " . ($error ? "No" : "Yes") . "\n"; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function buildDownloadArray($items, $isHash): array |
125
|
|
|
{ |
126
|
|
|
$downloadLinks = []; |
127
|
|
|
|
128
|
|
|
/** @var BeatMap $beatmap */ |
129
|
|
|
foreach ($this->DoMultiQuery($items, $isHash) as $beatmap) { |
130
|
|
|
$downloadLinks[$beatmap->getVersions()[0]->getHash()]["map"] = $beatmap->getVersions()[0]->getDownloadURL(); |
131
|
|
|
$downloadLinks[$beatmap->getVersions()[0]->getHash()]["cover"] = $beatmap->getVersions()[0]->getCoverURL(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $downloadLinks; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |