-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
272 lines (232 loc) · 7.53 KB
/
cron.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/***
* @copyright 2017 Ruben Schulz
* @author Ruben Schulz <[email protected]>
* @package Crypto Dashboard
* @link http://www.rubenschulz.nl/
* @version 1.0
***/
/*** Init system ***/
// Set UTF-8 header
header('Content-type: text/html; charset=UTF-8');
// Set error reporting & zlib output compression
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('zlib.output_compression', true);
// Set default timezone
date_default_timezone_set('Europe/Amsterdam');
// Require config
require_once(__DIR__.'/config/config.inc.php');
/*** Set variables ***/
$api_url = 'https://api.coinmarketcap.com/v1/ticker/';
$response = array();
$history = array();
$totals = array();
$data = array();
$sql = array();
/*** Actions ***/
// Open connection
$curl = curl_init();
// Build header
$header = array();
$header[] = 'Content-Type: application/json';
// Set options
curl_setopt($curl, CURLOPT_URL, $api_url.'?limit=0&convert=EUR');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute call
$result = curl_exec($curl);
// Get result parts
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$response = substr($result, $header_size);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Close connection
curl_close($curl);
// Decode response
$response = json_decode($response);
/*** Databaase ***/
try {
// Connect to database
$database = new PDO('mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME, DATABASE_USERNAME, DATABASE_PASSWORD);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get transactions
$sql['get_transactions'] = "
SELECT
DISTINCT (transactions.label)
FROM
transactions
ORDER BY
transactions.label ASC
";
$data['transactions'] = $database->query($sql['get_transactions'])->fetchAll(PDO::FETCH_ASSOC);
// Rebuild array
$transactions = array();
foreach($data['transactions'] as $transaction){
$transactions[] = $transaction['label'];
}
// Build SQL
$sql['save_market'] = "
INSERT INTO markets (
label,
name,
price_btc,
price_usd,
price_eur,
volume_24h,
timestamp
) VALUES (
:label,
:name,
:price_btc,
:price_usd,
:price_eur,
:volume_24h,
:timestamp
)
";
// Prepare statement
$statement = $database->prepare($sql['save_market']);
// Loop through markets
foreach($response as $market){
// Check if market in transactions
if(in_array($market->symbol, $transactions)){
// Bind values
$statement->bindValue(':label', $market->symbol);
$statement->bindValue(':name', $market->name);
$statement->bindValue(':price_btc', $market->price_btc);
$statement->bindValue(':price_usd', $market->price_usd);
$statement->bindValue(':price_eur', $market->price_eur);
$statement->bindValue(':volume_24h', $market->{'24h_volume_eur'});
$statement->bindValue(':timestamp', date('Y-m-d H:i:00'));
// Execute statement
$statement->execute();
}
}
// Delete Bitgem
$database->query('DELETE FROM markets WHERE name = "Bitgem"');
// Set memory limit
ini_set('memory_limit', '256M');
// Loop through transactions
foreach($transactions as $transaction){
// Get history
$sql['get_history_'.$transaction] = "
SELECT
markets.label,
markets.name,
UNIX_TIMESTAMP(intervals.timestamp) * 1000 AS timestamp,
SUM(transactions.amount) AS amount,
markets.price_eur * SUM(transactions.amount) AS value
FROM
intervals
LEFT JOIN
markets
ON
intervals.timestamp = markets.timestamp
AND markets.label = '".$transaction."'
LEFT JOIN
transactions
ON
markets.label = transactions.label
AND markets.timestamp >= transactions.timestamp
WHERE 1 = 1
AND intervals.timestamp <= '".date('Y-m-d H:i:s')."'
AND transactions.amount IS NOT NULL
GROUP BY
intervals.timestamp
ORDER BY
intervals.timestamp ASC
";
$data['history_'.$transaction] = $database->query($sql['get_history_'.$transaction])->fetchAll(PDO::FETCH_ASSOC);
if(!empty($data['history_'.$transaction])){
// Set history
$history[$transaction] = array();
$history[$transaction]['name'] = $data['history_'.$transaction][0]['name'];
$history[$transaction]['data'] = array();
// Loop trough transactions
foreach($data['history_'.$transaction] as $key => $value){
$history[$transaction]['data'][] = array(
(int)$value['timestamp'],
(float)$value['value'],
(float)$value['amount']
);
}
}
// Get history
$sql['get_totals_'.$transaction] = "
SELECT
markets.label,
markets.name,
UNIX_TIMESTAMP(intervals.timestamp) * 1000 AS timestamp,
MIN(markets.price_eur) AS minimum,
MAX(markets.price_eur) AS maximum,
AVG(markets.price_eur) AS average,
(
SELECT
SUM(transactions.amount)
FROM
transactions
WHERE 1 = 1
AND transactions.label = '".$transaction."'
AND markets.timestamp >= transactions.timestamp
GROUP BY
transactions.label
) AS amount
FROM
intervals
LEFT JOIN
markets
ON
intervals.timestamp = markets.timestamp
AND markets.label = '".$transaction."'
LEFT JOIN
transactions
ON
markets.label = transactions.label
AND markets.timestamp >= transactions.timestamp
WHERE 1 = 1
AND intervals.timestamp <= NOW()
GROUP BY
DATE(intervals.timestamp)
ORDER BY
intervals.timestamp ASC
";
$data[$transaction] = $database->query($sql['get_totals_'.$transaction])->fetchAll(PDO::FETCH_ASSOC);
// Rebuild data
foreach($data[$transaction] as $value){
// Set row
$data['totals'][$value['timestamp']]['timestamp'] = (int)$value['timestamp'];
$data['totals'][$value['timestamp']]['minimum'] = !empty($data['totals'][$value['timestamp']]['minimum']) ? $data['totals'][$value['timestamp']]['minimum'] + ($value['amount'] * $value['minimum']) : $value['amount'] * $value['minimum'];
$data['totals'][$value['timestamp']]['maximum'] = !empty($data['totals'][$value['timestamp']]['maximum']) ? $data['totals'][$value['timestamp']]['maximum'] + ($value['amount'] * $value['maximum']) : $value['amount'] * $value['maximum'];
$data['totals'][$value['timestamp']]['average'] = !empty($data['totals'][$value['timestamp']]['average']) ? $data['totals'][$value['timestamp']]['average'] + ($value['amount'] * $value['average']) : $value['amount'] * $value['average'];
}
}
// Rebuild data
foreach($data['totals'] as $key => $value){
$totals['averages'][] = array(
(int)$value['timestamp'],
(int)$value['average']
);
$totals['ranges'][] = array(
(int)$value['timestamp'],
(int)$value['minimum'],
(int)$value['maximum']
);
}
// Write JSON
$fp = fopen('data/data-history.json', 'w');
fwrite($fp, json_encode($history));
fclose($fp);
// Write JSON
$fp = fopen('data/data-totals.json', 'w');
fwrite($fp, json_encode($totals));
fclose($fp);
// Close connection
$database = null;
}catch(PDOException $e){
// Print error message
echo $e->getMessage();
}
?>