public
Last active

Plot HackerNews polls on favorite and disliked programming languages

  • Download Gist
get_votes.pl
Perl
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
#!/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();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.