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

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

  ViewVC Help
Powered by ViewVC 1.1.26