/[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 1332 by dpavlin, Sun Apr 18 11:04:49 2010 UTC
# Line 3  Line 3 
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
 my $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::Slurp;  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 $skip_results = 0;
18    my $cites_by_year = 0;
19    
20    my $max_cites = 5000; # ISI limit to get cites
21    
22    if ( 0 ) {
23            $q = 'TS=psychology AND AD=Croatia';
24            $range_size = 50;
25            $overlap    = 0;
26            $max_cites  = 50;
27    }
28    
29    $q = unac_string( 'utf-8', join(' ', @ARGV) ) if @ARGV;
30    
31  my $mech = WWW::Mechanize->new(  our $mech = WWW::Mechanize->new(
32          autocheck => 1,          autocheck => 1,
33          cookie_jar => undef,          cookie_jar => undef,
34  );  );
35    
36  my $step = 1;  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 = shift;          my $path = shift;
         my $path = "/tmp/isi.$step.html";  
         write_file $path, $mech->content;  
         warn "# step $step ", -s $path;  
         $mech->dump_all;  
45          $step++;          $step++;
46            my $base_path = sprintf('%s/%04d', $dir,$step);
47            $path ||= $base_path;
48            $path .= $mech->{ct} =~ m{html}i ? '.html' : '.txt';
49            $mech->save_content( $path );
50            warn "# [$step] $path ", -s $path, " ", $mech->ct, "\n";
51            open(my $dump, '>', "$base_path.dump.txt");
52            $mech->dump_all($dump);
53  }  }
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 {
60            warn "# advanced serach";
61            $mech->follow_link( url_regex => qr/AdvancedSearch/ );
62            save_mech;
63    
64  warn "# advanced serach";          warn "# cookie_jar ", dump $mech->cookie_jar;
 $mech->follow_link( url_regex => qr/AdvancedSearch/ );  
 save_mech $mech;  
65    
66  warn "# cookie_jar ", dump $mech->cookie_jar;          my $q_this = $q;
67    
68  $mech->submit_form(          if ( @ranges ) {
69          fields => {                  $q_this .= ' AND (' . join(' OR ', map { "PY=$_" } @{ shift @ranges } ) . ')';
                 'value(input1)' => $q,  
70          }          }
 );  
 save_mech $mech;  
71    
72  warn "# summary";          warn "# submit_form search: $q_this\n";
73  $mech->follow_link( url_regex => qr/summary/ );          $mech->submit_form(
74  save_mech $mech;                  fields => {
75                            'value(input1)' => $q_this,
76  my $from = 1;                  },
77  my $range_size = 10;          );
78            save_mech;
79  $mech->submit_form(  
80          form_name => 'summary_output_form',          warn "# summary";
81          fields => {          $mech->follow_link( url_regex => qr/summary/ );
82                  record_select_type => 'range',          save_mech;
83                  mark_from => $from,  }
84                  mark_to => $from += $range_size,  
85                  mark_id => 'WOS',  sub get_results {
86            my $desc = shift;
87                  qo_fields => 'fullrecord',          my $from = 1;
88                  citedref => 'citedref',  
89            while ( 1 ) {
90                  save_options => 'plain_text',  
91                    my $to = $from + $range_size;
92                  fields => 'Full',  
93                  format => 'save',                  warn "# submit_form results $from - $to\n";
94          },  
95          button => 'save',                  $mech->submit_form(
96  );                          form_name => 'summary_output_form',
97  save_mech $mech;                          fields => {
98                                    record_select_type => 'range',
99                                    mark_from => $from,
100                                    mark_to => $to,
101                                    mark_id => 'WOS',
102    
103                                    qo_fields => 'fullrecord',
104                                    citedref => 'citedref',
105    
106                                    save_options => 'plain_text',
107    
108                                    fields => 'Full',
109                                    format => 'save',
110                            },
111                            button => 'save',
112                    );
113                    save_mech;
114    
115    
116                    if ( $mech->content =~ m{invalid API call} ) {
117                            $mech->back;
118                            last;
119                    }
120    
121    
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/ );
127                    save_mech $path;
128    
129                    $from += $range_size - $overlap;
130    
131                    $mech->back;
132                    $mech->back;
133                    #save_mech;
134            }
135    }
136    
137    
138    sub citations {
139            warn "# citation report";
140            $mech->follow_link( url_regex => qr/search_mode=CitationReport/ );
141            save_mech;
142    
143            warn "view citing articles";
144            $mech->follow_link( url_regex => qr/search_mode=TotalCitingArticles/ );
145            save_mech;
146    }
147    
148    sub years {
149            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";
156            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)";
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";
162            $mech->get( $years_url );
163            save_mech;
164    
165            my $html = $mech->content;
166            my $years;
167            while ( $html =~ s{<label.+?PublicationYear.+?>(\d{4})\s\(([\d,]+)\)</label>}{} ) {
168                    my ( $year, $count ) = ( $1, $2 );
169                    $count =~ s{,}{}g;
170                    $years->{$year} = $count;
171            }
172            warn "# years ",dump $years;
173            $mech->back;
174    
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  warn "# save_file";          my $y = shift @y;
186  $mech->follow_link( url_regex => qr/save_file/ );          my $size = $years->{$y};
187  save_mech $mech;  
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    search;
215    years;
216    get_results unless $skip_results;
217    
218    
219    citations;
220    
221    do {
222            my $part;
223            if ( @ranges ) {
224                    $part .= $ranges[0]->[0] . '.';
225                    search;
226                    citations;
227            }
228            $part .= 'citing';
229            get_results $part;
230    } while ( @ranges );
231    

Legend:
Removed from v.1280  
changed lines
  Added in v.1332

  ViewVC Help
Powered by ViewVC 1.1.26