Browse Source

init

master
Simon Vieille 8 months ago
commit
0d800b1a69
Signed by: deblan <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
3 changed files with 69 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +9
    -0
      config.ini.dist
  3. +58
    -0
      index.php

+ 2
- 0
.gitignore View File

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

+ 9
- 0
config.ini.dist View File

@@ -0,0 +1,9 @@
[feed]
url =

[cache]
file = 'cache.db'

[mattermost]
hook_url =
message_template =

+ 58
- 0
index.php 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);
}

Loading…
Cancel
Save