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

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

  ViewVC Help
Powered by ViewVC 1.1.26