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

Legend:
Removed from v.1289  
changed lines
  Added in v.1292

  ViewVC Help
Powered by ViewVC 1.1.26