/[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 1290 by dpavlin, Sat Sep 19 12:43:03 2009 UTC revision 1335 by dpavlin, Sat Jun 5 15:49:59 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    use Data::Dump qw(dump);
8    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    $q = 'CA=BRATKO, D';
16    
17  my $range_size = 500;  my $range_size = 500;
18    my $overlap    = 3; # between previous and this range
19    
20  my $dump = @ARGV ? 1 : 0;  my $results = 0;
21    my $citations = 0;
22    
23  $q = 'TS=psychology AND AD=Croatia';  my $cites_by_year = 0;
24    
25  use WWW::Mechanize;  my $max_cites = 5000; # ISI limit to get cites
26  use Data::Dump qw/dump/;  
27    $q = unac_string( 'utf-8', join(' ', @ARGV) ) if @ARGV;
28    
29    warn "QUERY: $q\n";
30    
31  our $mech = WWW::Mechanize->new(  our $mech = WWW::Mechanize->new(
32          autocheck => 1,          autocheck => 0, # it dies in reference download with it!
33          cookie_jar => undef,          cookie_jar => undef,
34  );  );
35    
36  our $step = 0;  our $step = 0;
37    our @ranges;
38    
39    my $dir = '/tmp/isi/';
40    #rmtree $dir if -e $dir;
41    mkdir $dir unless -d $dir;
42    
43  sub save_mech {  sub save_mech {
44          my ( $mech, $path ) = @_;          my $path = shift;
45          $step++;          $step++;
46          mkdir '/tmp/isi/' unless -e '/tmp/isi';          my $base_path = sprintf('%s/%04d', $dir,$step);
47          my $base_path = sprintf('/tmp/isi/%04d', $step);          $path ||= $base_path;
48          $path ||= $base_path . ( $mech->{ct} =~ m{html}i ? '.html' : '.txt' );          $path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt';
49          $mech->save_content( $path );          $mech->save_content( $path );
50          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";          warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
51          open(my $dump, '>', "$base_path.dump.txt");          open(my $dump, '>', "$base_path.dump.txt");
# Line 34  sub save_mech { Line 54  sub save_mech {
54    
55  warn "# get session";  warn "# get session";
56  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );  $mech->get( 'http://isiknowledge.com/?DestApp=WOS' );
57  save_mech $mech;  save_mech;
58    
59  sub search {  sub search {
         my $q = shift;  
   
60          warn "# advanced serach";          warn "# advanced serach";
61          $mech->follow_link( url_regex => qr/AdvancedSearch/ );          $mech->follow_link( url_regex => qr/AdvancedSearch/ );
62          save_mech $mech;          save_mech;
63    
64          warn "# cookie_jar ", dump $mech->cookie_jar;          warn "# cookie_jar ", dump $mech->cookie_jar;
65    
66            my $q_this = $q;
67    
68            if ( @ranges ) {
69                    $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
70            }
71    
72            warn "# submit_form search: $q_this\n";
73          $mech->submit_form(          $mech->submit_form(
74                  fields => {                  fields => {
75                          'value(input1)' => $q,                          'value(input1)' => $q_this,
76                  }                  },
77          );          );
78          save_mech $mech;          save_mech;
79    
80          warn "# summary";          warn "# summary";
81          $mech->follow_link( url_regex => qr/summary/ );          $mech->follow_link( url_regex => qr/summary/ );
82          save_mech $mech;          save_mech;
83  }  }
84    
85  sub get_results {  sub get_results {
86          my $q = shift;          my $desc = shift;
87          my $from = 1;          my $from = 1;
88    
89          while ( 1 ) {          while ( 1 ) {
90    
91                  my $to = $from + $range_size;                  my $to = $from + $range_size;
92    
93                    warn "# submit_form results $from - $to\n";
94    
95                  $mech->submit_form(                  $mech->submit_form(
96                          form_name => 'summary_output_form',                          form_name => 'summary_output_form',
97                          fields => {                          fields => {
# Line 83  sub get_results { Line 110  sub get_results {
110                          },                          },
111                          button => 'save',                          button => 'save',
112                  );                  );
113                  save_mech $mech;                  save_mech;
114    
115    
116                  if ( $mech->content =~ m{invalid API call} ) {                  if ( $mech->content =~ m{invalid API call} ) {
117                          $mech->back;                          $mech->back;
118                          last;                          last;
119                  }                  }
120    
121                  warn "range $from - $to [$q]\n";  
122                    my $path = "/tmp/isi.$q.$from-$to";
123                    $path .= '.' . $desc if $desc;
124    
125                    warn "save $from - $to into $path\n";
126                  $mech->follow_link( url_regex => qr/save_file/ );                  $mech->follow_link( url_regex => qr/save_file/ );
127                  save_mech $mech => "/tmp/isi.$q.$from-$to.txt";                  save_mech $path;
128    
129                  $from += $range_size;                  $from += $range_size - $overlap;
130    
131                  $mech->back;                  $mech->back;
132                  $mech->back;                  $mech->back;
133                  #save_mech $mech;                  #save_mech;
   
134          }          }
   
135  }  }
136    
137    
138  sub citations {  sub citations {
         save_mech $mech;  
139          warn "# citation report";          warn "# citation report";
140          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );          $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
141          save_mech $mech;          save_mech;
142    
143          warn "view citing articles";          warn "view citing articles";
144          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );          $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
145          save_mech $mech;          save_mech;
146  }  }
147    
148  sub years {  sub years {
149          my $years_url = $mech->find_link( text_regex => qr/more options/ )->url_abs;          my $years_url = $mech->find_link( url_regex => qr/ra_name=/ );
150            if ( ! $years_url ) {
151                    warn "W: can't find ra_name link\n";
152                    return;
153            }
154            $years_url = $years_url->url_abs;
155          warn "## $years_url";          warn "## $years_url";
156          $years_url =~ s{ra_name=\w+}{ra_name=PublicationYear} || die "ra_name";          if ( $years_url !~ s{ra_name=\w+}{ra_name=PublicationYear} ) {
157                    warn "W: no ra_name in $years_url\n";
158                    return;
159            }
160          warn "# refine years (hidden by javascript)";          warn "# refine years (hidden by javascript)";
161          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";
162          $mech->get( $years_url );          $mech->get( $years_url );
163          save_mech $mech;          save_mech;
164    
165          my $html = $mech->content;          my $html = $mech->content;
166          my @years;          my $years;
167          while ( $html =~ s{>(\d\d\d\d)\s\((\d+)\)</label.+?value="PublicationYear_}{} ) {          while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
168                  push @years, [ $1 => $2 ];                  my ( $year, $count ) = ( $1, $2 );
169                    $count =~ s{,}{}g;
170                    $years->{$year} = $count;
171          }          }
172          warn "# years ",dump @years;          warn "# years ",dump $years;
173          $mech->back;          $mech->back;
174          return @years;  
175            my @y = sort keys %$years;
176    
177            @ranges = ();
178    
179            if ( $cites_by_year ) {
180                    push @ranges, [ $_ ] foreach @y;
181                    warn "# cites_by_year ranges ", dump @ranges;
182                    return;
183            }
184    
185            my $y = shift @y;
186            my $size = $years->{$y};
187    
188            my $cites_range;
189            $cites_range = [$y] if $y;
190    
191            foreach my $y ( @y ) {
192                    if ( $size + $years->{$y} > $max_cites ) {
193                            push @ranges, $cites_range;
194                            warn "# cites_range $size years ",dump( $cites_range ),$/;
195    
196                            $cites_range = [];
197                            $size = 0;
198                    }
199                    $size += $years->{$y};
200                    push @$cites_range, $y;
201            }
202    
203            if ( $cites_range ) {
204                    push @ranges, $cites_range;
205                    warn "# cites_range $size years ",dump( $cites_range ), " FINAL\n"
206            }
207    
208            warn '# ranges ', dump @ranges;
209            @ranges = () if $#ranges == 1; # just take all
210    
211            return $years;
212    }
213    
214    if ( $results ) {
215            search;
216            years;
217            get_results;
218    }
219    
220    if ( $citations ) {
221    
222            citations;
223            years unless @ranges;
224    
225            do {
226                    my $part;
227                    if ( @ranges ) {
228                            $part .= $ranges[0]->[0] . '.';
229                            search;
230                            citations;
231                    }
232                    $part .= 'citing';
233                    get_results $part;
234            } while ( @ranges );
235    
236  }  }
237    
238  search $q;  if ( $q =~ m{CA=(.+)} ) {
239  years;  
240  get_results $q;          my $CA = $1;
241    
242            warn "# citated reference search";
243            $mech->follow_link( url_regex => qr/CitedReferenceSearch/ );
244            save_mech;
245    
246    
247            $mech->submit_form(
248                    form_name => 'WOS_CitedReferenceSearch_input_form',
249                    fields => {
250                            'value(input1)' => $CA,
251                    },
252            );
253    
254            my $page = 1;
255            my $records = $1 if $mech->content =~ m/(\d+)\s+records/;
256            warn "# found $records records\n";
257            my $last_span = 'fake';
258    
259            while (1) {
260                    save_mech "/tmp/isi.$q.citedref.$page";
261    
262                    $page++;
263    
264                    $mech->submit_form(
265                            form_name => 'summary_navigation',
266                            fields => {
267                                    'page' => $page,
268                            },
269                    );
270    
271                    $mech->form_name( 'summary_navigation' );
272                    last if $mech->value('page') < $page;
273    
274                    if ( $mech->content =~ m/(\d+\s*-\s*(\d+))/ ) {
275                            warn "span: $1\n";
276                            last if $2 == $records;
277                            last if $1 == $last_span;
278                            $last_span = $1;
279                    } elsif ( $page > 5 ) {
280                            warn "ARTIFICALLY LIMITED TO 5 PAGES WITHOUT VALID SPAN!";
281                            last;
282                    }
283    
284            }
285    
286  citations;  }
 years;  
 get_results $q . '.citing';  
287    
288    warn "OVER\n";

Legend:
Removed from v.1290  
changed lines
  Added in v.1335

  ViewVC Help
Powered by ViewVC 1.1.26