270 lines
5.5 KiB
Perl
Executable File
270 lines
5.5 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
# use cPanelUserConfig; #for cpanel servers
|
|
|
|
use 5.010;
|
|
use strict;
|
|
use warnings;
|
|
use File::Slurp; # https://metacpan.org/pod/File::Slurp
|
|
use JSON;
|
|
use Time::Piece;
|
|
|
|
use DateTime;
|
|
|
|
###################################
|
|
#
|
|
# Daily Blackout Poetry Challenge
|
|
#
|
|
# This script supports the Daily Blackout Poetry Challenge
|
|
# it takes in text files of books and converts them into JSON
|
|
# splitting them up into "pages" for easier display
|
|
#
|
|
# license GPLv3.0 https://www.gnu.org/licenses/gpl-3.0.en.html
|
|
# Code repository:
|
|
#
|
|
# Written by Jacob Haddon https://jacobhaddon.com
|
|
#
|
|
# This uses File::Slurp - https://metacpan.org/pod/File::Slurp
|
|
# Texts are from Project Gutenburg - https://www.gutenberg.org/
|
|
#
|
|
###################################
|
|
|
|
my $json = new JSON;
|
|
|
|
# my $date = localtime->strftime('%Y-%m-%d');
|
|
# print $date;
|
|
|
|
my @books = (
|
|
{
|
|
title => "Wuthering Heights",
|
|
author => "Emily Bronte",
|
|
url => "https://www.gutenberg.org/ebooks/768",
|
|
file => "books/wutheringheights.txt"
|
|
},
|
|
{
|
|
title => "Jane Eyre",
|
|
author => "Charlotte Bronte",
|
|
url => "https://www.gutenberg.org/ebooks/1260",
|
|
file => "books/janeeyre.txt"
|
|
},
|
|
{
|
|
title => "Agnes Grey",
|
|
author => "Anne Bronte",
|
|
url => "https://www.gutenberg.org/ebooks/767",
|
|
file => "books/agnesgrey.txt"
|
|
}
|
|
); # books hash
|
|
|
|
# print($books[1]{'file'});
|
|
|
|
# print(int(rand($for)));
|
|
|
|
my $k = 0;
|
|
|
|
foreach (@books) {
|
|
|
|
# print $books[$k]{'title'};
|
|
|
|
|
|
|
|
# read in a whole file into a scalar
|
|
my $book = read_file($books[$k]{'file'});
|
|
|
|
my @chapters = split('\nCHAPTER', $book);
|
|
my $test = @chapters;
|
|
# print($test);
|
|
|
|
# number of lines in a page
|
|
my $number_of_lines = 31;
|
|
|
|
my $j = 0;
|
|
my @page_text;
|
|
foreach (@chapters) {
|
|
# print($j);
|
|
my @chapter_lines = split(/\r\n/, $chapters[$j]);
|
|
if ($j != 0){
|
|
$chapter_lines[0] = "<h1>CHAPTER" . $chapter_lines[0] . "</h1>\n\n<p>";
|
|
}
|
|
|
|
my $test2 = @chapter_lines;
|
|
# print ($test2);
|
|
my $i = 0;
|
|
my $line_number = $number_of_lines * $i;
|
|
|
|
while($line_number < $test2) {
|
|
# print($line_number);
|
|
# print($i);
|
|
|
|
# print($chapter_lines[6]);
|
|
my @page = splice(@chapter_lines, 0, $number_of_lines);
|
|
# print($page[22]);
|
|
|
|
$page[0] = "<p>" . $page[0];
|
|
|
|
foreach (@page) {
|
|
|
|
if ($_ eq "") {
|
|
$_ = "</p>\n\n<p>";
|
|
}
|
|
} # foreach page
|
|
|
|
$page_text[$j][$i] = join(" ", @page);
|
|
$page_text[$j][$i] = $page_text[$j][$i] . "</p>\n\n";
|
|
|
|
|
|
$i++;
|
|
$line_number = $number_of_lines * $i;
|
|
|
|
} # while line number
|
|
|
|
$j++;
|
|
|
|
# print($i);
|
|
# $i++;
|
|
# my @chapter_lines = split(/[\n|\r]/, $_);
|
|
# print($chapter_lines[0]);
|
|
|
|
|
|
} # foreach chapters
|
|
|
|
$books[$k]{'pages'} = \@page_text;
|
|
|
|
|
|
$k++;
|
|
|
|
|
|
} # foreach books
|
|
|
|
# my @tst_page = @{$books[$k]{'pages'} };
|
|
|
|
# print($tst_page[12][0]);
|
|
|
|
# my $json_text = encode_json(\%books);
|
|
my $json_text = $json->pretty->encode( \@books );
|
|
|
|
my $js_text = "let book = " . $json_text;
|
|
|
|
# write the file
|
|
open(FH, '>', "test4.js") or die $!;
|
|
print FH $js_text;
|
|
close(FH);
|
|
|
|
|
|
# make a list of 365 pages
|
|
my $for = @books;
|
|
my @page_list;
|
|
my %page_list2;
|
|
my $dt = DateTime->now();
|
|
# print $dt->ymd . "\n";
|
|
for ( my $l; $l < 365; $l++){
|
|
# print $l;
|
|
# $page_list[$l] = $l;
|
|
my $book_list = int(rand($for));
|
|
# print $book_list;
|
|
|
|
# my %book1 = $books[$book_list];
|
|
# print $books[$book_list]{'title'};
|
|
my $chapter_list = @{$books[$book_list]{'pages'}};
|
|
# print $chapter_list;
|
|
my $chapter_select = int(rand($chapter_list));
|
|
# print $chapter_select;
|
|
my $page_list = @{$books[$book_list]{'pages'}[$chapter_select]};
|
|
# print $page_list;
|
|
my $page_select = int(rand($page_list));
|
|
my $page_rand = $books[$book_list]{'pages'}[$chapter_select][$page_select];
|
|
# print $page_rand;
|
|
# my $page_select = int(rand($page_list));
|
|
$page_list[$l] = {
|
|
title => $books[$book_list]{'title'},
|
|
author => $books[$book_list]{'author'},
|
|
page => $page_rand
|
|
};
|
|
|
|
# my $new_test = $dt->add(days => 1)->ymd;
|
|
# print $new_test . "\n";
|
|
|
|
$page_list2{$dt->add(days => 1)->ymd} = {
|
|
title => $books[$book_list]{'title'},
|
|
author => $books[$book_list]{'author'},
|
|
page => $page_rand
|
|
};
|
|
|
|
|
|
# print $dt->add(days => 1)->ymd;
|
|
# my $new_date = $dt->add(days => $l);
|
|
# print $new_date->ymd;
|
|
# for ( my $m; $m < $for; $m++) {
|
|
#
|
|
#
|
|
# } # for m
|
|
#
|
|
} # for l
|
|
|
|
# print scalar keys %page_list2;
|
|
|
|
# print $page_list[55]{'title'};
|
|
#
|
|
|
|
# my $json_text = encode_json(\%books);
|
|
my $json_text2 = $json->pretty->encode( \%page_list2 );
|
|
|
|
my $js_text2 = "let book = " . $json_text2;
|
|
|
|
# write the file
|
|
open(FH, '>', "test5.js") or die $!;
|
|
print FH $js_text2;
|
|
close(FH);
|
|
|
|
|
|
# $books{'pages'} = \@page_text;
|
|
#
|
|
# my @tst_page = @{$books{'pages'} };
|
|
#
|
|
# # print($tst_page[12][0]);
|
|
#
|
|
# # my $json_text = encode_json(\%books);
|
|
# my $json_text = $json->pretty->encode( \%books );
|
|
#
|
|
# my $js_text = "let book = " . $json_text;
|
|
#
|
|
# # write the file
|
|
# open(FH, '>', "test3.js") or die $!;
|
|
# print FH $js_text;
|
|
# close(FH);
|
|
|
|
# print($page_text[12][0]);
|
|
|
|
# print($books{'pages'});
|
|
|
|
# print($chapters[1]);
|
|
# my @chapter_lines = split(/\r\n/, $chapters[1]);
|
|
# $chapter_lines[0] = "<h1>Chapter" . $chapter_lines[0] . "</h1>\n\n<p>";
|
|
|
|
|
|
# my $test2 = @chapter_lines;
|
|
# print ($test2);
|
|
#
|
|
# my $line_number = 31 * $i;
|
|
#
|
|
# while($line_number < $test2) {
|
|
# print($line_number);
|
|
# print($i);
|
|
#
|
|
# # print($chapter_lines[6]);
|
|
# my @page = splice(@chapter_lines, 0, 31);
|
|
# # print($page[22]);
|
|
#
|
|
# foreach (@page) {
|
|
# if ($_ eq "") {
|
|
# $_ = "</p>\n\n<p>";
|
|
# }
|
|
# } # foreach page
|
|
#
|
|
# $page_text[$i] = join("", @page);
|
|
# $page_text[$i] = $page_text[$i] . "</p>\n\n";
|
|
#
|
|
# $i++;
|
|
# $line_number = 31 * $i;
|
|
#
|
|
# }
|
|
#
|
|
# print($page_text[1]); |