#!/usr/bin/perl

use warnings;
use strict;

my $q = 'AD=Croatia';
my $range_size = 500;

my $dump = @ARGV ? 1 : 0;

$q = 'TS=psychology AND AD=Croatia';

use WWW::Mechanize;
use Data::Dump qw/dump/;

our $mech = WWW::Mechanize->new(
	autocheck => 1,
	cookie_jar => undef,
);

our $step = 0;

sub save_mech {
	my ( $mech, $path ) = @_;
	$step++;
	$path ||= sprintf('/tmp/isi.%02d.%s', $step, $mech->{ct} =~ m{html}i ? 'html' : 'txt' );
	$mech->save_content( $path );
	warn "# [$step] $path ", -s $path, " ", $mech->ct;
	$mech->dump_all if $dump;
}

warn "# get session";
$mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
save_mech $mech;

warn "# advanced serach";
$mech->follow_link( url_regex => qr/AdvancedSearch/ );
save_mech $mech;

warn "# cookie_jar ", dump $mech->cookie_jar;

$mech->submit_form(
	fields => {
		'value(input1)' => $q,
	}
);
save_mech $mech;

warn "# summary";
$mech->follow_link( url_regex => qr/summary/ );
save_mech $mech;

sub get_results {
	my $q = shift;
	my $from = 1;

	while ( 1 ) {

		my $to = $from + $range_size;

		$mech->submit_form(
			form_name => 'summary_output_form',
			fields => {
				record_select_type => 'range',
				mark_from => $from,
				mark_to => $to,
				mark_id => 'WOS',

				qo_fields => 'fullrecord',
				citedref => 'citedref',

				save_options => 'plain_text',

				fields => 'Full',
				format => 'save',
			},
			button => 'save',
		);
		save_mech $mech;

		if ( $mech->content =~ m{invalid API call} ) {
			$mech->back;
			return;
		}

		warn "# save_file $from - $to [$q]";
		$mech->follow_link( url_regex => qr/save_file/ );
		save_mech $mech => "/tmp/isi.$q.$from-$to.txt";

		$from += $range_size;

		$mech->back;
		$mech->back;
		#save_mech $mech;

	} # while

}

get_results $q;

save_mech $mech;
warn "# citations";
$mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
save_mech $mech;

$mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
save_mech $mech;

get_results $q . '.citing';

