-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathimport_files.php
42 lines (30 loc) · 1.16 KB
/
import_files.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
<?php
require('./vendor/autoload.php');
use Carbon\Carbon;
use Dotenv\Dotenv;
$dotenv = new Dotenv(__DIR__);
$dotenv->load();
$db = new mysqli($_ENV['MYSQL_HOST'], $_ENV['MYSQL_USER'], $_ENV['MYSQL_PASS'], $_ENV['MYSQL_DB']);
$dir = './data';
$fileList = scandir($dir);
foreach ($fileList as $fileName) {
if (substr($fileName, -4) !== "json") {
fwrite(STDOUT, "[-] Skipping: {$fileName}" . PHP_EOL);
continue;
}
$file = json_decode(file_get_contents("{$dir}/{$fileName}"), true);
foreach ($file['series']['data'] as $bar) {
$start = (new Carbon($bar['time']))->format('Y-m-d H:i:s');
$open = (double)$bar['open'];
$close = (double)$bar['close'];
$high = (double)$bar['high'];
$low = (double)$bar['low'];
$volume = (double)$bar['volume'];
$db->query("INSERT IGNORE INTO {$symbol} VALUES('{$start}', '{$open}', '{$high}', '{$low}', '{$close}', '{$volume}')");
if ($db->error) {
fwrite(STDOUT, "[!] MYSQL ERROR: {$db->error}" . PHP_EOL);
}
}
fwrite(STDOUT, "[+] Processed: {$dir}/{$fileName}" . PHP_EOL);
}
$db->close();