From 57f10859013400038f984f9d0cd9543d7952e8ce Mon Sep 17 00:00:00 2001 From: gashapwn Date: Mon, 8 Mar 2021 03:06:33 +0000 Subject: [PATCH] added bookdl.pl for downloading books from lainchan --- perl-script/bookdl.pl | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 perl-script/bookdl.pl diff --git a/perl-script/bookdl.pl b/perl-script/bookdl.pl new file mode 100644 index 0000000..5cd039d --- /dev/null +++ b/perl-script/bookdl.pl @@ -0,0 +1,92 @@ +#!/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 = ; + 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]); +}