-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
213 lines (175 loc) · 7.33 KB
/
cron.php
File metadata and controls
213 lines (175 loc) · 7.33 KB
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
<?php
/**
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 Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use SimplePie\SimplePie;
include_once('conf/config.php');
require __DIR__ . '/vendor/autoload.php';
if (defined('LOGS_DAYS_TO_KEEP')) {
$handler = new RotatingFileHandler(__DIR__ . '/logs/synchronization.log', LOGS_DAYS_TO_KEEP);
} else {
$handler = new RotatingFileHandler(__DIR__ . '/logs/synchronization.log', 7);
}
#$stream = new StreamHandler(__DIR__ . '/logs/synchronization.log', Logger::DEBUG);
$logger = new Logger('SyncLogger');
#$logger->pushHandler($stream);
$logger->pushHandler($handler);
$logger->info('Sync started');
function convertFileSize($bytes)
{
if ($bytes < 1024) {
return round(($bytes / 1024), 2) . ' o';
} elseif (1024 < $bytes && $bytes < 1048576) {
return round(($bytes / 1024), 2) . ' ko';
} elseif (1048576 < $bytes && $bytes < 1073741824) {
return round(($bytes / 1024) / 1024, 2) . ' Mo';
} elseif (1073741824 < $bytes) {
return round(($bytes / 1024) / 1024 / 1024, 2) . ' Go';
}
}
function getEnclosureHtml($enclosure)
{
$html = '';
if ($enclosure != null && $enclosure->link != '') {
$enclosureName = substr(
$enclosure->link,
strrpos($enclosure->link, '/') + 1,
strlen($enclosure->link)
);
$enclosureArgs = strpos($enclosureName, '?');
if ($enclosureArgs !== false)
$enclosureName = substr($enclosureName, 0, $enclosureArgs);
$enclosureFormat = isset($enclosure->handler)
? $enclosure->handler
: substr($enclosureName, strrpos($enclosureName, '.') + 1);
$html = '<div class="enclosure"><h1>Fichier média :</h1>';
$enclosureType = $enclosure->get_type();
if (strpos($enclosureType, 'image/') === 0) {
$html .= '<img src="' . $enclosure->link . '" />';
} elseif (strpos($enclosureType, 'audio/') === 0) {
$html .= '<audio src="' . $enclosure->link . '" preload="none" controls>Audio not supported</audio>';
} elseif (strpos($enclosureType, 'video/') === 0) {
$html .= '<video src="' . $enclosure->link . '" preload="none" controls>Video not supported</video>';
} else {
$html .= '<a href="' . $enclosure->link . '"> ' . $enclosureName . '</a>';
}
$html .= ' <span>(Format ' . strtoupper($enclosureFormat) . ', ' . convertFileSize($enclosure->length) . ')</span></div>';
}
return $html;
}
$sp = new \SimplePie();
$sp->enable_cache(true);
$sp->set_cache_location (__DIR__ . '/cache');
$sp->set_useragent('Mozilla/5.0 (compatible; Exabot/3.0; +http://www.exabot.com/go/robot)');
$db = new mysqli(MYSQL_HOST, MYSQL_LOGIN, MYSQL_MDP, MYSQL_BDD);
$db->set_charset('utf8mb4');
$db->query('SET NAMES utf8mb4');
$query_feed = "select * from flux order by name";
$result_feed = $db->query($query_feed);
$start = microtime(true);
$currentDate = date('d/m/Y H:i:s');
$nbErrors = 0;
$nbOk = 0;
$nbTotal = 0;
$localTotal = 0; // somme de tous les temps locaux, pour chaque flux
$nbTotalEventsMaj = 0;
$nbTotalEventsIns = 0;
$syncId = time();
while ($row = $result_feed->fetch_array()) {
$fluxName = $row['name'];
$fluxUrl = $row['url'];
$fluxId = $row['id'];
$sp->set_feed_url($fluxUrl);
$sp->handle_content_type();
echo "\tParse flux: \t{$fluxName}\n";
$logger->info("\tParse flux: \t{$fluxName}\n");
echo "\t\tFlux id: \t$fluxId\n";
$logger->info("\t\tFlux id: \t$fluxId\n");
if (!$sp->init()) {
$error = $sp->error;
$lastSyncInError = 1;
$nbErrors += 1;
echo "\t\tFlux id: \t$sp->error()\n";
$logger->error("\t\tError: \t$sp->error()\n");
continue;
}
$linesUpdated = 0;
$linesInserted = 0;
foreach ($sp->get_items() as $item) {
#$guid = $item->get_id(true);
$guid = md5($item->get_permalink());
$permalink = $item->get_permalink();
$content = $db->real_escape_string($item->get_content());
$title = $db->real_escape_string(mb_strimwidth($item->get_title(), 0, 250, "..."));
$description = $db->real_escape_string(mb_strimwidth($item->get_description(), 0, 300, "..."));
$link = $item->get_link();
$enclosure = getEnclosureHtml($item->get_enclosure());
$author = '';
if ($creator = $item->get_author()) {
$author = $creator->get_name();
} elseif ($item->get_authors()) {
foreach ($item->get_authors() as $creator) {
$author .= $creator->get_name() . ',';
}
} else {
$author = mb_strimwidth($link, 0, 152, "...");
}
$author = mb_strimwidth($author, 0, 152, "...");
if (is_numeric($item->get_date())) {
$pubdate = $item->get_date();
} elseif ($item->get_date()) {
$pubdate = strtotime($item->get_date());
} else {
$pubdate = time();
}
$insertOrUpdate = "INSERT INTO influx.items VALUES ('" . $guid . "','" . $title . "','" . $db->real_escape_string($author) . "','" . $content . $enclosure . "','" . $description . "','" . $permalink . "',1," . $fluxId . ",0," . $pubdate . ',' . time() . ',' . $syncId . ") ON DUPLICATE KEY UPDATE title = '" . $title . "', content = '" . $content . "'";
$db->query($insertOrUpdate);
$ret = $db->affected_rows;
if ($ret == 1) {
$linesInserted += 1;
$nbTotalEventsIns += 1;
} elseif ($ret == 2) {
$linesUpdated += 1;
$nbTotalEventsMaj += 1;
}
if ($db->errno) {
echo "\t\tFailure: \t$db->error\n";
$logger->info("\t\tFailure: \t$db->error\n");
$logger->error($insertOrUpdate);
}
}
$nbOk += 1;
echo "\t\tLines updated: $linesUpdated\n";
$logger->info("\t\tLines updated: $linesUpdated\n");
echo "\t\tLines inserted: $linesInserted\n";
$logger->info("\t\tLines inserted: $linesInserted\n");
}
$totalTime = microtime(true) - $start;
$totalTimeStr = number_format($totalTime, 3);
$currentDate = date('d/m/Y H:i:s');
echo "\t{$nbErrors}\t" . 'ERRORS' . "\n";
$logger->error("\t{$nbErrors}\t" . 'ERRORS' . "\n");
echo "\t{$nbOk}\t" . 'GOOD' . "\n";
$logger->info("\t{$nbOk}\t" . 'GOOD' . "\n");
echo "\t{$nbTotal}\t" . 'AT_TOTAL' . "\n";
$logger->info("\t{$nbTotal}\t" . 'AT_TOTAL' . "\n");
echo "\t$currentDate\n";
$logger->info("\t$currentDate\n");
echo "\tTotal Updated\t$nbTotalEventsMaj\n";
$logger->info("\tTotal Updated\t$nbTotalEventsMaj\n");
echo "\tTotal Inserted\t$nbTotalEventsIns\n";
$logger->info("\tTotal Inserted\t$nbTotalEventsIns\n");
echo "\t{$totalTimeStr}\t" . 'SECONDS' . "\n";
$logger->info("\t{$totalTimeStr}\t" . 'SECONDS' . "\n");