/[webpac2]/trunk/bin/isi-download-results.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/isi-download-results.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1282 by dpavlin, Fri Sep 18 17:46:04 2009 UTC revision 1294 by dpavlin, Sat Sep 19 22:54:45 2009 UTC
# Line 3  Line 3 
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6  my $q = 'TS=psychology AND AD=Croatia';  # Advanced search syntax:
7  my $range_size = 50;  # http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html
8    
9  my $dump = @ARGV ? 1 : 0;  our $q = 'AD=Croatia';
10    my $range_size = 500;
11  $q = 'AD=Croatia';  my $overlap    = 3; # between previous and this range
12  $range_size = 500;  
13    my $max_cites = 5000; # ISI limit to get cites
14    
15    if ( 0 ) {
16            $q = 'TS=psychology AND AD=Croatia';
17            $range_size = 50;
18            $overlap    = 0;
19            $max_cites  = 50;
20    }
21    
22  use WWW::Mechanize;  use WWW::Mechanize;
23  use Data::Dump qw/dump/;  use Data::Dump qw(dump);
24    use File::Path;
25    
26  my $mech = WWW::Mechanize->new(  our $mech = WWW::Mechanize->new(
27          autocheck => 1,          autocheck => 1,
28          cookie_jar => undef,          cookie_jar => undef,
29  );  );
30    
31  our $step = 0;  our $step = 0;
32    our @ranges;
33    
34    my $dir = '/tmp/isi/';
35    rmtree $dir if -e $dir;
36    mkdir $dir;
37    
38  sub save_mech {  sub save_mech {
39          my ( $mech, $path ) = @_;          my $path = shift;
40          $step++;          $step++;
41          $path ||= sprintf('/tmp/isi.%02d.%s', $step, $mech->{ct} =~ m{html}i ? 'html' : 'txt' );          my $base_path = sprintf('%s/%04d', $dir,$step);
42            $path ||= $base_path;
43            $path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt';
44          $mech->save_content( $path );          $mech->save_content( $path );
45          warn "# [$step] $path ", -s $path, " ", $mech->ct;          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
46          $mech->dump_all if $dump;          open(my $dump, '>', "$base_path.dump.txt");
47            $mech->dump_all($dump);
48  }  }
49    
50  warn "# get session";  warn "# get session";
51  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
52  save_mech $mech;  save_mech;
53    
54  warn "# advanced serach";  sub search {
55  $mech->follow_link( url_regex => qr/AdvancedSearch/ );          warn "# advanced serach";
56  save_mech $mech;          $mech->follow_link( url_regex => qr/AdvancedSearch/ );
57            save_mech;
58    
59  warn "# cookie_jar ", dump $mech->cookie_jar;          warn "# cookie_jar ", dump $mech->cookie_jar;
60    
61  $mech->submit_form(          my $q_this = $q;
62          fields => {  
63                  'value(input1)' => $q,          if ( @ranges ) {
64                    $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
65          }          }
 );  
 save_mech $mech;  
66    
67  warn "# summary";          warn "# submit_form search: $q_this\n";
68  $mech->follow_link( url_regex => qr/summary/ );          $mech->submit_form(
69  save_mech $mech;                  fields => {
70                            'value(input1)' => $q_this,
71                    },
72            );
73            save_mech;
74    
75  my $from = 1;          warn "# summary";
76            $mech->follow_link( url_regex => qr/summary/ );
77            save_mech;
78    }
79    
80  while ( $from ) {  sub get_results {
81            my $desc = shift;
82            my $from = 1;
83    
84          my $to = $from + $range_size;          while ( 1 ) {
85    
86          $mech->submit_form(                  my $to = $from + $range_size;
                 form_name => 'summary_output_form',  
                 fields => {  
                         record_select_type => 'range',  
                         mark_from => $from,  
                         mark_to => $to,  
                         mark_id => 'WOS',  
87    
88                          qo_fields => 'fullrecord',                  warn "# submit_form results $from - $to\n";
                         citedref => 'citedref',  
89    
90                          save_options => 'plain_text',                  $mech->submit_form(
91                            form_name => 'summary_output_form',
92                            fields => {
93                                    record_select_type => 'range',
94                                    mark_from => $from,
95                                    mark_to => $to,
96                                    mark_id => 'WOS',
97    
98                          fields => 'Full',                                  qo_fields => 'fullrecord',
99                          format => 'save',                                  citedref => 'citedref',
                 },  
                 button => 'save',  
         );  
         save_mech $mech;  
100    
101          last if $mech->content =~ m{invalid API call};                                  save_options => 'plain_text',
102    
103          warn "# save_file";                                  fields => 'Full',
104          $mech->follow_link( url_regex => qr/save_file/ );                                  format => 'save',
105          save_mech $mech => "/tmp/isi.$q.$from-$to.txt";                          },
106                            button => 'save',
107                    );
108                    save_mech;
109    
         $from += $range_size;  
110    
111                    if ( $mech->content =~ m{invalid API call} ) {
112                            $mech->back;
113                            last;
114                    }
115    
116    
117                    my $path = "/tmp/isi.$q.$from-$to";
118                    $path .= '.' . $desc if $desc;
119    
120                    warn "save $from - $to into $path\n";
121                    $mech->follow_link( url_regex => qr/save_file/ );
122                    save_mech $path;
123    
124                    $from += $range_size - $overlap;
125    
126                    $mech->back;
127                    $mech->back;
128                    #save_mech;
129            }
130    }
131    
132    
133    sub citations {
134            warn "# citation report";
135            $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
136            save_mech;
137    
138            warn "view citing articles";
139            $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
140            save_mech;
141    }
142    
143    sub years {
144            my $years_url = $mech->find_link( url_regex => qr/ra_name=/ );
145            if ( ! $years_url ) {
146                    warn "W: can't find ra_name link\n";
147                    return;
148            }
149            $years_url = $years_url->url_abs;
150            warn "## $years_url";
151            if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
152                    warn "W: no ra_name in $years_url\n";
153                    return;
154            }
155            warn "# refine years (hidden by javascript)";
156    #       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";
157            $mech->get( $years_url );
158            save_mech;
159    
160            my $html = $mech->content;
161            my $years;
162            while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
163                    my ( $year, $count ) = ( $1, $2 );
164                    $count =~ s{,}{}g;
165                    $years->{$year} = $count;
166            }
167            warn "# years ",dump $years;
168          $mech->back;          $mech->back;
         $mech->back;  
         #save_mech $mech;  
169    
170            my @y = sort keys %$years;
171    
172            my $y = shift @y;
173            my $size = $years->{$y};
174    
175            @ranges = ();
176            my $cites_range;
177            $cites_range = [$y] if $y;
178    
179            foreach my $y ( @y ) {
180                    if ( $size + $years->{$y} > $max_cites ) {
181                            push @ranges, $cites_range;
182                            warn "# cites_range $size years ",dump( $cites_range ),$/;
183    
184                            $cites_range = [];
185                            $size = 0;
186                    }
187                    $size += $years->{$y};
188                    push @$cites_range, $y;
189            }
190    
191            if ( $cites_range ) {
192                    push @ranges, $cites_range;
193                    warn "# cites_range $size years ",dump( $cites_range ), " FINAL\n"
194            }
195    
196            warn '# ranges ', dump @ranges;
197            @ranges = () if $#ranges == 1; # just take all
198    
199            return $years;
200  }  }
201    
202    search;
203    years;
204    get_results;
205    
206    
207    citations;
208    
209    do {
210            my $part;
211            if ( @ranges ) {
212                    $part .= $ranges[0]->[0] . '.';
213                    search;
214                    citations;
215            }
216            $part .= 'citing';
217            get_results $part;
218    } while ( @ranges );
219    

Legend:
Removed from v.1282  
changed lines
  Added in v.1294

  ViewVC Help
Powered by ViewVC 1.1.26