This commit is contained in:
Simon Vieille 2019-07-25 09:30:24 +02:00
commit 0d800b1a69
Signed by: deblan
GPG Key ID: 03383D15A1D31745
3 changed files with 69 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/cache.db
/config.ini

9
config.ini.dist Normal file
View File

@ -0,0 +1,9 @@
[feed]
url =
[cache]
file = 'cache.db'
[mattermost]
hook_url =
message_template =

58
index.php Normal file
View File

@ -0,0 +1,58 @@
<?php
chdir(__DIR__);
$config = parse_ini_file('config.ini', true);
$feedUrl = $config['feed']['url'];
$hookUrl = $config['mattermost']['hook_url'];
$cacheFile = $config['cache']['file'];
$template = $config['mattermost']['message_template'];
if (file_exists($cacheFile)) {
$cache = json_decode(file_get_contents($cacheFile), true);
if ($cache === null) {
$cache = [];
}
}
$feedContent = file_get_contents($feedUrl);
preg_match_all('#<title><!\[CDATA\[(.*)\]\]></title>#isU', $feedContent, $titles, PREG_SET_ORDER);
preg_match_all('#<link>(.*)</link>#isU', $feedContent, $links, PREG_SET_ORDER);
$commands = [];
foreach ($titles as $key => $title) {
$link = $links[$key] ?? null;
if (!$link) {
continue;
}
$title = $title[1];
$link = $link[1];
$hash = md5($title.$link);
if (!isset($cache[$hash])) {
$cache[$hash] = true;
$json = json_encode([
'text' => sprintf($template, $title, $link),
]);
$commands[] = sprintf(
'curl -i -X POST -H "Content-Type: application/json" -d %s %s',
escapeshellarg($json),
escapeshellarg($hookUrl)
);
}
}
file_put_contents($cacheFile, json_encode($cache));
foreach ($commands as $command) {
shell_exec($command);
}