forked from GregOriol/php-LinkyAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall.php
95 lines (72 loc) · 2.75 KB
/
all.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
<?php
require_once './Linky.php';
require_once './EnedisCredentials.php';
require_once './LinkyException.php';
require_once './DataHelper.php';
use Linky\EnedisCredentials;
use Linky\Linky;
use Linky\LinkyException;
use Linky\DataHelper;
/*
* Configuration
*/
$storage = './linky-data.json';
$enedisCredentials = new EnedisCredentials('mylogin', 'mypass');
/*
* Main
*/
$data = array(
Linky::DATASET_HOURLY => null,
Linky::DATASET_DAILY => null,
Linky::DATASET_MONTHLY => null,
Linky::DATASET_YEARLY => null,
);
if (file_exists($storage)) {
$data = json_decode(file_get_contents($storage), true);
}
try {
$linky = new Linky($enedisCredentials);
echo 'Getting all years...'.PHP_EOL;
$yearlyData = $linky->getYearlyData();
DataHelper::merge($data, Linky::DATASET_YEARLY, $yearlyData);
foreach ($yearlyData as $year => $_) {
if ($_ === null) {
continue;
}
echo 'Getting monthly for year '.$year.'...'.PHP_EOL;
try {
$monthlyData = $linky->getMonthlyData(new \DateTime($year.'-01-01', new \DateTimeZone(Linky::TIMEZONE)), new \DateTime($year.'-12-31', new \DateTimeZone(Linky::TIMEZONE)));
DataHelper::merge($data, Linky::DATASET_MONTHLY, $monthlyData);
foreach ($monthlyData as $month => $_) {
if ($_ === null) {
continue;
}
echo 'Getting daily for '.$month.'...'.PHP_EOL;
try {
$dailyData = $linky->getDailyData(new \DateTime($month.'-01', new \DateTimeZone(Linky::TIMEZONE)), new \DateTime($month.'-31', new \DateTimeZone(Linky::TIMEZONE)));
DataHelper::merge($data, Linky::DATASET_DAILY, $dailyData);
foreach ($dailyData as $day => $_) {
if ($_ === null) {
continue;
}
echo 'Getting hourly for day '.$day.'...'.PHP_EOL;
try {
$hourlyData = $linky->getHourlyData(new \DateTime($day, new \DateTimeZone(Linky::TIMEZONE)));
DataHelper::merge($data, Linky::DATASET_HOURLY, $hourlyData);
} catch (LinkyException $e) {
echo $e->getMessage().PHP_EOL;
}
}
} catch (LinkyException $e) {
echo $e->getMessage().PHP_EOL;
}
}
} catch (LinkyException $e) {
echo $e->getMessage().PHP_EOL;
}
}
} catch (LinkyException $e) {
echo $e->getMessage().PHP_EOL;
exit;
}
file_put_contents($storage, json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));