You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.3KB

  1. <?php
  2. chdir(__DIR__);
  3. $options = getopt("c:");
  4. $config = parse_ini_file($options['c'] ?? 'config.ini', true);
  5. $feedUrl = $config['feed']['url'];
  6. $hookUrl = $config['mattermost']['hook_url'];
  7. $cacheFile = $config['cache']['file'];
  8. $template = $config['mattermost']['message_template'];
  9. if (file_exists($cacheFile)) {
  10. $cache = json_decode(file_get_contents($cacheFile), true);
  11. if ($cache === null) {
  12. $cache = [];
  13. }
  14. }
  15. $feedContent = file_get_contents($feedUrl);
  16. preg_match_all('#<title><!\[CDATA\[(.*)\]\]></title>#isU', $feedContent, $titles, PREG_SET_ORDER);
  17. preg_match_all('#<link>(.*)</link>#isU', $feedContent, $links, PREG_SET_ORDER);
  18. $commands = [];
  19. foreach ($titles as $key => $title) {
  20. $link = $links[$key] ?? null;
  21. if (!$link) {
  22. continue;
  23. }
  24. $title = $title[1];
  25. $link = $link[1];
  26. $hash = md5($title.$link);
  27. if (!isset($cache[$hash])) {
  28. $cache[$hash] = true;
  29. $json = json_encode([
  30. 'text' => sprintf($template, $title, $link),
  31. ]);
  32. $commands[] = sprintf(
  33. 'curl -i -X POST -H "Content-Type: application/json" -d %s %s',
  34. escapeshellarg($json),
  35. escapeshellarg($hookUrl)
  36. );
  37. }
  38. }
  39. file_put_contents($cacheFile, json_encode($cache));
  40. foreach ($commands as $command) {
  41. shell_exec($command);
  42. }