/[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 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    # Advanced search syntax:
7    # http://images.isiknowledge.com/WOK46/help/WOS/h_advanced_examples.html
8    
9  our $q = 'AD=Croatia';  our $q = 'AD=Croatia';
10  my $range_size = 500;  my $range_size = 500;
11    my $overlap    = 10; # between previous and this range
12    
13  my $dump = @ARGV ? 1 : 0;  $q = 'TS=psychology AND AD=Croatia' if @ARGV; # FIXME debug
14    
15  $q = 'TS=psychology AND AD=Croatia';  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);
# Line 26  rmtree $dir if -e $dir; Line 31  rmtree $dir if -e $dir;
31  mkdir $dir;  mkdir $dir;
32    
33  sub save_mech {  sub save_mech {
34          my ( $mech, $path ) = @_;          my $path = shift;
35          $step++;          $step++;
36          my $base_path = sprintf('%s/%04d', $dir,$step);          my $base_path = sprintf('%s/%04d', $dir,$step);
37          $path ||= $base_path . ( $mech->{ct} =~ m{html}i ? '.html' : '.txt' );          $path ||= $base_path;
38            $path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt';
39          $mech->save_content( $path );          $mech->save_content( $path );
40          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
41          open(my $dump, '>', "$base_path.dump.txt");          open(my $dump, '>', "$base_path.dump.txt");
# Line 38  sub save_mech { Line 44  sub save_mech {
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 {  sub search {
50          warn "# advanced serach";          warn "# advanced serach";
51          $mech->follow_link( url_regex => qr/AdvancedSearch/ );          $mech->follow_link( url_regex => qr/AdvancedSearch/ );
52          save_mech $mech;          save_mech;
53    
54          warn "# cookie_jar ", dump $mech->cookie_jar;          warn "# cookie_jar ", dump $mech->cookie_jar;
55    
56            my $q_this = $q;
57    
58            if ( @ranges ) {
59                    $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
60            }
61    
62            warn "# submit_form search: $q_this\n";
63          $mech->submit_form(          $mech->submit_form(
64                  fields => {                  fields => {
65                          'value(input1)' => $q,                          'value(input1)' => $q_this,
66                  }                  },
67          );          );
68          save_mech $mech;          save_mech;
69    
70          warn "# summary";          warn "# summary";
71          $mech->follow_link( url_regex => qr/summary/ );          $mech->follow_link( url_regex => qr/summary/ );
72          save_mech $mech;          save_mech;
73  }  }
74    
75  sub get_results {  sub get_results {
76            my $desc = shift;
77          my $from = 1;          my $from = 1;
78    
79          while ( 1 ) {          while ( 1 ) {
80    
81                  my $to = $from + $range_size;                  my $to = $from + $range_size;
82    
83                    warn "# submit_form results $from - $to\n";
84    
85                  $mech->submit_form(                  $mech->submit_form(
86                          form_name => 'summary_output_form',                          form_name => 'summary_output_form',
87                          fields => {                          fields => {
# Line 84  sub get_results { Line 100  sub get_results {
100                          },                          },
101                          button => 'save',                          button => 'save',
102                  );                  );
103                  save_mech $mech;                  save_mech;
104    
105    
106                  if ( $mech->content =~ m{invalid API call} ) {                  if ( $mech->content =~ m{invalid API call} ) {
107                          $mech->back;                          $mech->back;
108                          last;                          last;
109                  }                  }
110    
111                  warn "range $from - $to [$q]\n";  
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/ );                  $mech->follow_link( url_regex => qr/save_file/ );
117                  save_mech $mech => "/tmp/isi.$q.$from-$to.txt";                  save_mech $path;
118    
119                  $from += $range_size;                  $from += $range_size - $overlap;
120    
121                  $mech->back;                  $mech->back;
122                  $mech->back;                  $mech->back;
123                  #save_mech $mech;                  #save_mech;
   
124          }          }
   
125  }  }
126    
127    
128  sub citations {  sub citations {
         save_mech $mech;  
129          warn "# citation report";          warn "# citation report";
130          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
131          save_mech $mech;          save_mech;
132    
133          warn "view citing articles";          warn "view citing articles";
134          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
135          save_mech $mech;          save_mech;
136  }  }
137    
138  sub years {  sub years {
139          my $years_url = $mech->find_link( text_regex => qr/more options/ );          my $years_url = $mech->find_link( url_regex => qr/ra_name=/ );
140          if ( ! $years_url ) {          if ( ! $years_url ) {
141                  warn "W: can't find years\n";                  warn "W: can't find ra_name link\n";
142                  return;                  return;
143          }          }
144          $years_url = $years_url->url_abs;          $years_url = $years_url->url_abs;
145          warn "## $years_url";          warn "## $years_url";
146          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
147                  warn "W: no ra_name\n";                  warn "W: no ra_name in $years_url\n";
148                  return;                  return;
149          }          }
150          warn "# refine years (hidden by javascript)";          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";  #       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 );          $mech->get( $years_url );
153          save_mech $mech;          save_mech;
154    
155          my $html = $mech->content;          my $html = $mech->content;
156          my $years;          my $years;
157          while ( $html =~ s{>(\d\d\d\d)\s\((\d+)\)</label.+?value="PublicationYear_}{} ) {          while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
158                  $years->{$1} = $2;                  my ( $year, $count ) = ( $1, $2 );
159                    $count =~ s{,}{}g;
160                    $years->{$year} = $count;
161          }          }
162          warn "# years ",dump $years;          warn "# years ",dump $years;
163          $mech->back;          $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;          return $years;
195  }  }
196    
# Line 150  get_results; Line 200  get_results;
200    
201    
202  citations;  citations;
203  years;  
204  $q .= '.citing';  do {
205  get_results;          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.1291  
changed lines
  Added in v.1292

  ViewVC Help
Powered by ViewVC 1.1.26