lyadmin/perl-script/bookdl.pl

93 lines
1.7 KiB
Perl

#!/usr/bin/perl
binmode STDOUT, ":utf8";
use warnings;
use strict;
use JSON;
my $THREAD_NO;
my $URL_PREFIX;
my $FN;
my $OUT_DIR;
my %jh;
my @a1;
$URL_PREFIX = "https://lainchan.org/lit/src/";
$THREAD_NO = 4953;
$FN = "$THREAD_NO.json";
$OUT_DIR = "./dl/";
# Read JSON with list of files
open FILE, "<", $FN or die "could not open file";
do{
my $json_str;
local $/=undef;
$json_str = <FILE>;
chomp $json_str;
%jh = %{JSON->new()->decode($json_str)};
};
close FILE;
# anonymous function that returns a list
# of tuples of the below form:
# (file_name, file_url)
@a1 = sub{
my @a0;
my @a2;
my $f1;
# filters for file types we
# dont want to downloads
sub f1 {
return $_[0]->{"ext"} && !($_[0]->{"ext"} =~ /jpe?g/);
}
sub f2 {
return !($_[0]->{"ext"} =~ /png/);
}
sub f3 {
return !($_[0]->{"ext"} =~ /gif/);
}
sub f4 {
return !($_[0]->{"ext"} =~ /webm/);
}
sub f0 {
return f1($_[0]) && f2($_[0]) && f3($_[0]) && f4($_[0])
}
# create an array of files
# that meet our file ext requirement
@a0 = grep {f0($_)} @{$jh{"posts"}};
# do the same filter on the
# extra_files attribute
@a2 = grep {
f0($_)
} map {
@{$_->{"extra_files"}}
} grep {
$_->{"extra_files"}
} @{$jh{"posts"}};
# Return our tuple
return map {
[
sprintf("%s%s", $_->{"filename"}, $_->{"ext"}), # file_name
sprintf("%s%s%s", $URL_PREFIX, $_->{"tim"}, $_->{"ext"}) # file_url
]
} (@a0, @a2);
}->();
# Print a list of wget commands from our tuples
for my $i1 (@a1){
printf("wget -N %s -O '%s%s'\n", scalar $i1->[1], $OUT_DIR, scalar $i1->[0]);
}