How to Transfer URLs from the Command Line
While Mac OS X includes lukem's ftp client, Ubuntu does not. However, one can make a perl script to fetch a URL is trivial. Indeed, one is included below. Works everywhere perl does... Enjoy.
use warnings;
use strict;
use diagnostics;
use Net::FTP;
use URI;
my $uri = shift @ARGV;
die "Usage: $0 ftp-url\n" if not defined $uri;
my $url = URI->new($uri);
die "need FTP URL to work\n" unless $url->scheme =~ /ftp/;
my $conn = Net::FTP->new($url->host) or die "$!";
$conn -> get($url->path);
$conn -> quit;
How to SMS Yourself Reminders
The attached script puts your 30 boxes calendar and notifies you using Twitter direct messages. It is written in perl. I plugged this into a cronjob and have been using the script about a week. Source code below.
use warnings;
use strict;
use diagnostics;
use WebService::30Boxes::API;
use Date::Manip;
use Env qw/HOME/;
use Net::Twitter;
my %config;
$config{TB_auth} = 'your30boxesAuthCode';
$config{TB_key} = 'your30boxesSecretKey';
$config{TW_user} = yourtwitterusername';
$config{TW_pass} = 'yourtwitterpassword';
my $fmt = '%Y-%m-%d';
my $today = UnixDate(ParseDate('today'), $fmt);
my $boxInstance = WebService::30Boxes::API->new(api_key => "$config{TB_key}");
my $events = $boxInstance->call('events.GetDisplayList', {authorizedUserToken => $config{TB_auth}, end => "$today"});
my $twitter = Net::Twitter->new(username => $config{TW_user}, password => $config{TW_pass});
my %dm;
$dm{user} = $config{TW_user};
foreach my $event ($events->get_eventIds) {
$dm{text} = $events->get_startTime($event)." ". $events->get_title($event);
$twitter->new_direct_messages(\%dm);
}
How to Fix Perl Version Problems
You know me, I'm the Prolific Programmer. So prolific am I, that I often encounter problems with my Swiss Army knife of tools. One of my favourite tools in the knife is Perl. So, it was with great sadness when I found that it was having version problems. Apparently, Compress::Zlib had been updated to 2.008 and the author, Paul Marquees had forgotten to update the other modules. Until he does, you need only comment out the versions from the relevant use lines. For example:
use CGI 3.31; # becomes...
use CGI; # 3.31
How to Get Around Google Reader's Shortcomings
Remember I was complaining of my shared items disappearing? Well, I wrote myself a script to get them back and it's pasted after the flip.
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use XML::Feed;
my $feed = XML::Feed->parse(URI->new('http://www.google.com/reader/public/atom/user/08730762945348639336/state/com.google/broadcast'));
my $cgi = new CGI;
print $cgi->header, $cgi->start_html;
print "- <a href="".$_->link."">",$_->title."</a><br/>" foreach $feed->entries;
print $cgi->end_html;
How to Grab Media off the Internet
You're familiar with the streaming media, right? It's the same as a download. Indeed, one can actually capture the stuff in its native format. After the flip, I've included perl code to do just that! For those too lazy to cut and paste, it's at the usual place as well.
use LWP::Simple;
use HTML::TreeBuilder;
use File::Temp;
foreach my $page (@ARGV) {
my $pagedata = get $page;
my $tree = HTML::TreeBuilder->new;
$tree->parse($pagedata);
$tree->eof;
my @base = $tree->find('base');
my $base_url = '';
$base_url = $base[0]->{'href'} if @base;
my @embeds = $tree->find('embed');
foreach my $embed (@embeds) {
my $target = $embed->{'src'};
my $newurl = "$base_url$target";
my @newurl_parts = split (/\//);
my $filename = $newurl_parts[-1];
my ($fh, $fname) = tempfile("$filename");
print $fh get $newurl;
close $fh;
print STDERR "$filename saved as $fname";
}
$tree->delete;
}
How to Improve GMail's Spam Filter
GoogleMail has a spam filter second-to-none, but it could be better. The one failure that's a very low-hanging fruit (at least from my view) is language identification. Indeed, I'm including code below to identify a given message's language and give it a confidence:
#!/usr/bin/env perl
use warnings;
use strict;
use diagnostics;
use Lingua::Identify qw/:language_identification/;
use Mail::POP3Client;
my $pop = new Mail::POP3Client(USER => "$USER",
PASSWORD => "$PASSWORD",
HOST => 'pop.gmail.com',
PORT => 995,
USESSL => 'true'
);
$pop->Connect;
my $count = $pop->Count;
my $debug = 1;
for (my $counter = 1; $counter != $count; $counter++) {
my ($language, $prob);
while (my $text = $pop->Body($counter)) {
print $text if defined $debug;
($language, $prob) = langof($text);
}
print "Message $counter is $language, $prob probability.\n";
}
$pop->Close;
So, basically, you analyse the sent mail as a control group to determine which languages the user knows. Then you store these languages and anything that doesn't match these can be assumed to be spam. Then you just apply the standard bayesian filter.
How to Solve JTV's Extra Credit Problems Using Perl
Though I'm not desperately looking for a job, I enjoy these sort of problems. Here, I have chosen to solve it using perl. Anyhow, here's the result:$expr = '([0-9]+\s)([-+*/]\s)([0-9]+)'; s/$expr/\2\1\3/g;It doesn't work for complex, nested cases yet, but for the simple ones, it chews and spits out what's expected. I suppose to handle order of operations, you'd need to order the application of the substitutions.
