| 1 | package Genealogy::ChroniclingAmerica; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # OLD API | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # https://chroniclingamerica.loc.gov/search/pages/results/?date1=1912&state=Indiana&format=json&andtext=ralph%3Dbixler | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # TODO: NEW API | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # https://libraryofcongress.github.io/data-exploration/loc.gov%20JSON%20API/Chronicling_America/README.html | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # https://libraryofcongress.github.io/data-exploration/loc.gov%20JSON%20API/Chronicling_America/ChronAm-download_results.html | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # https://www.loc.gov/collections/chronicling-america/?dl=page&end_date=1912-12-31&ops=PHRASE&qs=ralph+bixler&searchType=advanced&start_date=1912-01-01&fo=json | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use CHI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Params::Get 0.13; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Return::Set 0.02; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | Genealogy::ChroniclingAmerica - Find URLs for a given person on the Library of Congress Newspaper Records | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | Version 0.07 | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | use HTTP::Cache::Transparent; # be nice | ||||||||||||||||||||||||||||||||||||||||||
| 39 | use Genealogy::ChroniclingAmerica; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | HTTP::Cache::Transparent::init({ | ||||||||||||||||||||||||||||||||||||||||||
| 42 | BasePath => '/tmp/cache' | ||||||||||||||||||||||||||||||||||||||||||
| 43 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | my $loc = Genealogy::ChroniclingAmerica->new({ | ||||||||||||||||||||||||||||||||||||||||||
| 45 | firstname => 'John', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | lastname => 'Smith', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | state => 'Indiana', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | date_of_death => 1862 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | while(my $url = $loc->get_next_entry()) { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | print "$url\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | The B<Genealogy::ChroniclingAmerica> module allows users to search for historical newspaper records from the B<Chronicling America> archive, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | maintained by the Library of Congress. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | By providing a person's first name, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | last name, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | and state, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | the module constructs and executes search queries, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | retrieving URLs to relevant newspaper pages in JSON format. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | It supports additional filters like date of birth and date of death, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | enforces B<rate-limiting> to comply with API request limits, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | local caching, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | and includes robust error handling and validation. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | Ideal for genealogy research, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | this module streamlines access to historical newspaper archives with an easy-to-use interface. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =item * Rate-Limiting | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | A minimum interval between successive API calls can be enforced to ensure that the API is not overwhelmed and to comply with any request throttling requirements. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | Rate-limiting is implemented using L<Time::HiRes>. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | A minimum interval between API | ||||||||||||||||||||||||||||||||||||||||||
| 79 | calls can be specified via the C<min_interval> parameter in the constructor. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | Before making an API call, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | the module checks how much time has elapsed since the | ||||||||||||||||||||||||||||||||||||||||||
| 82 | last request and, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | if necessary, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | sleeps for the remaining time. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | Creates a Genealogy::ChroniclingAmerica object. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | It takes three mandatory arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =item * C<firstname> | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =item * C<lastname> | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =item * C<state> - Must be the full name, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | not an abbreviation. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | Accepts the following optional arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | =item * C<cache> | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | A caching object. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | If not provided, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | an in-memory cache is created with a default expiration of one hour. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =item * C<middlename> | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =item * C<date_of_birth> | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =item * C<date_of_death> | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =item * C<host> - The domain of the site to search, the default is L<https://chroniclingamerica.loc.gov>. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =item * C<ua> - An object that understands get and env_proxy messages, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | such as L<LWP::UserAgent::Throttled>. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =item * C<min_interval> - Amount to rate limit. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | Defaults to 3 seconds, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | inline with L<https://libraryofcongress.github.io/data-exploration/loc.gov%20JSON%20API/Chronicling_America/README.html#rate-limits> | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=28 SCAR=42.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 9 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 9 | ▶ | return unless(defined($class)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # Handle hash or hashref arguments | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 9 | ▶ | my $params = Params::Get::get_params(undef, \@_) || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params(undef, \@_) || {}
MC/DC0%0/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table50%1/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 9 | ▶ | if(Scalar::Util::blessed($class)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | # If $class is an object, clone it with new arguments | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return bless { %{$class}, %{$params} }, ref($class); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 9 | ▶ | unless($params->{'firstname'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | Carp::croak('Firstname is not optional'); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | return; # Don't know why this is needed, but it is | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # Fail when the input is just a set of numbers | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | ▶ | if($params->{'firstname'} !~ /\D/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | Carp::croak('Usage: ', __PACKAGE__, ": invalid input to new(), $params->{firstname}"); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | ▶ | unless(defined($params->{'lastname'})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | Carp::croak('Lastname is not optional'); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # Fail when the input is just a set of numbers | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ▶ | if($params->{'lastname'} !~ /\D/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | Carp::croak('Usage: ', __PACKAGE__, ": invalid input to new(), $params->{lastname}"); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | unless($params->{'state'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | Carp::croak('State is not optional'); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | if(length($params->{'state'}) == 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | Carp::croak('State needs to be the full name'); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # Fail when the input contains a number | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | if($params->{'state'} =~ /\d/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | Carp::croak('Usage: ', __PACKAGE__, ": invalid input to new(), $params->{state}"); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | my $ua = $params->{'ua'}; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | if(!defined($ua)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | my $ssl_opts; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | if(-r '/etc/ssl/certs/ca-certificates.crt') { # Linux | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | $ssl_opts = { | |||||||||||||||||||||||||||||||||||||||||
| 192 | 'SSL_ca_file' => '/etc/ssl/certs/ca-certificates.crt', | ||||||||||||||||||||||||||||||||||||||||||
| 193 | verify_hostname => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } elsif(-r '/opt/homebrew/etc/ca-certificates/cert.pem') { # MacOS | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $ssl_opts = { | |||||||||||||||||||||||||||||||||||||||||
| 197 | 'SSL_ca_file' => '/opt/homebrew/etc/ca-certificates/cert.pem', | ||||||||||||||||||||||||||||||||||||||||||
| 198 | verify_hostname => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $ssl_opts = { verify_hostname => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 204 | ssl_opts => $ssl_opts, | ||||||||||||||||||||||||||||||||||||||||||
| 205 | agent => __PACKAGE__ . "/$VERSION" | ||||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | $ua->env_proxy(1); | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # Set up caching (default to an in-memory cache if none provided) | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | ▶ | my $cache = $params->{cache} || CHI->new( | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params->{'cache'} || 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', '1 hour')
MC/DC0%0/2
$params->{'cache'} || 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', '1 hour')
$params->{'cache'}
'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', '1 hour')
Truth table33%1/3
A || B
A= $params->{'cache'}B= 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', '1 hour')
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | driver => 'Memory', | ||||||||||||||||||||||||||||||||||||||||||
| 215 | global => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 216 | expires_in => '1 hour', | ||||||||||||||||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | # Set up rate-limiting: minimum interval between requests (in seconds) | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # From https://libraryofcongress.github.io/data-exploration/loc.gov%20JSON%20API/Chronicling_America/README.html#rate-limits | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # Burst Limit: 20 requests per 1 minute, Block for 5 minutes | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | my $min_interval = $params->{min_interval} || 4; # default: four second delay | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'min_interval'} || 4
MC/DC0%0/1
$params->{'min_interval'} || 4
$params->{'min_interval'}
Truth table50%1/2
A
A= $params->{'min_interval'}
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | my $rc = { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | ▶ | %{$params}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'host'} || 'www.loc.gov'
MC/DC0%0/1
$params->{'host'} || 'www.loc.gov'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | min_interval => $min_interval, | ||||||||||||||||||||||||||||||||||||||||||
| 227 | ua => $ua, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 2 | host => $params->{'host'} || 'www.loc.gov', | |||||||||||||||||||||||||||||||||||||||||
| 229 | path => 'collections/chronicling-america', | ||||||||||||||||||||||||||||||||||||||||||
| 230 | cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||
| 231 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | my %query_parameters = ( 'fo' => 'json', 'location_state' => ucfirst(lc($params->{'state'})), 'ops' => 'PHRASE', 'searchType' => 'advanced' ); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 2 | ▶ | if($query_parameters{'location_state'} eq 'District of columbia') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | $query_parameters{'location_state'} = 'District of Columbia'; | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 2 | my $name = $params->{'firstname'}; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | ▶ | if($params->{'middlename'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | $rc->{'name'} = "$name $params->{middlename} $params->{lastname}"; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $name .= '+' . $params->{middlename}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | $rc->{'name'} = "$name $params->{lastname}"; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | $name .= "+$params->{lastname}"; | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | $name =~ s/\s/+/g; | |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | $query_parameters{'qs'} = $name; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | if($params->{'date_of_birth'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $query_parameters{'start_date'} = $params->{'date_of_birth'}; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | ▶ | if($params->{'date_of_death'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $query_parameters{'end_date'} = $params->{'date_of_death'}; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | # Just scanning for one year | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | $query_parameters{'start_date'} ||= $params->{'date_of_death'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$query_parameters{"start_date"} ||= $params->{"date_of_death"}
MC/DC0%0/2
$query_parameters{"start_date"} ||= $params->{"date_of_death"}
$query_parameters{"start_date"}
$params->{"date_of_death"}
Truth table33%1/3
A ||= B
A= $query_parameters{"start_date"}B= $params->{"date_of_death"}
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | ▶ | $query_parameters{'end_date'} ||= $params->{'date_of_birth'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$query_parameters{"end_date"} ||= $params->{"date_of_birth"}
MC/DC0%0/2
$query_parameters{"end_date"} ||= $params->{"date_of_birth"}
$query_parameters{"end_date"}
$params->{"date_of_birth"}
Truth table33%1/3
A ||= B
A= $query_parameters{"end_date"}B= $params->{"date_of_birth"}
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | $query_parameters{'start_date'} .= '-01-01' if($query_parameters{'start_date'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | ▶ | $query_parameters{'end_date'} .= '-12-31' if($query_parameters{'end_date'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | my $uri = URI->new("https://$rc->{host}/$rc->{path}"); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | my $url = $uri->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 266 | # ::diag(">>>>$url = ", $rc->{'name'}); | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # print ">>>>$url = ", $rc->{'name'}, "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | my $items = _get_items($ua, $url); | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # Update last_request timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | $rc->{'last_request'} = time(); | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | if(scalar(@{$items})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | # Add 'fo=json' to the end of each row | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my @rc; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | for my $item (@{$items}) { | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | unless($item->{'id'} =~ /&fo=json$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $item->{'id'} .= '&fo=json'; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | push @rc, $item; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $rc->{'items'} = \@rc; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $rc->{'index'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | $rc->{'matches'} = scalar(@rc); | |||||||||||||||||||||||||||||||||||||||||
| 286 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 2 | $rc->{'matches'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | return bless $rc, $class; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =head2 get_next_entry | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | Returns the next match as a URL. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | sub get_next_entry | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_next_entry: called CC=12 SCAR=48.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Exit if no matches or index out of bounds | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | return if($self->{'matches'} == 0) || ($self->{'index'} >= $self->{'matches'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"matches"} == 0 or $self->{"index"} >= $self->{"matches"}
MC/DC0%0/2
$self->{"matches"} == 0 or $self->{"index"} >= $self->{"matches"}
$self->{"matches"} == 0
$self->{"index"} >= $self->{"matches"}
Truth table33%1/3
A or B
A= $self->{"matches"} == 0B= $self->{"index"} >= $self->{"matches"}
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | # Retrieve the next entry and increment index | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $entry = $self->{'items'}->[$self->{'index'}++]; | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # ::diag(Data::Dumper->new([$entry])->Dump()); | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # Create a cache key based on the location, date and time zone (might want to use a stronger hash function if needed) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my $cache_key = "loc:$entry->{id}"; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | if(my $cached = $self->{cache}->get($cache_key)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | return $cached; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | # Enforce rate-limiting: ensure at least min_interval seconds between requests. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my $now = time(); | |||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | my $elapsed = $now - $self->{last_request}; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if($elapsed < $self->{min_interval}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | Time::HiRes::sleep($self->{min_interval} - $elapsed); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | # Make the API request | ||||||||||||||||||||||||||||||||||||||||||
| 325 | # ::diag(__LINE__); | ||||||||||||||||||||||||||||||||||||||||||
| 326 | # ::diag(Data::Dumper->new([$entry])->Dump()); | ||||||||||||||||||||||||||||||||||||||||||
| 327 | # ::diag($entry->{'id'}); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | my $resp = $self->{'ua'}->get($entry->{'id'}); | |||||||||||||||||||||||||||||||||||||||||
| 329 | # ::diag(__LINE__); | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # ::diag(Data::Dumper->new([$resp])->Dump()); | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | # Update last_request timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | $self->{last_request} = time(); | |||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Handle error responses | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if($resp->is_error()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | # print 'got: ', $resp->content(), "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | Carp::carp("get_next_entry: API returned error on $entry->{id}: ", $resp->status_line()) unless($resp->code() == 404); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | unless($resp->is_success()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | Carp::croak($resp->status_line()); | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $data = decode_json($resp->decoded_content()); | |||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | my $full_text = $data->{'full_text'}; | |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if(!defined($full_text)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | return $self->get_next_entry(); | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | $full_text =~ s/[\r\n]/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | if($full_text !~ /$self->{'name'}/ims) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | return $self->get_next_entry(); | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | # ::diag(__LINE__); | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # ::diag($data->{full_text}); | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | foreach my $page(@{$data->{'page'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if($page->{'mimetype'} eq 'application/pdf') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | # Cache the result before returning it | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | $self->{'cache'}->set($cache_key, $page->{'url'}); | |||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | return Return::Set::set_return($page->{'url'}, { type => 'string', 'min' => 5, matches => qr/\.pdf$/ }); | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | # This is the sample code at https://libraryofcongress.github.io/data-exploration/loc.gov%20JSON%20API/Chronicling_America/ChronAm-download_results.html | ||||||||||||||||||||||||||||||||||||||||||
| 370 | # translated into Perl | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # Run P1 search and get a list of results | ||||||||||||||||||||||||||||||||||||||||||
| 373 | sub _get_items | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_items: called CC=21 SCAR=52.5
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | my ($ua, $url, $items_ref, $conditional, $depth) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 2 | ▶ | $items_ref ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$items_ref ||= []
MC/DC0%0/1
$items_ref ||= []
$items_ref
Truth table50%1/2
A
A= $items_ref
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 2 | ▶ | $conditional ||= 'True'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$conditional ||= "True"
MC/DC0%0/1
$conditional ||= "True"
$conditional
Truth table50%1/2
A
A= $conditional
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 2 | ▶ | $depth ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$depth ||= 0
MC/DC0%0/1
$depth ||= 0
$depth
Truth table50%1/2
A
A= $depth
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | # Check that the query URL is not an item or resource link | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 2 | my @exclude = ('loc.gov/item', 'loc.gov/resource'); | |||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | for my $string (@exclude) { | |||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | ▶ | if (index($url, $string) != -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | Carp::croak('Your URL points directly to an item or ', | |||||||||||||||||||||||||||||||||||||||||
| 386 | 'resource page (you can tell because "item" ', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 'or "resource" is in the URL). Please use ', | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 'a search URL instead. For example, instead ', | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 'of "https://www.loc.gov/item/2009581123/", ', | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 'try "https://www.loc.gov/maps/?q=2009581123".'); | ||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | # Create URI object and add parameters | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 2 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||
| 396 | 2 | $uri->query_form( | |||||||||||||||||||||||||||||||||||||||||
| 397 | $uri->query_form(), | ||||||||||||||||||||||||||||||||||||||||||
| 398 | fo => 'json', | ||||||||||||||||||||||||||||||||||||||||||
| 399 | c => 100, | ||||||||||||||||||||||||||||||||||||||||||
| 400 | at => 'results,pagination' | ||||||||||||||||||||||||||||||||||||||||||
| 401 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | # Make HTTP request | ||||||||||||||||||||||||||||||||||||||||||
| 404 | # ::diag(__LINE__); | ||||||||||||||||||||||||||||||||||||||||||
| 405 | # ::diag($uri); | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 2 | my $response = $ua->get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | # Check that the API request was successful | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | ▶ | if($response->is_success() && $response->header('Content-Type') && ($response->header('Content-Type') =~ /json/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$response->is_success and $response->header("Content-Type") and $response->header("Content-Type") =~ /json/
$response->is_success and $response->header("Content-Type")
MC/DC0%0/3
$response->is_success and $response->header("Content-Type") and $response->header("Content-Type") =~ /json/
$response->is_success
$response->header("Content-Type")
$response->header("Content-Type") =~ /json/
Truth table25%1/4
A and B and C
A= $response->is_successB= $response->header("Content-Type")C= $response->header("Content-Type") =~ /json/
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my $data = decode_json($response->decoded_content()); | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | my $results = $data->{results}; | |||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | for my $result(@{$results}) { | |||||||||||||||||||||||||||||||||||||||||
| 414 | # Filter out anything that's a collection or web page | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | my $original_format = $result->{original_format} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$result->{'original_format'} || []
MC/DC0%0/1
$result->{'original_format'} || []
$result->{'original_format'}
Truth table0%0/2
A
A= $result->{'original_format'}
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | my $filter_out = 0; | |||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | # Check if original_format contains "collection" or "web page" | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | for my $format (@$original_format) { | |||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | if ($format =~ /collection/i || $format =~ /web page/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$format =~ /collection/i or $format =~ /web page/i
MC/DC0%0/2
$format =~ /collection/i or $format =~ /web page/i
$format =~ /collection/i
$format =~ /web page/i
Truth table0%0/3
A or B
A= $format =~ /collection/iB= $format =~ /web page/i
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $filter_out = 1; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | # Evaluate conditional (simplified - assumes 'True' means true) | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | if ($conditional ne 'True') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | $filter_out = 1; | |||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | unless ($filter_out) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | # Get the link to the item record | ||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | if (my $item = $result->{id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Filter out links to Catalog or other platforms | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | if ($item =~ /^http:\/\/www\.loc\.gov\/resource/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | # my $resource = $item; # Assign item to resource | ||||||||||||||||||||||||||||||||||||||||||
| 437 | # push @$items_ref, $resource; | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | push @$items_ref, $result; | |||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if ($item =~ /^http:\/\/www\.loc\.gov\/item/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | push @$items_ref, $result; | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | # Repeat the loop on the next page, unless we're on the last page | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # Put the $depth in case the end of list code doesn't work | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | if(($depth <= 10) && defined(my $next_url = $data->{pagination}->{next})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$depth <= 10 and defined(my $next_url = $data->{"pagination"}{"next"})
MC/DC0%0/2
$depth <= 10 and defined(my $next_url = $data->{"pagination"}{"next"})
$depth <= 10
defined(my $next_url = $data->{"pagination"}{"next"})
Truth table0%0/3
A and B
A= $depth <= 10B= defined(my $next_url = $data->{"pagination"}{"next"})
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | _get_items($ua, $next_url, $items_ref, $conditional, $depth + 1); | |||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | return $items_ref; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | Carp::carp($url, ': ', $response->status_line()); | |||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | return $items_ref; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | If a middle name is given and no match is found, | ||||||||||||||||||||||||||||||||||||||||||
| 466 | it should search again without the middle name. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | =item * L<https://github.com/nigelhorne/gedcom> | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | =item * L<https://chroniclingamerica.loc.gov> | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | =item * L<https://github.com/LibraryOfCongress/data-exploration> | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | Please report any bugs or feature requests to C<bug-genealogy-chroniclingamerica at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||
| 481 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||
| 482 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Genealogy-ChroniclingAmerica>. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 484 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | perldoc Genealogy::ChroniclingAmerica | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Genealogy-ChroniclingAmerica> | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | L<https://metacpan.org/release/Genealogy-ChroniclingAmerica> | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | Copyright 2018-2025 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | This program is released under the following licence: GPL2 | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 3 | 1; # End of Genealogy::ChroniclingAmerica | |||||||||||||||||||||||||||||||||||||||||