-
Notifications
You must be signed in to change notification settings - Fork 16
/
picofeed
executable file
·135 lines (115 loc) · 3.09 KB
/
picofeed
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
use PicoFeed\Config\Config;
use PicoFeed\Parser\Feed;
use PicoFeed\Reader\Favicon;
use PicoFeed\Scraper\Scraper;
use PicoFeed\Reader\Reader;
use PicoFeed\Logging\Logger;
use PicoFeed\PicoFeedException;
if (class_exists('\Monolog\Logger')) {
$log = new \Monolog\Logger('log');
$handler = new Monolog\Handler\StreamHandler('debug.log', \Monolog\Logger::DEBUG);
$log->pushHandler($handler);
Logger::setLogger($log);
printf("Monolog found, logs will be written in debug.log.\n");
} else {
printf("Monolog is not installed, we won't log anything.\n");
}
function get_feed($url, $disable_filtering = false)
{
try {
$reader = new Reader;
$resource = $reader->discover($url);
$parser = $reader->getParser(
$resource->getUrl(),
$resource->getContent(),
$resource->getEncoding()
);
if ($disable_filtering) {
$parser->disableContentFiltering();
}
return $parser->execute();
}
catch (PicoFeedException $e) {
echo 'Exception thrown ===> "'.$e->getMessage().'"'.PHP_EOL;
return false;
}
}
function get_item(Feed $feed, $item_id)
{
foreach ($feed->items as $item) {
if ($item->getId() === $item_id) {
echo $item;
echo "============= CONTENT ================\n";
echo $item->getContent();
echo "\n============= CONTENT ================\n";
break;
}
}
}
function dump_feed($url)
{
$feed = get_feed($url);
echo $feed;
}
function dump_item($url, $item_id)
{
$feed = get_feed($url);
if ($feed !== false) {
get_item($feed, $item_id);
}
}
function nofilter_item($url, $item_id)
{
$feed = get_feed($url, true);
if ($feed !== false) {
get_item($feed, $item_id);
}
}
function grabber($url)
{
$grabber = new Scraper(new Config);
$grabber->setUrl($url);
$grabber->execute();
// print_r(Logger::getMessages());
echo "============= CONTENT ================\n";
echo $grabber->getRelevantContent().PHP_EOL;
echo "============= FILTERED ================\n";
echo $grabber->getFilteredContent().PHP_EOL;
}
function fetch_favicon($url)
{
$favicon = new Favicon();
echo $favicon->find($url) . PHP_EOL;
}
// Parse command line arguments
if ($argc === 4) {
switch ($argv[1]) {
case 'item':
dump_item($argv[2], $argv[3]);
die;
case 'nofilter':
nofilter_item($argv[2], $argv[3]);
die;
}
} else if ($argc === 3) {
switch ($argv[1]) {
case 'feed':
dump_feed($argv[2]);
die;
case 'grabber':
grabber($argv[2]);
die;
case 'favicon':
fetch_favicon($argv[2]);
die;
}
}
printf("Usage:\n");
printf("%s feed <feed-url>\n", $argv[0]);
printf("%s item <feed-url> <item-id>\n", $argv[0]);
printf("%s nofilter <feed-url> <item-id>\n", $argv[0]);
printf("%s grabber <url>\n", $argv[0]);
printf("%s favicon <url>\n", $argv[0]);