This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoins.php
126 lines (89 loc) · 3.89 KB
/
coins.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
<?php
class CoinGeckoImages {
public function execute($testRun = false) {
echo $this->consoleHeader("CoinGeckoImages");
// Check if the directory exists. If not create one
if (!file_exists("./images-coins")) {
mkdir("./images-coins", 0777);
}
// NOTE: Nothing special, used for testing only
// The testfile contains a couple coins for testing.
if($testRun == true) {
$json = json_decode(file_get_contents("./test/list-coins.json"), true);
} else {
$json = json_decode($this->httpRequest("https://api.coingecko.com/api/v3/coins/list"), true);
}
$totalCount = count($json) - 1;
for ($i=0; $i <= $totalCount; $i++) {
if($this->fileCheck($json[$i]["id"]) == false) {
echo $this->console($json[$i]["id"]);
$jsonTarget = json_decode($this->httpRequest("https://api.coingecko.com/api/v3/coins/" . $json[$i]["id"]), false);
$imageUrl = $jsonTarget->image->large;
if($imageUrl !== null) {
// Need to take the filetype to extension conversion
$extension = pathinfo(parse_url($imageUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
// Making all lowerCase because otherwise you can have .PNG instead of .png
// CoinGecko has saved an published some images with capital extensions
$filename = strtolower("./images-coins/" . $jsonTarget->id . "." . $extension);
// Simple one liner
// Using PHP-GD extension to make a PNG file from the input.
file_put_contents($filename, $this->httpRequest($imageUrl));
}
// Sleeping is needed because of the CoinGecko API ratelimit.
usleep(750000);
}
}
// Displays in hours because of lots of crypto takes a lot of time.
echo $this->consoleHeader("FINISHED");
}
private function httpRequest($endpoint) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $endpoint,
/*
BUG: SSL operation failed with code 1... & Failed to enable crypto in...
Solution disable SSL verification. Good solution? Nope. Might fix later
*/
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
private function console($input) {
// Safe input
if($input == null) {
$input = "n/a";
}
// Date time
// EXAMPLE: [23:57:04] ethereum
$dt = '[' . date("H:i:s") . '] ';
return $dt . $input . PHP_EOL;
}
private function consoleHeader($input) {
$line = "----------------------------------------------------------------------------" . PHP_EOL;
$return = PHP_EOL . $line;
$return .= $input . PHP_EOL;
$return .= $line . PHP_EOL;
return $return;
}
private function fileCheck($id) {
// The empty-extension (last in Array) is needed because two files use no extensions and just a dot
foreach([".png", ".jpg", ".webp", ".jpeg", ".ico", ".svg", ".gif", "."] as $extension) {
if(file_exists("./images-coins/" . $id . $extension)) {
return true;
}
}
return false;
}
}
$cgi = new CoinGeckoImages();
$cgi->execute();