Tuesday, August 28, 2007

Using Perl's WWW::Myspace

For getting information on your Myspace page and other people's, posting comments and updates to your profile, or anything inbox related (and some other stuff)... WWW::Myspace is a good module to use.

In this article, I'll show some examples of how to use some of the functionality of this module. Pretty handy... especially if you just want the information without dealing with the horrible interface that Myspace has (and people's themes and playing songs by default and other annoyances).

Get your friends



Get a list of all your friends, maybe handy, maybe not... this is just an example so it doesn't have to be useful! :)

Here is how one would get all their friends and what times they last logged in:

#!/opt/local/bin/perl
#
use strict;
use warnings;

use WWW::Myspace;

$| = 1;

my $username = $ARGV[0];
my $password = $ARGV[1];
my @friends;

print "Username: $username\tPassword: $password\n";

print "Logging in... ";
my $myspace = WWW::Myspace->new( $username, $password );
print "done\n";
if ( ! $myspace->logged_in ) {
print "Unable to login: " . $myspace->error . "\n";
}

# Get list of your friends
print "Getting friends... ";
@friends = $myspace->get_friends();
print "done\n";

# Present list all nice-like
foreach my $friend (@friends) {
eval {
print "friend id.: $friend\n";

my $friend_name = $myspace->friend_user_name($friend);
print "name......: $friend_name\n";

my $last_login = $myspace->last_login_ymd($friend);
print "last login: $last_login\n\n";
};
if ($@) {
# Usually an error when can't view profile due to "maintenance", yet WWW::Myspace breaks
print "Unable to complete request: $@\n\n";
}
}


Get your inbox



This is pretty simple, get all messages from your inbox:

#!/opt/local/bin/perl
#
use strict;
use warnings;

use WWW::Myspace;

$| = 1;

my $username = $ARGV[0];
my $password = $ARGV[1];
my @friends;

print "Username: $username\tPassword: $password\n";

print "Logging in... ";
my $myspace = WWW::Myspace->new( $username, $password );
print "done\n";
if ( ! $myspace->logged_in ) {
print "Unable to login: " . $myspace->error . "\n";
}


print "Getting inbox...\n";
my $inbox = $myspace->inbox;

foreach my $message (@{$inbox}) {
print "Sender: " . $message->{sender} . "\n";
print "Status: " . $message->{status} . "\n";
print "messageID: " . $message->{message_id} . "\n";
print "Subject: " . $message->{subject} . "\n\n";

# Note, to read each message do
#my $message_hashref = $myspace->read_message($message->{message_id});
#print "From: $message_hashref->{'from'}\n"; # Friend ID of sender
#print "Date: $message_hashref->{'date'}\n"; # Date (as formatted on Myspace)
#print "Subject: $message_hashref->{'subject'}\n";
#print "Body: $message_hashref->{'body'}\n\n"; # Message body
}


Note that it will print out all messages you have. The message details part of the example is commented out due to being very spammy (you can uncomment it out if you wish).

Get the last 5 comments of all your friends



This just gets your friends list like before, then goes over each to get their comments on their Myspace page. It reports back the friend name, id and time they posted the comment.


#!/opt/local/bin/perl
#
use strict;
use warnings;

use WWW::Myspace;

$| = 1;

my $username = $ARGV[0];
my $password = $ARGV[1];
my @friends;

print "Username: $username\tPassword: $password\n";

print "Logging in... ";
my $myspace = WWW::Myspace->new( $username, $password );
print "done\n";
if ( ! $myspace->logged_in ) {
print "Unable to login: " . $myspace->error . "\n";
}

# Get list of your friends
print "Getting friends... ";
@friends = $myspace->get_friends();
print "done\n";

my %friend_mapping; # key = id, value = name

# Present list all nice-like
foreach my $friend (@friends) {
eval {
print "friend id.: $friend\n";

my $friend_name = $myspace->friend_user_name($friend);
print "name......: $friend_name\n";
$friend_mapping{$friend} = $friend_name;

print "Getting last 5 comments.\n";

my $comments = $myspace->get_comments($friend);
my $max_comments_display = 5;
do {
my $comment = shift @{$comments};
my $friend_name_comment = "";
if (exists $friend_mapping{$comment->{sender}}) {
$friend_name_comment = $friend_mapping{$comment->{sender}};
} else {
eval { $friend_name_comment = $myspace->friend_user_name($comment->{sender})};
if ($friend_name_comment) {
$friend_mapping{$comment->{sender}} = $friend_name_comment;
}
}
if ($friend_name_comment) {
print "- $friend_name_comment ($comment->{sender}) @ $comment->{date}\n";
} else {
print "- N/A ($comment->{sender}) @ $comment->{date}\n";
}
#print "$comment->{comment}\n"; # The actual comment
$max_comments_display--;
} while ($max_comments_display > 0);


};
if ($@) {
print "Unable to complete request: $@\n";
}
}


Pretty simple, eh? I hope you explore the CPAN page yourself for more functionality, as I've only covered a small part.

A handy little module!

4 comments:

Anonymous said...

is there a beginning tutorial of how i would make use of this info? Like a complete noob's guide or something? I like the possibility of checking inbox and other stuff without having to use myspace's interface.

Christopher Humphries said...

Hrm, I thought this was a newbie tutorial. I apologize if I jumped the gun on that.

I am writing WWW::Myspace::Lite that is easier to use and has only read-only functionality. Should be more user-friendly and faster. Look for it on CPAN in a couple weeks or so.

Anonymous said...

Can this module approve friend requests?

Christopher Humphries said...

I am not sure if it can approve friend requests.

I only really tested the read-only abilities, as doing otherwise would probably get your account shutoff, as noted in the README for the module.

If it is possible, I'd recommend doing it at your own risk.