/[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 1291 by dpavlin, Sat Sep 19 15:05:12 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    
 our $q = 'AD=Croatia';  
 my $range_size = 500;  
   
 my $dump = @ARGV ? 1 : 0;  
   
 $q = 'TS=psychology AND AD=Croatia';  
   
6  use WWW::Mechanize;  use WWW::Mechanize;
7  use Data::Dump qw(dump);  use Data::Dump qw(dump);
8  use File::Path;  use File::Path;
9    use Text::Unaccent;
10    
11    # Advanced search syntax:
12    # http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html
13    
14    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    $q = unac_string( 'utf-8', join(' ', @ARGV) ) if @ARGV;
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/';  my $dir = '/tmp/isi/';
41  rmtree $dir if -e $dir;  #rmtree $dir if -e $dir;
42  mkdir $dir;  mkdir $dir unless -d $dir;
43    
44  sub save_mech {  sub save_mech {
45          my ( $mech, $path ) = @_;          my $path = shift;
46          $step++;          $step++;
47          my $base_path = sprintf('%s/%04d', $dir,$step);          my $base_path = sprintf('%s/%04d', $dir,$step);
48          $path ||= $base_path . ( $mech->{ct} =~ m{html}i ? '.html' : '.txt' );          $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, "\n";          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
52          open(my $dump, '>', "$base_path.dump.txt");          open(my $dump, '>', "$base_path.dump.txt");
# Line 38  sub save_mech { Line 55  sub save_mech {
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 {  sub search {
61          warn "# advanced serach";          warn "# advanced serach";
62          $mech->follow_link( url_regex => qr/AdvancedSearch/ );          $mech->follow_link( url_regex => qr/AdvancedSearch/ );
63          save_mech $mech;          save_mech;
64    
65          warn "# cookie_jar ", dump $mech->cookie_jar;          warn "# cookie_jar ", dump $mech->cookie_jar;
66    
67            my $q_this = $q;
68    
69            if ( @ranges ) {
70                    $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
71            }
72    
73            warn "# submit_form search: $q_this\n";
74          $mech->submit_form(          $mech->submit_form(
75                  fields => {                  fields => {
76                          'value(input1)' => $q,                          'value(input1)' => $q_this,
77                  }                  },
78          );          );
79          save_mech $mech;          save_mech;
80    
81          warn "# summary";          warn "# summary";
82          $mech->follow_link( url_regex => qr/summary/ );          $mech->follow_link( url_regex => qr/summary/ );
83          save_mech $mech;          save_mech;
84  }  }
85    
86  sub get_results {  sub get_results {
87            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 84  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                          last;                          last;
120                  }                  }
121    
122                  warn "range $from - $to [$q]\n";  
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    
138    
139  sub citations {  sub citations {
         save_mech $mech;  
140          warn "# citation report";          warn "# citation report";
141          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
142          save_mech $mech;          save_mech;
143    
144          warn "view citing articles";          warn "view citing articles";
145          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
146          save_mech $mech;          save_mech;
147  }  }
148    
149  sub years {  sub years {
150          my $years_url = $mech->find_link( text_regex => qr/more options/ );          my $years_url = $mech->find_link( url_regex => qr/ra_name=/ );
151          if ( ! $years_url ) {          if ( ! $years_url ) {
152                  warn "W: can't find years\n";                  warn "W: can't find ra_name link\n";
153                  return;                  return;
154          }          }
155          $years_url = $years_url->url_abs;          $years_url = $years_url->url_abs;
156          warn "## $years_url";          warn "## $years_url";
157          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
158                  warn "W: no ra_name\n";                  warn "W: no ra_name in $years_url\n";
159                  return;                  return;
160          }          }
161          warn "# refine years (hidden by javascript)";          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";  #       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 );          $mech->get( $years_url );
164          save_mech $mech;          save_mech;
165    
166          my $html = $mech->content;          my $html = $mech->content;
167          my $years;          my $years;
168          while ( $html =~ s{>(\d\d\d\d)\s\((\d+)\)</label.+?value="PublicationYear_}{} ) {          while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
169                  $years->{$1} = $2;                  my ( $year, $count ) = ( $1, $2 );
170                    $count =~ s{,}{}g;
171                    $years->{$year} = $count;
172          }          }
173          warn "# years ",dump $years;          warn "# years ",dump $years;
174          $mech->back;          $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                            $cites_range = [];
198                            $size = 0;
199                    }
200                    $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            warn '# ranges ', dump @ranges;
210            @ranges = () if $#ranges == 1; # just take all
211    
212          return $years;          return $years;
213  }  }
214    
215  search;  search;
216  years;  years;
217  get_results;  get_results 'results' if $results;
218    
219    if ( $citations ) {
220    
221  citations;          citations;
 years;  
 $q .= '.citing';  
 get_results;  
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.1291  
changed lines
  Added in v.1334

  ViewVC Help
Powered by ViewVC 1.1.26