carcassonne has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

I've subscribed our dept. to the Safari online bookshelf. I would like to get regular informations in an automatic way about which books are on bookshelves, for each user, so that it is possible to make a list and perhaps share some (eg. instead of one developer adding the same book as somebody else) to perhaps obtain an optimal use of the service.

My question is, how could Perl and the CPAN help me in doing this ? The task, as I imagine, would be to automatically log on a user account, then go to the bookshelf section and grab all the book titles.

To grab the book titles I could use one of the HTML or XML modules to traverse the page and get to the tags representing the books, but what about the automatic login and the command to go to the bookshelf section ?

Thanks for any comments/suggestions.

Replies are listed 'Best First'.
Re: Getting user stats from Safari
by imp (Priest) on Oct 22, 2006 at 03:15 UTC
    Unfortunately safari doesn't play well with others and uses javascript redirects when loading the login page, and after submitting the login request. They also use somewhat obfuscated javascript to hide their actions in a few places, which hints at them not wanting you to do this.

    I would read over their terms of service and make sure this is not an action that will get your accounts terminated. At a glance it seems ok, but best to be cautious. It might be prudent to ask them, but the answer will likely be "don't do that".

    If not for the above mentioned issues my first inclination is to use WWW::Mechanize to automate the login and navigation, as it combines HTTP, html parsing, form population/submission, and link extraction.

    The links on the safari bookshelf page seem to be a forward slash followed by a numeric ISBN. So "Perl and LWP", with ISBN 0-596-00178-9, is "/0596001789".

    An example of how you could go about solving this problem, if not for the javascript and legality issues, would be something like the following:

    use strict; use warnings; use WWW::Mechanize; use Data::Dumper; my $user = 'foo'; my $pass = 'bar'; my $mech = WWW::Mechanize->new(); my $response = $mech->get('http://safari.oreilly.com/login'); die "Failed to load login page: " + $response->message unless $respon +se->is_success; $mech->form_number(1); $mech->set_visible($user,$pass); $response = $mech->submit(); die "Failed to login: " + $response->message unless $response->is_suc +cess; # TODO - Check the page content here for a failed login message # Find the link to the bookshelf, and follow it $mech->follow_link(url_regex => qr/mybookshelf/); # Get a list of all links, then limit the list to those that are /nume +ric my @links = $mech->links(); my @books = grep {$_->url =~ /^\/\d+$/} @links; for my $book (@books) { my $isbn = $book->url; $isbn =~ s//^\//; printf "ISBN: %s Title: %s\n", $isbn, $book->text; }
    Update - It might be worth looking at safari enterprise offerings.
Re: Getting user stats from Safari
by fmerges (Chaplain) on Oct 22, 2006 at 03:13 UTC