Using perl to scrape and twitter the ISS sightings.
I have just got one of those electricity monitors and it has a data connection. I am going to write an app to allow it to tweet my power consumption but I’ve got to wait for the data cable so I thought I’d take some time to write a perl script in preparation. I’ve never coded in perl before so its a nice little project.
I decided to code up a script that gets the sightings for the ISS from heaves-above and tweets them. Nothing amazing but it will use a lot of the code I will need to parse the output from the energy monitor and then tweet it.
Below is my code, you’ll need to satisfy the dependencies using CPAN.
UPDATE: Thanks to osfameron, I’ve updated the script to be a little more strict and to use a config file. You need to create a config file in /etc/twitteriss.conf with the following:
twitter_username = someusername twitter_password = somepassword location_url = the_url_from_heavens_above
#!/usr/bin/perl use strict; use warnings; use Net::Twitter; use LWP::Simple; use HTML::TableContentParser; use HTML::Strip; use POSIX qw/strftime/; use Config::General; my %config = Config::General->new("/etc/twitteriss.conf")->getall; die "Could not read config file" unless defined $config{location_url}; my $twitter = Net::Twitter->new( traits => [qw/API::REST/], username => $config{twitter_username}, password => $config{twitter_password} ); my $url = $config{location_url}; my $content = get $url; die "Couldn’t get $url" unless defined $content; my $tcp = HTML::TableContentParser->new; my $tables = $tcp->parse($content); my $hs = HTML::Strip->new(); foreach my $table (@$tables){ if (defined($table->{id}) && $table->{id} eq "ctl00_ContentPlaceHolder1_tblPasses"){ my (undef, undef, @rows) = @{$table->{rows}}; foreach my $row (@rows){ my $sightingdate = $hs->parse($row->{cells}[0]{data}); if ($sightingdate eq strftime(‘%d %b’,localtime)){ #we have a sighting today! my @data = map $_->{data}, @{$row->{cells}}; my ($date, $mag, $starttime, $startel, $startdir, $maxtime, $maxel, $maxdir, $endtime, $endel, $enddir) = @data; my $tweetline = "ISS today: Mag $mag Starts: $starttime $startdir" . chr(176) . " $startel Max: $maxtime $maxel" . chr(176) . " $maxdir Ends: $endtime $endel" . chr(176) . " $enddir"; $twitter->update($tweetline); } } } }
Like I said, its my first perl script so I’m sure there are better ways but its a good start and it works! Comments on a postcard to the usual address.
Jay.
Thursday, November 26th, 2009
![[Get Ubuntu]](/img/ubuntu_button_80x15.png)


![[Help For Heroes]](/img/h4h_donations_Small.jpg)