/[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 1280 by dpavlin, Fri Sep 18 16:52:57 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 = 'TS=psychology AND 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;
11    my $overlap    = 10; # between previous and this range
12    
13    $q = 'TS=psychology AND AD=Croatia' if @ARGV; # FIXME debug
14    
15    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::Slurp;  use File::Path;
21    
22  my $mech = WWW::Mechanize->new(  our $mech = WWW::Mechanize->new(
23          autocheck => 1,          autocheck => 1,
24          cookie_jar => undef,          cookie_jar => undef,
25  );  );
26    
27  my $step = 1;  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 = shift;          my $path = shift;
         my $path = "/tmp/isi.$step.html";  
         write_file $path, $mech->content;  
         warn "# step $step ", -s $path;  
         $mech->dump_all;  
35          $step++;          $step++;
36            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 );
40            warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
41            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    sub search {
50            warn "# advanced serach";
51            $mech->follow_link( url_regex => qr/AdvancedSearch/ );
52            save_mech;
53    
54  warn "# advanced serach";          warn "# cookie_jar ", dump $mech->cookie_jar;
 $mech->follow_link( url_regex => qr/AdvancedSearch/ );  
 save_mech $mech;  
55    
56  warn "# cookie_jar ", dump $mech->cookie_jar;          my $q_this = $q;
57    
58  $mech->submit_form(          if ( @ranges ) {
59          fields => {                  $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
                 'value(input1)' => $q,  
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  my $from = 1;                  },
67  my $range_size = 10;          );
68            save_mech;
69  $mech->submit_form(  
70          form_name => 'summary_output_form',          warn "# summary";
71          fields => {          $mech->follow_link( url_regex => qr/summary/ );
72                  record_select_type => 'range',          save_mech;
73                  mark_from => $from,  }
74                  mark_to => $from += $range_size,  
75                  mark_id => 'WOS',  sub get_results {
76            my $desc = shift;
77                  qo_fields => 'fullrecord',          my $from = 1;
78                  citedref => 'citedref',  
79            while ( 1 ) {
80                  save_options => 'plain_text',  
81                    my $to = $from + $range_size;
82                  fields => 'Full',  
83                  format => 'save',                  warn "# submit_form results $from - $to\n";
84          },  
85          button => 'save',                  $mech->submit_form(
86  );                          form_name => 'summary_output_form',
87  save_mech $mech;                          fields => {
88                                    record_select_type => 'range',
89                                    mark_from => $from,
90                                    mark_to => $to,
91                                    mark_id => 'WOS',
92    
93                                    qo_fields => 'fullrecord',
94                                    citedref => 'citedref',
95    
96                                    save_options => 'plain_text',
97    
98                                    fields => 'Full',
99                                    format => 'save',
100                            },
101                            button => 'save',
102                    );
103                    save_mech;
104    
105    
106                    if ( $mech->content =~ m{invalid API call} ) {
107                            $mech->back;
108                            last;
109                    }
110    
111    
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/ );
117                    save_mech $path;
118    
119                    $from += $range_size - $overlap;
120    
121  warn "# save_file";                  $mech->back;
122  $mech->follow_link( url_regex => qr/save_file/ );                  $mech->back;
123  save_mech $mech;                  #save_mech;
124            }
125    }
126    
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    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            my @y = sort keys %$years;
166    
167            my $y = shift @y;
168            my $size = $years->{$y};
169    
170            @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.1280  
changed lines
  Added in v.1292

  ViewVC Help
Powered by ViewVC 1.1.26