Josh (desh) wrote,
Josh
desh

Finally, after 3ish months of the service being down, my Ambient Orb is working again. And the program I wrote (well, helped write) for it works.

So now I have a glowing orb in my home that turns red for 12 hours after a Phillies win, and turns blue for 12 hours after a Phillies loss.

Aside from being really awesome, it's now the best way for me to know the result of a Friday night Phillies game before I go to sleep, since I won't use my computer/phone/TV/radio like I will the rest of the week.

It's also pretty cool because I think this is the first time I successfully completed a computer program, written on my own free time, to do something actually useful.


Code by desh, marnanel, and one or two other people who I think might have been crucially and daveman692? But all the crappy parts of the code are mine. Comments welcome.

#!/usr/bin/perl

use strict;
use warnings FATAL=>'all';
use LWP::Simple;
use Date::Calc qw(:all);
use Data::Dumper;
use constant HOURS => 12; # how long we want the orb to glow after a game
$|++;

my $devid = 'XXX-XXX-XXX'; # redacted -- replace with your own

my %colors = (
  'red'    => 0,
  'orange' => 3,
  'yellow' => 6,
  'green'  => 12,
  'teal'   => 18,
  'blue'   => 24,
  'purple' => 30,
  'white'  => 36
);

my %anims = ( # not currently used
  'none'      => 0,
  'slow'      => 2,
  'medium'    => 4,
  'fast'      => 7,
  'crescendo' => 8,
  'heartbeat' => 9
);

my $win_color = $colors{'red'};
my $lose_color = $colors{'teal'};
my $no_color = $colors{'white'};

# get info about what we did to the orb last
my $log_status = open(my $colorlog, "</home/deshin00/orblog.txt");
my $last_time;
my $last_color = 0;
my $last_anim = 0;
my $color;
my $anim;
my $dummy;
if ($log_status) {
  while(<$colorlog>) {
    ($last_time, $last_color, $last_anim, $dummy) = split /\s+/; #only keep last line
  }
}
close $colorlog;

# get lines containing last time, score, and winner (asterisk)
# from http://www.totallyscored.com/rss/team/83
my $score_line = '';
my $time_line = '';
my @feed = split (/\n/, get 'http://www.totallyscored.com/rss/team/83');
my ($seen_channel, $seen_item);
foreach (@feed) {
    last if /<\/item>/;
    $seen_channel = 1 if /<channel>/;
    next unless $seen_channel;
    $seen_item = 1 if /<item>/;
    next unless $seen_item;
    $score_line = $_ if (!$score_line && /<title>/);
    $time_line = $_ if (!$time_line && /<pubDate>/);
}

# get the score from the captured line
my ($phils_score, $opp_score, $phils_asterisk, $opp_asterisk, $post_time) =
    (0, 0, '', '', '');
$score_line =~ m!(\w+(?: \w+)?) (\d+)(\*?) vs\. (\w+(?: \w+)?) (\d+)(\*?)!;
my ($team1, $score1, $star1, $team2, $score2, $star2) =
    ($1, $2, $3, $4, $5, $6);
if ($team1 =~ /Philadelphia/) {
    $phils_score = $score1;
    $opp_score = $score2;
    $phils_asterisk = length $star1;
} elsif ($team2 =~ /Philadelphia/) {
    $phils_score = $score2;
    $opp_score = $score1;
    $phils_asterisk = length $star2;
} else {
    die "Couldn't find the Phillies!\n";
}

# get the time from the captured line
$time_line =~ /\w+, (\d+ \w+ \d+) (\d+):(\d+):(\d+) ([+-]?)(\d+)/;
my ($d1, $d2, $d3) = Decode_Date_EU($1) or die "Couldn't decode date '$1'\n";
$post_time = Date_to_Time($d1, $d2, $d3, $2, $3, $4);
my ($offsign, $offset) = ($5, $6);

$offset *= -1 if $offsign =~ /-/;
$post_time -= ($offset*60*60 / 100); #now in GMT

my $dtnow = Date_to_Time(Today_and_Now(1)); #1 makes it GMT

# determine score and color from previous info
$anim = $anims{'none'};
if ($phils_score > $opp_score || $phils_asterisk) {
    $color = $win_color;
} elsif ($phils_score < $opp_score || $opp_asterisk) {
    $color = $lose_color;
} else { # tie??
    #$color = $no_color;
    die "Couldn't find score, or tie score in:\n$score_line\n";
}
if (($dtnow - $post_time > HOURS *60*60) || ($dtnow < $post_time)) {
    $color = $no_color;
}

# set the orb
if ($color != $last_color || $anim != $last_anim) {
    getprint("http://www.myambient.com:8080/java/my_devices/submitdata.jsp?devID=$devid&color=$color&anim=$anim") or die "couldn't set orb!\n";
    open LOGSTAT, ">>/home/deshin00/orblog.txt" or die "Couldn't log!\n";
    print LOGSTAT time."\t$color\t$anim\t\n";
}
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments