Skip to content

Instantly share code, notes, and snippets.

@attractivechaos
Created March 24, 2012 17:15
Show Gist options
  • Save attractivechaos/2185232 to your computer and use it in GitHub Desktop.
Save attractivechaos/2185232 to your computer and use it in GitHub Desktop.
Plot HackerNews polls on favorite and disliked programming languages
#!/usr/bin/env perl
# This script collects voting from HackerNews and outputs a plot votes.eps
# You need to have gnuplot installed for plotting.
use strict;
use warnings;
use IO::Socket::INET;
sub http_get {
my ($host, $path, $proxy) = @_;
my $server = ($proxy)? $proxy : $host;
$server .= ":80" unless ($server =~ /:\d+$/); # port 80 by default
my $url = ($proxy)? "http://$host$path" : $path;
my $fh = IO::Socket::INET->new($server) || die("Fail to connect server: $server.\n");
print $fh join("\015\012", "GET $url HTTP/1.0", "Host: $host", "", "");
return $fh;
}
sub get_poll {
my ($host, $path, $hash) = @_;
my $fh = &http_get($host, $path);
while (<$fh>) {
while (/<font color=#000000>([^<]+)<\/font><\/div><\/td><\/tr><tr><td><\/td><td class="default"><span class="comhead"><span id=score_\d+>(\d+) points</g) {
next if ($1 eq 'Other');
my ($lang, $cnt) = ($1, $2);
$lang =~ s/\s/-/;
push(@{$hash->{$lang}}, $cnt);
}
}
close($fh);
}
sub main {
my (%hash, @a, $fh);
&get_poll('news.ycombinator.com', '/item?id=3746692', \%hash);
&get_poll('news.ycombinator.com', '/item?id=3748961', \%hash);
for (keys %hash) {
if (@{$hash{$_}} == 2) {
push(@a, [$_, @{$hash{$_}}]);
}
}
open($fh, ">votes.txt") || die;
@a = sort{($b->[1]+$b->[2])<=>($a->[1]+$a->[2])} @a;
for (@a) {
print $fh join("\t", @{$_}), "\n";
}
close($fh);
open($fh, ">votes-alt.txt") || die;
@a = sort{$b->[1]/($b->[1]+$b->[2])<=>$a->[1]/($a->[1]+$a->[2])} @a;
for (@a) {
print $fh join("\t", @{$_}), "\n";
}
close($fh);
open($fh, "| gnuplot") || die;
my $date = gmtime;
print $fh qq[
set style line 1 lt 1 lc rgb "#FF0000" lw 1;
set style line 2 pt 4 lt 1 lc rgb "#00C000" lw 1;
set style line 3 lt 1 lc rgb "#0080FF" lw 1;
set t po eps co "Helvetica,13"
set out "votes.eps"
set size 1, 2
set multiplot
set boxwidth 0.75 absolute
set key font ",16"
set key top right vert spac 1.5 wid -3
set style fill solid 1.00
set style histogram rowstacked title offset character 0, 0, 0
set style data histograms
set xtics border in scale 0,0 nomirror rotate by -45 offset character 0, 0, 0
set xtics norangelimit font ",13"
set xtics ()
set mytics 5
set ylab "Number of votes" font ",16"
set ytics nomirror
set yran [0:3200]
set y2ran [0:128]
set y2tics 0, 20, 100
set my2tics 2
set title "HackerNews polls on favorite/disliked programming languages ($date)" font ",16"
f(x)=100
];
print $fh q[
set origin 0, 1
set size 1,1
plot f(x) t '' lt 3 lc rgb '#CCCCCC' axis x1y2, "votes.txt" u 2:xtic(1) t 'Favorite (HN:3746692)', '' u 3 ls 3 t 'Disliked (HN:3748961)', "<awk '{print $1,$2/($2+$3)*100}' votes.txt" u 2:xtic(1) t 'Percent favorite' ls 2 axis x1y2 w lp
set origin 0, 0
set title "Sorted by percent favorite"
set size 1,1
plot f(x) t '' lt 3 lc rgb '#CCCCCC' axis x1y2, "votes-alt.txt" u 2:xtic(1) t 'Favorite (HN:3746692)', '' u 3 ls 3 t 'Disliked (HN:3748961)', "<awk '{print $1,$2/($2+$3)*100}' votes-alt.txt" u 2:xtic(1) t 'Percent favorite' ls 2 axis x1y2 w lp
set nomultiplot
];
close($fh);
}
&main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment