| 1 | package Business::Legal; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | 1 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Data::Dumper; | ||
| 9 | use File::Spec; | ||
| 10 | |||
| 11 | =pod | ||
| 12 | USAGE: | ||
| 13 | |||
| 14 | my $whole_data_subfolder = "./monthly_data"; | ||
| 15 | my $folder_count = 12; | ||
| 16 | Business::Legal::read_rawdata_by_month_dirs($whole_data_subfolder, "1999-12-11", $folder_count); | ||
| 17 | |||
| 18 | USAGE: | ||
| 19 | |||
| 20 | my $subfolder = "."; | ||
| 21 | Business::Legal::get_aoh_from_range_of_jsons($subfolder); | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | $| = 1; | ||
| 28 | |||
| 29 | sub extract_tbody{ | ||
| 30 | my $html_content = shift; | ||
| 31 | $html_content =~ /<tbody>(.*)<\/tbody>/sg; | ||
| 32 | my $tbody = $1; | ||
| 33 | return $tbody; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub extract_aoh_from_html_content{ | ||
| 37 | my $html_content = shift; | ||
| 38 | my $tbody = extract_tbody($html_content); | ||
| 39 | my @tbodies = $tbody =~ /<tr>(.*?)<\/tr>/sg; | ||
| 40 | my @aoh; | ||
| 41 | foreach my $trow (@tbodies){ | ||
| 42 | my $hashref = get_hashref_from_trow($trow); | ||
| 43 | push @aoh, $hashref; | ||
| 44 | # printf "%s \n%s\n", $trow, '-' x 40 ; | ||
| 45 | } | ||
| 46 | return @aoh; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub get_hashref_from_trow{ | ||
| 50 | my $trow = shift; | ||
| 51 | my ($current_state) = $trow =~ /<td class="current-state-col d-table-cell">([^<]+)<\/td>/g; | ||
| 52 | my ($state) = $trow =~ /<td class="state-col d-none d-sm-table-cell"><a href=[^<]+>([^<]+)<\/a><\/td>/g; # new | ||
| 53 | my ($name) = $trow =~ /<td class="name-col d-none d-sm-table-cell">([^<]+)<\/td>/g; | ||
| 54 | my ($date) = $trow =~ /<td class="date-col d-none d-sm-table-cell">([^<]+)<\/td>/g; | ||
| 55 | my ($rep_id, $rep_name) = $trow =~ /<td class="rep-col d-none d-lg-table-cell"><a href="\/\w+\/(\d*)">([^<]*)<\/a><\/td>/ ? ($1, $2) : ('', ''); | ||
| 56 | my ($app_database_id, $app_legal_id) = $trow =~ /<td class="number-col d-none d-sm-table-cell"><a href="\/applications\/(\d*)">([^<]*)<\/a><\/td>/ ? ($1, $2) : ('', ''); | ||
| 57 | |||
| 58 | my $hashref = { | ||
| 59 | current_state => $current_state, | ||
| 60 | name => $name, | ||
| 61 | state=> $state, | ||
| 62 | date => $date, | ||
| 63 | rep_id => $rep_id, | ||
| 64 | rep_name => $rep_name, | ||
| 65 | app_database_id => $app_database_id, | ||
| 66 | app_legal_id => $app_legal_id, | ||
| 67 | }; | ||
| 68 | return $hashref; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub extract_aoh_from_file{ | ||
| 72 | my $fn = shift; | ||
| 73 | my $metadata = shift; | ||
| 74 | open FH, "<$fn" or die $!; | ||
| 75 | my $html_content = do {local $/; <FH>}; | ||
| 76 | my @aoh = extract_aoh_from_html_content($html_content, $metadata); | ||
| 77 | |||
| 78 | # print Dumper \@aoh; | ||
| 79 | return @aoh; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub read_destination_folder_aohref{ | ||
| 83 | my $dir = shift; | ||
| 84 | opendir(D, "$dir") || die "Can't open directory $dir: $!\n"; | ||
| 85 | my @file_list = readdir(D); | ||
| 86 | my @aoh; | ||
| 87 | foreach my $html_filename (@file_list) { | ||
| 88 | next unless $html_filename =~ /\d{4}-\d{2}-\d{2}_(\d{4}-\d{2}-\d{2})_(\d{4})/; | ||
| 89 | my $end_date_of_span = $1; | ||
| 90 | my $page = $2; | ||
| 91 | print "."; | ||
| 92 | my $full_file_path = File::Spec->catdir($dir, $html_filename); | ||
| 93 | my $metadata = {end_date_of_span=>$end_date_of_span, page=>$page, html_filename=>$html_filename}; | ||
| 94 | my @aoh_from_one_file = extract_aoh_from_file($full_file_path, $metadata); | ||
| 95 | push @aoh, @aoh_from_one_file; | ||
| 96 | } | ||
| 97 | return \@aoh; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub read_rawdata_by_month_dirs{ | ||
| 101 | my $whole_data_subfolder = shift; | ||
| 102 | my $start_folder_prefix = shift; | ||
| 103 | my $folder_count = shift; | ||
| 104 | printf "whole_data_subfolder = %s\n", $whole_data_subfolder; | ||
| 105 | opendir(D, "$whole_data_subfolder") || die "Can't open directory $whole_data_subfolder: $!\n"; | ||
| 106 | my @file_list = readdir(D); | ||
| 107 | my @aoh; | ||
| 108 | my $count = 0; | ||
| 109 | foreach my $entry (@file_list) { | ||
| 110 | if($entry ge $start_folder_prefix){ | ||
| 111 | $count++; | ||
| 112 | last if $count > $folder_count; | ||
| 113 | my $destination_folder = File::Spec->catdir($whole_data_subfolder, $entry); | ||
| 114 | print $destination_folder, "\n"; | ||
| 115 | |||
| 116 | my $aohref = read_destination_folder_aohref($destination_folder); | ||
| 117 | printf "count = %d\n", scalar @$aohref; | ||
| 118 | |||
| 119 | my $json_text = JSON->new->utf8->encode($aohref); | ||
| 120 | my $json_file_name = File::Spec->catdir($whole_data_subfolder, "$entry.json.txt"); | ||
| 121 | open FH, ">$json_file_name" or die $!; | ||
| 122 | print FH $json_text, "\n"; | ||
| 123 | close FH; | ||
| 124 | printf "Metadata output to %s\n", $json_file_name; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | closedir(D); | ||
| 128 | return @aoh; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | sub get_aoh_from_one_json{ | ||
| 136 | my $whole_data_subfolder = shift; | ||
| 137 | my $fn = shift; | ||
| 138 | my $full_file_path = File::Spec->catdir($whole_data_subfolder, $fn); | ||
| 139 | open( my $fh, '<', "$full_file_path" ); | ||
| 140 | my $json_text = <$fh>; | ||
| 141 | my $perl_scalar = decode_json( $json_text ); | ||
| 142 | # print Dumper $perl_scalar; | ||
| 143 | close($fh); | ||
| 144 | # printf "count = %d\n", scalar @$perl_scalar; | ||
| 145 | return @$perl_scalar; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub get_aoh_from_range_of_jsons{ | ||
| 149 | my $whole_data_subfolder = shift; | ||
| 150 | |||
| 151 | opendir(D, "$whole_data_subfolder") || die "Can't open directory $whole_data_subfolder: $!\n"; | ||
| 152 | my @file_list = readdir(D); | ||
| 153 | my @cumulative_aoh; | ||
| 154 | foreach my $f (@file_list) { | ||
| 155 | next if ( -d $f or $f !~ /\.json\.txt$/g); | ||
| 156 | my @aoh = get_aoh_from_one_json($whole_data_subfolder, $f); | ||
| 157 | printf "%d ", scalar @aoh; | ||
| 158 | push @cumulative_aoh, @aoh; | ||
| 159 | # print "$f "; | ||
| 160 | } | ||
| 161 | return @cumulative_aoh; | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||