#!/usr/bin/perl

use warnings;
use strict;

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

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

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

my $step = 1;

sub save_mech {
	my $mech = shift;
	my $path = "/tmp/isi.$step.html";
	write_file $path, $mech->content;
	warn "# step $step ", -s $path;
	$mech->dump_all;
	$step++;
}

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;

my $from = 1;
my $range_size = 10;

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

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

		save_options => 'plain_text',

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

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

