init
This commit is contained in:
commit
0d800b1a69
|
@ -0,0 +1,2 @@
|
||||||
|
/cache.db
|
||||||
|
/config.ini
|
|
@ -0,0 +1,9 @@
|
||||||
|
[feed]
|
||||||
|
url =
|
||||||
|
|
||||||
|
[cache]
|
||||||
|
file = 'cache.db'
|
||||||
|
|
||||||
|
[mattermost]
|
||||||
|
hook_url =
|
||||||
|
message_template =
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue