-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss
executable file
·64 lines (50 loc) · 1.57 KB
/
rss
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
#!/usr/bin/php
<?php
if ($_SERVER['argc'] === 1) {
echo 'Uso: rss tag [tag] [tag] ...'."\n";
exit(1);
}
$tags = array();
$filtros = array();
$limite = 0;
for ($i = 1; $i < $_SERVER['argc']; $i += 1) {
if (strstr($_SERVER['argv'][$i], '=')) {
$campos = explode('=', $_SERVER['argv'][$i]);
$filtros[$campos[0]] = $campos[1];
}
elseif (strstr($_SERVER['argv'][$i], '-')) {
$limite = str_replace('-', '', $_SERVER['argv'][$i]);
$limite = is_numeric($limite) ? $limite : 0;
}
else {
$tags[$_SERVER['argv'][$i]] = '';
}
}
$hay_filtros = (count($filtros) > 0);
$stdin = fopen('php://stdin', 'r');
$rss = stream_get_contents($stdin);
fclose($stdin);
$xml = simplexml_load_string($rss);
$contador = 0;
foreach ($xml->channel->item as $item) {
$condicion = true;
if ($hay_filtros) {
foreach ($filtros as $key => $value) {
$condicion &= (strstr(strtoupper($item->{$key}), strtoupper($value)) !== FALSE);
}
}
if ($condicion) {
$c = 1;
foreach ($item as $key => $value) {
if (array_key_exists($key, array_merge($tags, $filtros))) {
passthru('tput setaf '.$c.'; echo "'.$value.'";tput op');
}
$c += 2;
}
$contador += 1;
if ($limite > 0 && $contador >= $limite) {
break;
}
}
}
?>