-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.php
142 lines (124 loc) · 4.55 KB
/
run.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
<?php
declare(strict_types=1);
/**
* run.php
* Copyright (c) 2022 [email protected].
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use App\Exception\DownloadException;
use App\RateDownloader\ApiLayerDownloader;
use App\RateDownloader\DownloaderInterface;
use App\RateDownloader\ExchangeRateDownloader;
use Carbon\Carbon;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
include 'vendor/autoload.php';
$timezone = 'Europe/Amsterdam';
$logLevel = Level::Debug;
$currencies = explode(',', $argv[1]);
$handler = new StreamHandler('php://stdout', $logLevel);
$formatter = new LineFormatter(null, null, false, true);
$log = new Logger('exchange-rates');
$formatter->setDateFormat('Y-m-d H:i:s');
$handler->setFormatter($formatter);
$log->pushHandler($handler);
/*
* Here we go:
*/
$log->debug('Start of Exchange Rates 1.0');
/*
* Variables for the run:
*/
$date = date('Y-m-d');
$final = [];
$destination = sprintf('result/%s.json', $date);
$downloaders = [ExchangeRateDownloader::class, ApiLayerDownloader::class];
$tokens = [getenv('EXCHANGE_RATE_KEY'), getenv('API_LAYER_KEY')];
$result = [];
if (file_exists($destination)) {
$log->error(sprintf('Destination "%s" already exists, will not run again.', $destination));
}
/*
* Here we go for real
*/
if (!file_exists($destination)) {
$log->debug(sprintf('Will search for these currencies: %s', join(', ', $currencies)));
foreach ($currencies as $from) {
$log->debug(sprintf('Will now query rates of currency "%s"', $from));
// at this point several services exist that we can poll for data, and we support many of them (or at least two).
foreach ($downloaders as $index => $downloader) {
/** @var DownloaderInterface $object */
$object = new $downloader($log);
$object->setFrom($from);
$object->setTo($currencies);
$object->setToken($tokens[$index]);
try {
$object->download();
} catch (DownloadException $e) {
$log->error(sprintf('Could not download rates for %s: %s', $from, $e->getMessage()));
continue;
}
/**
* Expects the following format:
* YYYY-MM-DD => [
* ABC => rate
* DEF => rate
*
* etc.
*/
$result[$from] = $object->getResult();
if (count($result) === 0) {
$log->error(sprintf('No rates found for %s', $from));
continue;
}
break;
}
sleep(4);
}
}
if (0 === count($result)) {
$log->error('No rates found at all. Will exit now.');
exit(1);
}
/*
* Parse results into JSON file for weekly consumption by clients
* This is a separate step so the download can be skipped if necessary.
*/
$path = realpath('rates');
$log->debug(sprintf('Will store rates in %s', $path));
foreach ($result as $from => $set) {
foreach ($set as $date => $rates) {
$carbon = Carbon::createFromFormat('Y-m-d', $date, $timezone);
$log->debug(sprintf('[%s] Running for week %d, %d', $from, $carbon->isoWeek, $carbon->year));
$current = sprintf('%s/%d/%d/%s.json', $path, $carbon->year, $carbon->isoWeek, $from);
if (!file_exists(dirname($current))) {
mkdir(dirname($current), 0777, true);
$log->debug(sprintf('Created directory "%s"', dirname($current)));
}
$content = [
'date' => $date,
'rates' => [],
];
foreach ($rates as $to => $rate) {
$content['rates'][$to] = $rate;
}
$log->debug(sprintf('Stored file "%s" with %d rates', $current, count($content['rates'])));
file_put_contents($current, json_encode($content, JSON_PRETTY_PRINT));
}
}