public
Created

DON'T EDIT/REMOVE - referred to in http://perlmonks.org/?node_id=1024748

  • Download Gist
orsr.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#!/usr/bin/perl
# Which companies are the names associated with?
use strict;
use warnings;
use HTML::LinkExtor;
use LWP::Simple;
use URI::Encode qw(uri_encode);
 
my $names_file = shift;
 
open my $fh, "<", $names_file;
my @names = <$fh>;
close $fh;
 
for my $name (@names) {
chomp $name;
print "Working on '$name'\n";
my $base_url = 'http://www.orsr.sk';
 
my @names = split ' ', $name;
if ( scalar @names ne 2 ) { # strange name
print "Skipping: >$name<\n";
next;
}
 
my $gname = uri_encode( lc $names[0] );
my $surname = uri_encode( lc $names[1] );
 
#my $gname = lc $names[0];
#my $surname = lc $names[1];
 
my $url =
"${base_url}/hladaj_osoba.asp?PR=${surname}&MENO=${gname}&SID=0&T=f0&R=on";
print ">$url<\n"; # Debug
 
sleep int rand(3); # lets be decent and wait for some random time
my @ids = get_ids($url);
if ( @ids == 0 ) {
 
# The name is not associated with any company
} else {
print "$name associated with ", scalar @ids, " companies\n";
for my $id (@ids) {
print "\t$base_url/vypis.asp?ID=${id}&SID=5&P=0\n";
}
}
}
 
####################
sub get_ids {
####################
my $url = shift;
 
my @ids; # internal ORSR IDs
my $parser = HTML::LinkExtor->new();
$parser->parse( get $url)->eof;
for my $linkarray ( $parser->links ) {
my @element = @$linkarray;
my ( $elt_type, $attr_name, $link ) = @element;
if ( $elt_type eq 'a' and $attr_name eq 'href' ) {
 
# vypis.asp?ID=101797&SID=5&P=0
push @ids, $1 if $link =~ /^vypis\.asp\?ID=(\d+).*P=0$/;
}
}
 
return @ids;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.