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

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

  ViewVC Help
Powered by ViewVC 1.1.26