Allow for ranged queries.
This commit is contained in:
@@ -6,7 +6,7 @@ Church by section number.
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
ccc <section_number>
|
||||
ccc <section_number|range>
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
54
ccc
54
ccc
@@ -30,14 +30,25 @@ use File::Spec;
|
||||
use File::HomeDir;
|
||||
use File::Path qw(make_path);
|
||||
|
||||
my $section_num = shift or usage();
|
||||
my $arg = shift or usage();
|
||||
|
||||
if($section_num eq '--help' || $section_num eq '-h') {
|
||||
if($arg eq '--help' || $arg eq '-h') {
|
||||
usage();
|
||||
}
|
||||
|
||||
unless($section_num =~ /^\d+$/) {
|
||||
print STDERR "Error: Section number must be a positive integer\n";
|
||||
# Parse argument - can be single number or range (e.g., "1691-1698")
|
||||
my ($start_section, $end_section);
|
||||
if($arg =~ /^(\d+)-(\d+)$/) {
|
||||
$start_section = $1;
|
||||
$end_section = $2;
|
||||
if($start_section > $end_section) {
|
||||
print STDERR "Error: Start section must be <= end section\n";
|
||||
exit 1;
|
||||
}
|
||||
} elsif($arg =~ /^\d+$/) {
|
||||
$start_section = $end_section = $arg;
|
||||
} else {
|
||||
print STDERR "Error: Invalid section format. Use a number (e.g., 270) or range (e.g., 1691-1698)\n";
|
||||
exit 1;
|
||||
}
|
||||
|
||||
@@ -47,23 +58,36 @@ make_path($cache_dir) unless -d $cache_dir;
|
||||
my $cache_file = File::Spec->catfile($cache_dir, 'section_map.json');
|
||||
|
||||
my $section_map = load_cache($cache_file);
|
||||
my $section_info = $section_map->{$section_num};
|
||||
|
||||
unless($section_info) {
|
||||
unless(scalar keys %$section_map) {
|
||||
print STDERR "Building cache of Catechism sections...\n";
|
||||
$section_map = build_section_map($cache_file);
|
||||
$section_info = $section_map->{$section_num};
|
||||
}
|
||||
|
||||
unless($section_info) {
|
||||
print STDERR "Error: Section $section_num not found in Catechism\n";
|
||||
# Fetch and display requested sections
|
||||
my $found_any = 0;
|
||||
for(my $i = $start_section; $i <= $end_section; $i++) {
|
||||
my $section_info = $section_map->{$i};
|
||||
if($section_info) {
|
||||
fetch_and_display_section($i, $section_info);
|
||||
$found_any = 1;
|
||||
}
|
||||
}
|
||||
|
||||
unless($found_any) {
|
||||
if($start_section == $end_section) {
|
||||
print STDERR "Error: Section $start_section not found in Catechism\n";
|
||||
} else {
|
||||
print STDERR "Error: No sections found in range $start_section-$end_section\n";
|
||||
}
|
||||
exit 1;
|
||||
}
|
||||
|
||||
fetch_and_display_section($section_num, $section_info);
|
||||
|
||||
sub usage {
|
||||
print STDERR "Usage: ccc <section_number>\n";
|
||||
print STDERR "Usage: ccc <section_number|range>\n";
|
||||
print STDERR "Examples:\n";
|
||||
print STDERR " ccc 270 # Single section\n";
|
||||
print STDERR " ccc 1691-1698 # Range of sections\n";
|
||||
exit 1;
|
||||
}
|
||||
|
||||
@@ -103,15 +127,19 @@ sub build_section_map {
|
||||
}
|
||||
|
||||
my $count = 0;
|
||||
my $consecutive_404s = 0;
|
||||
foreach my $filename (@filenames) {
|
||||
my $url = "$base_url/$filename";
|
||||
my $response = $ua->get($url);
|
||||
|
||||
unless($response->is_success) {
|
||||
# Stop when we hit a 404 - we've scanned all files
|
||||
# Count consecutive 404s, stop after too many
|
||||
$consecutive_404s++;
|
||||
next if $consecutive_404s < 10; # Allow up to 9 consecutive 404s
|
||||
last;
|
||||
}
|
||||
|
||||
$consecutive_404s = 0; # Reset counter on success
|
||||
my $content = $response->content;
|
||||
|
||||
# Extract all section numbers
|
||||
|
||||
Reference in New Issue
Block a user