-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.php
executable file
·73 lines (66 loc) · 1.98 KB
/
run.php
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
#!/usr/bin/env php
<?php
/**
* Only permit execution from the command-line.
*/
if (php_sapi_name() !== 'cli') {
echo "This script must be called from the command line.";
exit(1);
}
require_once __DIR__ . '/vendor/autoload.php';
/**
* Get the feed list and set up SimplePie.
* Checking for .csv is for backwards compatibility to pre-1.0.
*/
$simplepie = new SimplePie();
$feedsFile = is_readable(__DIR__ . '/feeds.csv') ? __DIR__ . '/feeds.csv' : __DIR__ . '/feeds.txt';
$feedUrls = array_filter(file($feedsFile, FILE_IGNORE_NEW_LINES));
$simplepie->set_feed_url($feedUrls);
$ua = 'Mozilla/4.0 (compatible; Sourdust Feed Aggregator https://github.com/samwilson/sourdust-feed-aggregator)';
$simplepie->set_useragent($ua);
/**
* Set up caching.
*/
$cacheDir = __DIR__ . '/cache';
if (!is_dir($cacheDir)) {
mkdir($cacheDir);
}
$simplepie->set_cache_location($cacheDir);
/**
* Run the actual feed aggregation and check for errors.
*/
$simplepie->init();
if ($simplepie->error()) {
$errors = !is_array($simplepie->error()) ? [$simplepie->error()] : $simplepie->error();
echo "Errors:\n " . join("\n ", $errors)."\n";
}
/**
* Get feed data.
*/
$feeds = [];
foreach ($simplepie->get_items() as $item) {
$feeds[$item->get_feed()->get_title()] = $item->get_feed();
}
ksort($feeds);
/**
* Create the output HTML and cache it for use by the syntax component.
*/
$loader = new \Twig_Loader_Filesystem([__DIR__]);
$twig = new \Twig_Environment($loader);
$twig->enableDebug();
$twig->addExtension(new \Twig_Extension_Debug());
// Render all templates.
foreach (glob(__DIR__ . '/*.twig') as $tpl) {
if (strpos('_example', $tpl) !== false) {
continue;
}
$template = $twig->loadTemplate(basename($tpl));
$out = $template->render([
'simplepie' => $simplepie,
'date_RFC2822' => date('r'),
'date_ISO8601' => date('c'),
'feeds' => $feeds,
]);
$name = pathinfo($tpl, PATHINFO_FILENAME);
file_put_contents(__DIR__ . '/' . $name, $out);
}