-
Notifications
You must be signed in to change notification settings - Fork 6
/
check.php
106 lines (93 loc) · 3.21 KB
/
check.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
<?php
require 'vendor/autoload.php';
use \HetznerNotify\ServerMessage as ServerMessage;
use \HetznerNotify\Config as Config;
// load configuration
$config = new Config(true);
$cache = "";
// get source from hetzner
$guzzleClient = new \GuzzleHttp\Client();
$res = $guzzleClient->request('GET', 'https://www.hetzner.com/a_hz_serverboerse/live_data.json');
if ($res->getStatusCode() != '200') {
echo "can't load hetzner data! Error ".$res->getStatusCode().die();
}
$data = json_decode($res->getBody());
$data = $data->server;
$items = [];
if($config->get('cache') && file_exists('cache.txt')) {
$contents = file_get_contents('cache.txt');
$items = explode(PHP_EOL, $contents);
}
$filteredServers = (new \HetznerNotify\ServerFilterService($data, $config->get('filter')))
->process()
->getServers();
$sortedList = [];
foreach($filteredServers as $keyId => $server) {
$cache .= $server->key . PHP_EOL;
if (!$config->get('cache') or !in_array($server->key, $items)) {
$sortedList[$keyId] = $server->cpu_benchmark;
}
}
$msgArray = [];
arsort($sortedList);
$sortedList = array_slice($sortedList, 0, $config->get('max_list'), true);
$i = 0;
foreach($sortedList as $keyId => $bench) {
$server = $filteredServers[$keyId];
if($i == 0) {
$message = '';
$message .= $config->get('mention');
$i = 1;
}
$msgArray[] = (new ServerMessage($server, $config->get('vat')))->asString();
}
$notifyClient = $config->get('client');
if(count($msgArray) < 1) {
// Exit the script, do not fire..
exit();
} else {
// We have some message to share, POST it to RC.
if($notifyClient == 'rocketchat') {
$message = '';
foreach($msgArray as $ms) {
$message .= $ms;
}
if($config->get('thanks') !== false) {
$message .= PHP_EOL . PHP_EOL;
$message .= '-------------------------------------' . PHP_EOL;
$message .= 'Hetzner Serverboerse notifier bot has been written by Rick Bakker and a bunch of other cool dudes :) ' . PHP_EOL;
$message .= '-------------------------------------' . PHP_EOL;
}
$client = new \RocketChatPhp\Client($config->get('host'), $config->get('token'));
$client->payload([
'text' => $message
]);
} elseif($notifyClient == 'discord') {
$webhook = new \DiscordWebhooks\Client($config->get('discord_webhook_url'));
$embed = new \DiscordWebhooks\Embed();
$embed->description($message);
$message .= 'I have found deals you might find interesting!';
$queue = $webhook->username('Hetzner Notifier Bot')->message($message);
foreach($msgArray as $message) {
$embed = new \DiscordWebhooks\Embed();
$embed->description($message);
$queue = $queue->embed($embed);
}
$queue->send();
} elseif($notifyClient == 'raw') {
$message = '';
foreach($msgArray as $ms) {
$message .= $ms;
}
if($config->get('thanks') !== false) {
$message .= PHP_EOL . PHP_EOL;
$message .= '-------------------------------------' . PHP_EOL;
$message .= 'Hetzner Serverboerse notifier bot has been written by Rick Bakker' . PHP_EOL;
$message .= '-------------------------------------' . PHP_EOL;
}
echo $message;
}
}
if($config->get('cache')) {
file_put_contents('cache.txt', $cache);
}