Allow for ranged queries.

This commit is contained in:
2026-03-31 15:43:05 +02:00
parent 6379b2dc42
commit baa24f17b6
2 changed files with 42 additions and 14 deletions

View File

@@ -6,7 +6,7 @@ Church by section number.
## Usage ## Usage
```bash ```bash
ccc <section_number> ccc <section_number|range>
``` ```
## Dependencies ## Dependencies

54
ccc
View File

@@ -30,14 +30,25 @@ use File::Spec;
use File::HomeDir; use File::HomeDir;
use File::Path qw(make_path); 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(); usage();
} }
unless($section_num =~ /^\d+$/) { # Parse argument - can be single number or range (e.g., "1691-1698")
print STDERR "Error: Section number must be a positive integer\n"; 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; 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 $cache_file = File::Spec->catfile($cache_dir, 'section_map.json');
my $section_map = load_cache($cache_file); 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"; print STDERR "Building cache of Catechism sections...\n";
$section_map = build_section_map($cache_file); $section_map = build_section_map($cache_file);
$section_info = $section_map->{$section_num};
} }
unless($section_info) { # Fetch and display requested sections
print STDERR "Error: Section $section_num not found in Catechism\n"; 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; exit 1;
} }
fetch_and_display_section($section_num, $section_info);
sub usage { 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; exit 1;
} }
@@ -103,15 +127,19 @@ sub build_section_map {
} }
my $count = 0; my $count = 0;
my $consecutive_404s = 0;
foreach my $filename (@filenames) { foreach my $filename (@filenames) {
my $url = "$base_url/$filename"; my $url = "$base_url/$filename";
my $response = $ua->get($url); my $response = $ua->get($url);
unless($response->is_success) { 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; last;
} }
$consecutive_404s = 0; # Reset counter on success
my $content = $response->content; my $content = $response->content;
# Extract all section numbers # Extract all section numbers