#!/usr/bin/perl
#
# Simple module to interface to http://www.skritter.com/
#
# $Id: skritter.pm,v 0.3 2009/01/08 15:29:54 magnus Exp $
#
# (c) Magnus Bodin, magnus@bodin.org, http://x42.com/
# Original BSD-license applies
#

package skritter;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use WWW::Mechanize;

require Exporter;
require AutoLoader;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(

);
my $VERSION = sprintf("%d.%02d", q$Revision: 0.3 $ =~ /(\d+)\.(\d+)/);
$skritter::VERSION=$VERSION;


my $LOGIN_URI = 'https://skrit.appspot.com/login';
my $HOME_URI = 'http://www.skritter.com/home';
my $AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)';

my $STATBASE = 'http://www.skritter.com/vocabulary/getlist';
my $DEFAULT_STATTYPE = 'next%20review';
my $DEFAULT_STATCLASS = 'char';

my $STAT_PAGESIZE = 20;
my $CHARSTAT = 'char';
my $TONESTAT = 'tone';

my $PROGRESS = "http://www.skritter.com/progress";

sub login(@)
{
	my ($self, %p) = @_;
	unless ($self->{loggedin} == 1)
	{
		$self->{loggedin} = 0;
		$self->{b}->get($LOGIN_URI);
		$self->{b}->field('username', $self->{username});
		$self->{b}->field('password', $self->{password});
		$self->{b}->submit();
		$self->{loggedin} = ($self->{b}->base() eq $HOME_URI) ? 1 : 0;
	}

	return $self->{loggedin};
}


sub stat
{
  my ($self, %p) = @_;
	my $result;

	my $class = $p{class} || $DEFAULT_STATCLASS;
	my $type = $p{type} || $DEFAULT_STATTYPE;
	
	return undef unless $self->login();
	my $page = 0;
	$self->{b}->get( "$STATBASE/$class/$type/$page" );
	while (($self->{b}->status() == 200) && (length($self->{b}->content()) > 0))
	{
		$result .= $self->{b}->content(),"\n";
		$page += $STAT_PAGESIZE;
	  $self->{b}->get( "$STATBASE/$class/$type/$page" );
	}

	return $result;
}

sub progress_html
{
  my ($self, %p) = @_;
	my $result;

  return undef unless $self->login();
	$self->{b}->get( $PROGRESS );
	$result .= $self->{b}->content(),"\n";

	return $result;
}

sub new
{
    my ($proto, %p) = @_;
		my $class=ref($proto) || $proto;
		my $self = bless {}, $class;
		$self->{username} = $p{username};
		$self->{password} = $p{password};
		$self->{useragent} = $p{useragent} || $AGENT;
		$self->{b} = WWW::Mechanize->new( agent => $self->{useragent} );
		$self->{loggedin} = 0;

		return $self;
}


1;
__END__

=head1 NAME

skritter - Perl extension to get user chars, progress and such from skritter.com

=head1 SYNOPSIS

use skritter;
$charstat = skritter->stat();

=head1 DESCRIPTION

Simple and lightweight module for retrieving things that needs to be retrieved.

=head1 METHODS

It exports these methods:

  new( 'username'=>'<username>', 'password'=>'<password>')
	   - allocates new object and sets credentials. (Does not login)
		 <username> is your skritter username
		 <password> is your skritter password
		 We do not deal with OpenID-logins here.

  stat( 'class'=>'<class>', 'type'=>'<type>' )
	   Retrieving all pages of '<class>' stats (char or tone)
		 ordered on <type>. Default class is 'char' and default
		 type is 'next%20review'.
		 This result is tab separated and is the same data that is
		 used on the vocabulary page; 
		   http://www.skritter.com/vocabulary/viewer

	progress_html()
	   Just returning the raw html of the progress page;
		 http://www.skritter.com/progress
     
		 Future module versions should cache this and provide
		 detailed progress methods, like extract the figures and google chart-urls.

=head1 AUTHOR

Magnus Bodin, magnus@bodin.org

=head1 SEE ALSO

http://www.skritter.com

=cut

