Skip to content

Instantly share code, notes, and snippets.

@zmughal
Created October 16, 2012 03:22
Show Gist options
  • Save zmughal/3897088 to your computer and use it in GitHub Desktop.
Save zmughal/3897088 to your computer and use it in GitHub Desktop.
Graph Dropbox spacerace for UH
set environment variables
DB_USER = your e-mail for dropbox
DB_PASS = your password for dropbox
UP = where to upload the file via SCP.
watch -n 30 sh plot.sh
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
use File::Slurp;
use DateTime;
use Try::Tiny;
use utf8 qw(:all);
die "No login information provided" unless $ENV{DB_USER} and $ENV{DB_PASS};
mkdir('data');
my $mech = WWW::Mechanize->new();
$mech->agent('Mozilla/5.0');
while(1) {
my $dt = DateTime->now;
print "Getting URL: $dt\n";
try {
$mech->get("http://www.dropbox.com/spacerace");
} catch {
warn "$_";
};
if($mech->response->decoded_content =~ /login_email/) {
login($mech);
#use HTML::Display; display($mech->content);
next;
}
#print $mech->content;
my $data = $mech->response->decoded_content;
utf8::encode($data);
next unless $data =~ /rocket-points/;
write_file("data/$dt", $data);
sleep 30;
}
sub login {
my $mech = shift;
my $response;
try {
$response = $mech->submit_form( with_fields => { login_email => $ENV{DB_USER}, login_password => $ENV{DB_PASS} });
} catch {
die "$_";
};
die "Could not login" unless $response->code == 200;
#my $form = $mech->form_with_fields( login_email => $ENV{DB_EMAIL}, login_password => $ENV{DB_PASS} );
#my $form = $mech->form_id( 'login_form_partial_form' );
#my @inputs = $form->inputs;
#use DDP; p @inputs;
}
DATA_FILE="points.dat"
DONE_DATA="$DATA_FILE.DONE"
touch "$DATA_FILE"
echo "00T00:00:00" > "$DONE_DATA"
cut -d, -f1 < "$DATA_FILE" >> "$DONE_DATA"
LAST=`tail -1 "$DONE_DATA"`
find data -type f -exec perl -e '$ARGV[0] =~ s,data/\d{4}-\d{2}-,,; exit(($ARGV[0] cmp $ARGV[1]) <= 0)' {} "$LAST" \; -print \
| xargs grep rocket-points \
| sed 's,data/,,g' \
| sed 's,:<div.*rocket-points" class="points">\([^<]*\).*,\,\1,' \
| grep -v div \
| sort \
| sed 's,[[:digit:]]\{4\}-[[:digit:]]\{2\}-,,g' >> "$DATA_FILE"
cat "$DATA_FILE" \
| gnuplot -p -e \
"set terminal png;
set output 'dropbox_spacerace.png';
set datafile separator ',';
set key left top;
set xdata time;
set timefmt '%dT%H:%M:%S';
set format x '%H:%M:%S';
set bmargin 5;
set rmargin 5;
set xtics rotate by -45 out offset 0,0 nomirror;
set title '`date`';
plot '-' using 1:2 with lines title 'Dropbox UH'"
#set format x \"%b %d\\n%Y\";
# http://psy.swansea.ac.uk/staff/Carter/gnuplot/gnuplot_time.htm
# set xdata time; set timefmt '%Y-%m-%dT%T';
# plot '-' using 1:2 with impulses linewidth 10 title 'Dropbox UH'"
# | sed 's,[T],,g'
scp dropbox_spacerace.png $UP
# | sed 's,[[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}T,,g' \
# grep -r rocket-points data/ \
# | grep -v -f "$DONE_DATA" \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment