-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchSefaria
executable file
·84 lines (70 loc) · 1.79 KB
/
fetchSefaria
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env perl
#fetches data from sefaria
use v5.12;
use utf8;
use JSON;
use LWP::Simple;
use Data::Dumper;
use Getopt::Long;
use lib qw(.);
use common::sense;
my $loadJsonManifest =0;
GetOptions(
"json|j=s" => \$loadJsonManifest
);
if( $loadJsonManifest){
local $/;
open( my $fh, '<', $loadJsonManifest );
my $json_text = <$fh>;
my $specs = decode_json( $json_text );
for my $spec (@$specs){
if( $spec->{literal}){
say $spec->{literal};
}
else{
say "[" . $spec->{name} . "]";
say join ("\n",fetch($spec->{passage}));
}
}
}
else{
my $spec = $ARGV[0] || 'Exodus.10.1-11' ;
my @lines = fetch($spec);
say join ("\n", @lines);
}
exit;
sub fetch{
my( $spec) = @_;
#https://www.sefaria.org.il/api/texts/Exodus.10.1-11?commentary=0&context=1&pad=0&wrapLinks=1
#grab data from sefaria;
my $url = "https://www.sefaria.org.il/api/texts/${spec}?commentary=0&context=1&pad=0&wrapLinks=1";
my $json = qx(curl -s $url) or die "failed to get $url";
my $obj = decode_json($json);
my @lines;
if( $obj->{isSpanning}){
my $spanIndex =0 ;
for my $spanSpec (@{$obj->{spanningRefs}}){
my $srcAref = $obj->{he}[$spanIndex];
my ($begSpec, $endSpec) = $spanSpec =~ m/:(?:(\d+)-)?(\d+)/;
if( ! $begSpec ){
$begSpec = $endSpec;
}
my $firstIndex = $begSpec -1;
my $lastIndex = $endSpec -1;
push @lines, @{$srcAref}[$firstIndex..$lastIndex];
$spanIndex++;
}
}
else{
my $spanSpec = $obj->{ref};
my $srcAref = $obj->{he};
my ($begSpec, $endSpec) = $spanSpec =~ m/:(?:(\d+)-)?(\d+)/;
if( ! $begSpec ){
$begSpec = $endSpec;
}
my $firstIndex = $begSpec -1;
my $lastIndex = $endSpec -1;
push @lines, @{$srcAref}[$firstIndex..$lastIndex];
}
return @lines;
}