| 1 | package Email::Extractor; | ||||||||||||||||||||||||||||
| 2 | 1 | $Email::Extractor::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Fast email crawler | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use HTML::Encoding 'encoding_from_html_document'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use List::Compare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use List::Util qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Email::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Email::Extractor::Utils qw[:ALL]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | my ( $class, %param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 1 | $param{ua} = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 19 | 1 | ▶ | $param{only_lang} = 'ru' if !defined $param{only_lang}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | bless {%param}, $class; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub search_until_attempts { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my ( $self, $uri, $attempts ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search_until_attempts: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $attempts = 10 if !defined $attempts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | my $links_checked = 1; | |||||||||||||||||||||||||||
| 29 | 0 | my $a = $self->get_emails_from_uri($uri); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return $a if @$a; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | while ( !@$a && $links_checked <= $attempts ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @$a and $links_checked <= $attempts
MC/DC0%0/2
not @$a and $links_checked <= $attempts
not @$a
$links_checked <= $attempts
Truth table0%0/3
not(A) and B
A= not @$aB= $links_checked <= $attempts
| |||||||||||||||||||||||||||||
| 34 | { # but no more than 10 iterations | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $urls = $crawler->extract_contact_links; | |||||||||||||||||||||||||||
| 37 | 0 | for my $u (@$urls) { | |||||||||||||||||||||||||||
| 38 | 0 | $a = $crawler->get_emails_from_uri($u); | |||||||||||||||||||||||||||
| 39 | 0 | $links_checked++; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | $self->{last_attempts} = $links_checked; | |||||||||||||||||||||||||||
| 44 | 0 | return $a; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub get_emails_from_uri { | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | my ( $self, $addr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_emails_from_uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 3 | @emails = (); | |||||||||||||||||||||||||||
| 51 | 3 | $self->{last_uri} = $addr; | |||||||||||||||||||||||||||
| 52 | 3 | my $text = load_addr_to_str($addr); | |||||||||||||||||||||||||||
| 53 | $self->{last_text} = | ||||||||||||||||||||||||||||
| 54 | 3 | $text; # store html in memory to speed up further search | |||||||||||||||||||||||||||
| 55 | my $finder = Email::Find->new( | ||||||||||||||||||||||||||||
| 56 | sub { | ||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my ( $email, $orig_email ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 4 | push @emails, $orig_email; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 3 | ); | |||||||||||||||||||||||||||
| 61 | 3 | $finder->find( \$text ); | |||||||||||||||||||||||||||
| 62 | 3 | @emails = uniq @emails; | |||||||||||||||||||||||||||
| 63 | 3 | return \@emails; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub extract_contact_links { | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | my ( $self, $text ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract_contact_links: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | ▶ | $text = $self->{last_text} if !defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | my $all_links = find_all_links($text); | |||||||||||||||||||||||||||
| 73 | 6 | $self->{last_all_links} = $all_links; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # TO-DO: do not remove links on social networks since there can be email too | ||||||||||||||||||||||||||||
| 76 | 6 | ▶ | if ( $self->{last_uri} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $all_links = remove_external_links( $all_links, $self->{last_uri} ); | |||||||||||||||||||||||||||
| 78 | 0 | $all_links = absolutize_links_array( $all_links, $self->{last_uri} ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 6 | $all_links = remove_query_params($all_links); | |||||||||||||||||||||||||||
| 82 | 6 | $all_links = drop_asset_links($all_links); | |||||||||||||||||||||||||||
| 83 | 6 | $all_links = drop_anchor_links($all_links); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | my @potential_contact_links; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | if ( $self->{only_lang} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 6 | my $contacts_loc = $self->contacts->{ $self->{only_lang} }; | |||||||||||||||||||||||||||
| 89 | push @potential_contact_links, | ||||||||||||||||||||||||||||
| 90 | 6 | @{ find_links_by_text( $text, $contacts_loc ) }; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 0 | for my $c ( @{ $self->contacts } ) { | |||||||||||||||||||||||||||
| 94 | 0 | my $res = find_links_by_text( $text, $c ); | |||||||||||||||||||||||||||
| 95 | 0 | push @potential_contact_links, @$res; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | my $grep_url_expr = join( '|', $self->url_with_contacts ); | |||||||||||||||||||||||||||
| 100 | my @potential_contact_links_by_url = | ||||||||||||||||||||||||||||
| 101 | 6 | grep { $_ =~ /$grep_url_expr/ } @$all_links; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | my @contact_links = | |||||||||||||||||||||||||||
| 104 | ( @potential_contact_links_by_url, @potential_contact_links ); | ||||||||||||||||||||||||||||
| 105 | 6 | @contact_links = uniq @contact_links; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | $self->{non_contact_links} = | ||||||||||||||||||||||||||||
| 108 | 6 | List::Compare->new( $all_links, \@contact_links )->get_symdiff; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 6 | return \@contact_links; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub contacts { | ||||||||||||||||||||||||||||
| 116 | return { | ||||||||||||||||||||||||||||
| 117 | 'en' => 'Contacts', | ||||||||||||||||||||||||||||
| 118 | 'ru' => 'Контакты', | ||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub url_with_contacts { | ||||||||||||||||||||||||||||
| 124 | 6 | ▶ | return qw/ | ||||||||||||||||||||||||||
|
Subroutine
url_with_contacts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | contacts | ||||||||||||||||||||||||||||
| 126 | kontaktyi | ||||||||||||||||||||||||||||
| 127 | kontakty | ||||||||||||||||||||||||||||
| 128 | about | ||||||||||||||||||||||||||||
| 129 | /; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub get_encoding { | ||||||||||||||||||||||||||||
| 134 | my ( $self, $html ) = @_; | ||||||||||||||||||||||||||||
| 135 | my $html_to_check = $html || $self->{last_text}; | ||||||||||||||||||||||||||||
| 136 | return encoding_from_html_document($html_to_check); | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub contacts { | ||||||||||||||||||||||||||||
| 141 | return { | ||||||||||||||||||||||||||||
| 142 | 6 | ▶ | 'en' => 'Contacts', | ||||||||||||||||||||||||||
|
Subroutine
contacts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 'ru' => 'Контакты', | ||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub get_encoding { | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my ( $self, $html ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_encoding: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $html_to_check = $html || $self->{last_text}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$html || $self->{'last_text'}
MC/DC0%0/2
$html || $self->{'last_text'}
$html
$self->{'last_text'}
Truth table0%0/3
A || B
A= $htmlB= $self->{'last_text'}
| |||||||||||||||||||||||||||||
| 151 | 0 | return encoding_from_html_document($html_to_check); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||