added fefe's blog reader
[dbstuff.git] / fefe.pl
blob27f9f4f702287ffc5cb5aae894e31a070206897d
1 #!/usr/bin/perl
3 # a quick-n-dirty cmdline pager for fefes blog
4 # navigate with 'n', 'p' and 'q'
5 # startup options: --color
6 # by Daniel Borkmann, borkmann@gnumaniacs.org
8 use strict;
9 use warnings;
10 use utf8;
11 use Encode;
12 use XML::RSS;
13 use LWP::UserAgent;
14 use Term::ANSIColor;
15 use Term::Size;
16 require HTML::TreeBuilder;
17 require HTML::FormatText;
19 my $color = 0;
20 my $with_colors = 0;
21 my ($columns, $rows) = Term::Size::chars *STDOUT{IO};
22 my $agent = LWP::UserAgent->new;
23 my $parser = XML::RSS->new;
24 my $formatter = HTML::FormatText->new( leftmargin => 0
25 , rightmargin => $columns - 2 );
26 my $rawrss = HTTP::Request->new(GET => 'http://blog.fefe.de/rss.xml?html');
27 my @colors = ( 'red'
28 , 'green'
29 , 'yellow'
30 , 'blue'
31 , 'magenta'
32 , 'cyan'
33 , 'white' );
34 my @links;
35 my $max;
37 $with_colors = 1 if (defined($ARGV[0]) && $ARGV[0] eq "--color");
38 $agent->default_header( 'Accept-Language' => 'en-US'
39 , 'Accept-Charset' => 'utf-8'
40 , 'Accept' => '*/*' );
41 $agent->agent('Fnord-Pager 1.0');
42 $agent->timeout(10);
43 $agent->env_proxy;
44 $parser->parse($agent->request($rawrss)->content);
46 sub cite
48 my $ref = scalar(@links);
49 my $link = shift;
50 $link = "http://blog.fefe.de".$link if ($link =~ m/^\/\?ts=/);
51 push(@links, $link);
52 return "$ref";
55 open(TTY, "+</dev/tty") or die "No tty: $!";
56 system "stty cbreak </dev/tty >/dev/tty 2>&1";
58 $max = scalar(@{$parser->{'items'}});
59 for (my $elem = 0; $elem < $max; )
61 my ($cmd, $html, $count, $lsize);
62 my $item = $parser->{'items'}[$elem];
63 my $content = $item->{'description'};
65 system "clear";
67 @links = ();
69 $content =~ s/^\s+//;
70 $content =~ s/\s+$//;
71 $content =~ s/<a\s*href=\"(.*?)\">(.*?)<\/a>/"$2 \[".cite($1)."\]"/eg;
73 $lsize = scalar(@links);
74 $content = $content."\n<ul>";
75 foreach ($count = 0; $count < $lsize; $count++)
77 $content = $content."<li>\[$count\] ".shift(@links)."</li>";
79 $content = $content."</ul>";
80 $content = "<html><body>".$content."</body></html>";
82 $html = HTML::TreeBuilder->new_from_content($content);
83 $content = $formatter->format($html);
84 $content = encode('utf-8', $content);
86 if ($with_colors == 1) {
87 print colored ['bold '.$colors[$color].' on_black'], "\r".$content;
88 print colored ['reset'], "\n\n";
89 } else {
90 print "\r".$content."\n\n";
93 $color = ($color + 1) % scalar(@colors);
95 $cmd = getc(TTY);
97 if ($cmd eq 'q') {
98 last;
99 } elsif ($cmd eq 'n') {
100 $elem = $elem + 1;
101 $elem = $max - 1 if ($elem == $max);
102 }elsif ($cmd eq 'p') {
103 $elem = $elem - 1;
104 $elem = 0 if ($elem < 0);
108 system "stty -cbreak </dev/tty >/dev/tty 2>&1";
109 close(TTY);
111 exit 0;