return years in hash
[webpac2] / bin / isi-download-results.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 our $q = 'AD=Croatia';
7 my $range_size = 500;
8
9 my $dump = @ARGV ? 1 : 0;
10
11 $q = 'TS=psychology AND AD=Croatia';
12
13 use WWW::Mechanize;
14 use Data::Dump qw(dump);
15 use File::Path;
16
17 our $mech = WWW::Mechanize->new(
18         autocheck => 1,
19         cookie_jar => undef,
20 );
21
22 our $step = 0;
23
24 my $dir = '/tmp/isi/';
25 rmtree $dir if -e $dir;
26 mkdir $dir;
27
28 sub save_mech {
29         my ( $mech, $path ) = @_;
30         $step++;
31         my $base_path = sprintf('%s/%04d', $dir,$step);
32         $path ||= $base_path . ( $mech->{ct} =~ m{html}i ? '.html' : '.txt' );
33         $mech->save_content( $path );
34         warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
35         open(my $dump, '>', "$base_path.dump.txt");
36         $mech->dump_all($dump);
37 }
38
39 warn "# get session";
40 $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
41 save_mech $mech;
42
43 sub search {
44         warn "# advanced serach";
45         $mech->follow_link( url_regex => qr/AdvancedSearch/ );
46         save_mech $mech;
47
48         warn "# cookie_jar ", dump $mech->cookie_jar;
49
50         $mech->submit_form(
51                 fields => {
52                         'value(input1)' => $q,
53                 }
54         );
55         save_mech $mech;
56
57         warn "# summary";
58         $mech->follow_link( url_regex => qr/summary/ );
59         save_mech $mech;
60 }
61
62 sub get_results {
63         my $from = 1;
64
65         while ( 1 ) {
66
67                 my $to = $from + $range_size;
68
69                 $mech->submit_form(
70                         form_name => 'summary_output_form',
71                         fields => {
72                                 record_select_type => 'range',
73                                 mark_from => $from,
74                                 mark_to => $to,
75                                 mark_id => 'WOS',
76
77                                 qo_fields => 'fullrecord',
78                                 citedref => 'citedref',
79
80                                 save_options => 'plain_text',
81
82                                 fields => 'Full',
83                                 format => 'save',
84                         },
85                         button => 'save',
86                 );
87                 save_mech $mech;
88
89                 if ( $mech->content =~ m{invalid API call} ) {
90                         $mech->back;
91                         last;
92                 }
93
94                 warn "range $from - $to [$q]\n";
95                 $mech->follow_link( url_regex => qr/save_file/ );
96                 save_mech $mech => "/tmp/isi.$q.$from-$to.txt";
97
98                 $from += $range_size;
99
100                 $mech->back;
101                 $mech->back;
102                 #save_mech $mech;
103
104         }
105
106 }
107
108
109 sub citations {
110         save_mech $mech;
111         warn "# citation report";
112         $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
113         save_mech $mech;
114
115         warn "view citing articles";
116         $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
117         save_mech $mech;
118 }
119
120 sub years {
121         my $years_url = $mech->find_link( text_regex => qr/more options/ );
122         if ( ! $years_url ) {
123                 warn "W: can't find years\n";
124                 return;
125         }
126         $years_url = $years_url->url_abs;
127         warn "## $years_url";
128         if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
129                 warn "W: no ra_name\n";
130                 return;
131         }
132         warn "# refine years (hidden by javascript)";
133         warn "http://apps.isiknowledge.com/RAMore.do?product=WOS&search_mode=TotalCitingArticles&SID=T1o6bChdN9PGP1LN1Nh&qid=3&ra_mode=more&ra_name=PublicationYear&db_id=WOS&viewType=raMore\n$years_url\n";
134         $mech->get( $years_url );
135         save_mech $mech;
136
137         my $html = $mech->content;
138         my $years;
139         while ( $html =~ s{>(\d\d\d\d)\s\((\d+)\)</label.+?value="PublicationYear_}{} ) {
140                 $years->{$1} = $2;
141         }
142         warn "# years ",dump $years;
143         $mech->back;
144         return $years;
145 }
146
147 search;
148 years;
149 get_results;
150
151
152 citations;
153 years;
154 $q .= '.citing';
155 get_results;
156