NAME

WWW::Myspace - Access MySpace.com profile information from Perl

VERSION

Version 0.10

SYNOPSIS

Myspace.pm provides methods to access myspace.com accounts and functions automatically. It provides a simple interface for scripts to log in, access lists of friends, scan user's profiles, retreive profile data, send messages, and post comments.

use Myspace;
my $myspace = Myspace->new ($account, $password);
    OR
my $myspace = new Myspace; # Prompts for email and password

my ( @friends ) = $myspace->get_friends();

Myspace works by interacting with the site through a UserAgent object, using HTTP::Request::Form to process forms. Since by nature web sites are dynamic, if you find that some interaction with the site breaks, check for a new version of this module (or if you go source diving, submit a patch).

Methods

new( $account, $password )

new( )

If called without the optional account and password, the new method looks in a user-specific preferences file in the user's home directory for the last-used account and password. It prompts for the username and password with which to log in, providing the last-used data (from the preferences file) as defaults.

Once the account and password have been retreived, the new method automatically invokes the "site_login" method and returns a new Myspace object reference. The new object already contains the content of the user's "home" page, the user's friend ID, and a UserAgent object used internally as the "browser" that is used by all methods in the Myspace class.

EXAMPLES
    use Myspace;
    my $myspace = Myspace->new();
    
    # Print my friend ID
    print $myspace->my_friend_id;
    
    # Print the contents of the home page
    print $myspace->current_page->content;
    
    # Print all my friends
    @friend_ids = $myspace->get_friends;
    foreach $id ( @friend_ids ) {
        print "${VIEW_PROFILE_URL}${id}\n"
    }
    
    # How many friends do we have? (Note: we don't include Tom
    # because he's everybody's friend and we don't want to be
    # spamming him with comments and such).
    print @friend_ids . " friends (not incl Tom)\n";

my_friend_id

Returns the friendID of the user you're logged in as.

EXAMPLE

print $myspace->my_friend_id;

account_name

Returns the account name (email address) under which you're logged in. Note that the account anem is retreived from the user or from your program depending on how you called the "new" method.

EXAMPLE

The following would prompt the user for their login information, then print out the account name:

use Myspace;
my $myspace = new Myspace;

print $myspace->account_name;

current_page

Returns a reference to an HTTP::Response object that contains the last page retreived by the Myspace object. All methods (i.e. get_page, post_comment, get_profile, etc) set this value.

EXAMPLE

The following will print the content of the user's profile page:

use Myspace;
my $myspace = new Myspace;

print $myspace->current_page->content;

Returns the path to the file we're using to store cookies. Defaults to $ENV{'HOME'}/.cookies.txt. If called with a filename, sets cookie_jar to that path.

If using this from a CGI script, you should set cookie_jar.

cache_dir

WWW::Myspace stores the last account/password used in a cache file for convenience if the user's entering it. Other modules store other cache data as well. cache_dir sets or returns the directory in which we should store cache data. Defaults to $ENV{'HOME'}/.www-myspace.

If using this from a CGI script, you will need to provide the account and password in the "new" method call, so cache_dir will not be used.

cache_file

Sets or returns the name of the file into which the login cache data is stored. Defaults to login_cache.

If using this from a CGI script, you will need to provide the account and password in the "new" method call, so cache_file will not be used.

remove_cache

Remove the login cache file. Call this after creating the object if you don't want the login data stored:

my $myspace = new Myspace( qw( myaccount, mypassword ) );
$myspace->remove_cache;

get_friends

Returns an array that is a list of your friend's friend IDs.

@friends = $myspace->get_friends;

friends_who_emailed

Returns an array containing the friend IDs of all friends with messages in your inbox (mail). Note that this only tells you who you have mail from, not how many messages, nor does it contain any method to link to those messages. This is primarily designed to aid in auto-responding programs that want to not contact (comment or email) people who have sent messages as someone will probably need to attend to them personally.

@friends = $myspace->friends_who_emailed;

friends_in_group( group_id )

Returns an array containing the friend IDs of all friends (by Myspace's definition) in the group identified by group_id.

Example:

my @hilary_fans = $myspace->friends_in_group( 100011592 );

@hilary_fans now contains the friend IDs of everyone in the HIlary
Duff Fan Club group (group ID 100011592 ).

To get the group ID, go to the group page in Myspace and look at the URL: http://groups.myspace.com/index.cfm?fuseaction=groups.viewMembers&GroupID=100011592&adTopicId=&page=3

The group ID is the number after "GroupID=".

get_page( $url )

get_page returns a referece to a HTTP::Response object that contains the web page specified by $url.

Use this method if you need to get a page that's not available via some other method. You could include the URL to a picture page for example then search that page for friendIDs using get_friends_on_page.

get_page try until it gets the page. Note that this routine will KEEP TRYING FOREVER, so make sure you have the right URL!!! This is designed to get past network problems and such.

EXAMPLE

The following displays the HTML source of MySpace.com's home
page.
my $res=get_page( "http://www.myspace.com/" );

print $res->content;

get_friends_on_page( $friends_page );

This routine takes the SOURCE CODE of an HTML page and returns an array that contains a list of friendIDs for which there are profile links on the page. This routine is used internally by "get_friends" to scan each of the user's "View my friends" pages.

Notes: - It does not return our friendID. - friendIDs are filtered so they are unique (i.e. no duplicates). - We filter out 6229, Tom's ID.

EXAMPLE:

List the friendIDs mentioned on Tom's profile:

use Myspace;
my $myspace = new Myspace;

$res = $myspace->get_profile( 6229 );

@friends = $myspace->get_friends_on_page( $res->content );
print "These people have left comments or have links on Tom's page:\n";
foreach $id ( @friends ) {
    print "$id\n";
}

get_profile( $friend_id )

Returns a reference to a HTTP::Response object that contains the profile page for $friend_id.

The following displays the HTML source code of the friend's
profile identified by "$friend_id":

my $res = $myspace->get_profile( $friend_id );

print $res->content;

post_comment( $friend_id, $message )

Post $message as a comment for the friend identified by $friend_id. The routine confirms success or failure by reading the resulting page. It returns a status string as follows:

P: Posted. This means the post went through and we read the
			phrase we needed from the resulting page.
PA: Posted, requires approval
FN: Failed Network. The POST returned a bad status.
FC: Failed. A CAPTCHA response was requested.
FF: Failed. Got "You must be someone's friend to post a comment" error.
F: Failed. The post went through, but we didn't get the phrase
			we needed to verify that it was ok.

Warning: It is possible for the status code to return a false "Failed" if the form post is successful but the resulting page fails to load.

EXAMPLE: use Myspace; my $myspace = new Myspace;

foreach $id ( $myspace->friends_who_emailed ) {
    $status = $myspace->post_comment( $id, "Thanks for the message!" )
}

comment_friends( $message ) =head2 comment_friends( $message, { 'ignore_dup' => 1 } )

This convenience method sends the message in $message to all of your friends. (Since you can only comment friends, it sends the comment to everyone you can).

If called in the second form, it uses the "already_commented" method to determine if a comment has already been left on each friend's page and skips the page if it detects a previous comment.

Note that you'll probably want to use the Myspace::Comment module as if the process is interrupted (which is likely), this routine doesn't offer a way to recover. The Myspace::Comment module logs where comments have been left, scans for previous comments we've left on the user's page, and can stop after a specified number of posts to avoid triggering security measures. It can also be re-run without leaving duplicate comments.

EXAMPLE: A simple script to leave a comment saying "Merry Christmas" to everyone on your friends list:

use Myspace;
my $myspace = new Myspace;
$myspace->comment_friends( "Merry Christmas!" );

already_commented

Returns true if there is a link to our profile on "$friend_id"'s page. (If we've left a comment, there'll be a link).

Note that if you're friends with this person and they have another link to your page on their page, this will return a "false" true.

EXAMPLE

  my Myspace;
  my $myspace = new Myspace;
  
  foreach $friend_id ( $myspace->get_friends ) {
	  unless ( $myspace->already_commented( $friend_id ) {
	  	$myspace->post_comment(
	  		$friend_id,
	  		"Hi, I haven't commented you before!"
	  	)
	  }
  }

send_message( $friend_id, $subject, $message )

Send a message to the user identified by $friend_id.

Returns a status code:

P: Posted. Verified by HTTP response code and reading a regexp
	from the resulting page saying the message was sent.
FC: Failed. A CAPTCHA response was requested.
FN: Failed. The POST returned an unsuccessful HTTP response code.
F:  Failed. Post went through, but we didn't see the regexp on the
	resulting page (message may or may not have been sent).

approve_friend_requests

Looks for any new friend requests and approves them. Returns a list of friendIDs that were approved.

EXAMPLE

# Approve any friend requests
@friends_added = $myspace->approve_friend_requests;

# Print the number of friends added and their friend IDs.
print "Added " . @friends_added . " friends: @friends_added.";

# Leave each new friend a thank you comment.
foreach $id ( @friends_added ) {
  $myspoace->post_comment( $id, "Thanks for adding me!" );
}

Run it as a cron job. :)

submit_form( $url, $form_no, $button, $fields_ref )

This powerful little method reads the web page specified by $url, finds the form specified by $form_no, fills in the values specified in $fields_ref, and clicks the button named "$button".

$url can either be a text string to a URL, or a reference to an HTTP::Response object that contains the source of the page that contains the form.

$form_no is used to numerically identify the form on the page. It's a simple counter starting from 0. If there are 3 forms on the page and you want to fill in and submit the second form, set $form_no to "1". For the first form, use "0".

$button is the name or type of button to submit. This will frequently be "submit", but if they've named the button something clever like "Submit22" (as MySpace does in their login form), then you may have to use that.

EXAMPLE

This is how post_comment actually posts the comment:

# Submit the comment to $friend_id's page
$self->submit_form( "${VIEW_COMMENT_FORM}${friend_id}", 1, "submit",
					{ 'f_comments' => "$message" }
				);

# Confirm it
$self->submit_form( $self->{current_page}, 1, "submit", {} );

The comment form is a 2-step process. The first command gets the form and fills it in, then posts it. Myspace then returns the HTML display of the form with a "Post Comment" button. So we just need to click that button ("Post Comment" is the button's "value", but its type is "submit". You could probably use either value. See the "press" method in "perldoc HTTP::Request::Form" for more info). We send that confirmation page to submit_form as a reference to the page returned by the first post.

AUTHOR

Grant Grueninger, <grantg at cpan.org>

KNOWN ISSUES

-

get_friends method does not work if user has > 40 friends (bug introduced by change in myspace site).

-

post_comment dies if it is told to post to a friendID that is not a friend of the logged-in user. (Myspace displays an error instead of a form).

BUGS

Please report any bugs or feature requests, or send any patches, to bug-www-myspace at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Myspace. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

Bug reports are nice, patches are nicer.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc WWW::Myspace

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2005 Grant Grueninger, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.