line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Fabnewsru::Utils; |
2
|
|
|
|
|
|
|
$Fabnewsru::Utils::VERSION = '0.01'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Some useful methods for operating with Mojo::DOM objects |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
14480
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
59
|
|
7
|
2
|
|
|
2
|
|
895
|
use List::MoreUtils qw( each_array ); |
|
2
|
|
|
|
|
13521
|
|
|
2
|
|
|
|
|
8
|
|
8
|
|
|
|
|
|
|
# use Data::Dumper; |
9
|
2
|
|
|
2
|
|
1679
|
use common::sense; |
|
2
|
|
|
|
|
38
|
|
|
2
|
|
|
|
|
8
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
87
|
use Exporter qw(import); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
980
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(table2hash table2array_of_hashes merge_hashes rm_spec_symbols_from_hash_values rm_spec_symbols_from_string); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub table2hash { |
17
|
1
|
|
|
1
|
1
|
3114
|
my ($dom, $container) = @_; |
18
|
1
|
|
|
|
|
2
|
my $h = {}; |
19
|
1
|
|
|
|
|
5
|
my $table_dom = $dom->at($container); |
20
|
1
|
|
|
|
|
348
|
for my $i ($table_dom->find("tr")->each) { |
21
|
6
|
|
|
|
|
626
|
my $key_candidate = rm_spec_symbols_from_string($i->find("td")->[0]->all_text); |
22
|
6
|
|
|
|
|
34
|
my $val_candidate = rm_spec_symbols_from_string($i->find("td")->[1]->all_text); |
23
|
6
|
|
|
|
|
23
|
$h->{$key_candidate} = $val_candidate; |
24
|
|
|
|
|
|
|
} |
25
|
1
|
|
|
|
|
6
|
return $h; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub table2array_of_hashes { |
31
|
0
|
|
|
0
|
1
|
0
|
my ($dom, $container, $fields_arr) = @_; |
32
|
0
|
|
|
|
|
0
|
my @array_of_hashes; #result |
33
|
|
|
|
|
|
|
my $fields; # array of fields |
34
|
0
|
0
|
|
|
|
0
|
if (defined $fields_arr) { |
35
|
0
|
|
|
|
|
0
|
$fields = $fields_arr; |
36
|
|
|
|
|
|
|
} else { |
37
|
0
|
|
|
|
|
0
|
for ($dom->find("thead th")->each) { |
38
|
0
|
|
|
|
|
0
|
push @$fields, rm_spec_symbols_from_string($_->text); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# warn Dumper $fields; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
for my $i ($dom->find("tbody tr")->each) { |
45
|
0
|
|
|
|
|
0
|
my $h = {}; |
46
|
0
|
|
|
|
|
0
|
my @values = $i->find("td")->each; # html values |
47
|
0
|
|
|
|
|
0
|
my $it = each_array(@$fields, @values); |
48
|
0
|
|
|
|
|
0
|
my @urls; |
49
|
0
|
|
|
|
|
0
|
while ( my ($x, $y) = $it->() ) { # start of iteration on each | |
50
|
0
|
|
|
|
|
0
|
$h->{$x}= rm_spec_symbols_from_string($y->all_text); # couldn't be text, need to be all_text |
51
|
0
|
|
|
|
|
0
|
for my $e ($y->find('a[href]')->each) { # extract all urls; |
52
|
0
|
|
|
|
|
0
|
push @urls, $e->attr("href"); |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
0
|
$h->{urls} = \@urls; |
55
|
|
|
|
|
|
|
# if (defined $y) { |
56
|
|
|
|
|
|
|
# push @urls, $y->at("a[href]")->attr("href"); |
57
|
|
|
|
|
|
|
# } |
58
|
|
|
|
|
|
|
} # end of iteration on each | |
59
|
0
|
|
|
|
|
0
|
push @array_of_hashes, $h; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
0
|
return \@array_of_hashes; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub merge_hashes { |
66
|
1
|
|
|
1
|
1
|
147561
|
my ($fields, $values) = @_; |
67
|
1
|
|
|
|
|
2
|
my $result ={}; |
68
|
1
|
|
|
|
|
6
|
while ( my ($i, $j) = each(%$values) ) { |
69
|
6
|
|
|
|
|
8
|
my ($new_key) = grep { $fields->{$_} eq $i } keys $fields; |
|
36
|
|
|
|
|
35
|
|
70
|
6
|
|
|
|
|
13
|
$result->{$new_key} = $j; |
71
|
|
|
|
|
|
|
} |
72
|
1
|
|
|
|
|
9
|
return $result; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub rm_spec_symbols_from_string { |
78
|
12
|
|
|
12
|
1
|
1942
|
my $str = shift; |
79
|
12
|
|
|
|
|
36
|
$str =~ s/[\$#~!&;:]+//g; |
80
|
12
|
|
|
|
|
26
|
$str =~ s/^\s+//g; |
81
|
12
|
|
|
|
|
25
|
$str =~ s/\s+$//g; |
82
|
12
|
|
|
|
|
19
|
$str =~ s/\s{2,}/ /g; |
83
|
12
|
|
|
|
|
15
|
return $str; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |