spsp

Repository for the "Simplified Perl Status Poster" (SPSP).
git clone git://git.samirparikh.com/spsp
Log | Files | Refs | README

commit d8b2e30c9b4e6995e14e350df8828e9545bd56d5
parent e7086be3cf236985dfa080b8b7bbbb4181189a46
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 26 Jan 2023 16:55:54 +0000

initial commit for spsp

Diffstat:
Aspsp | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 77 insertions(+), 0 deletions(-)

diff --git a/spsp b/spsp @@ -0,0 +1,77 @@ +#!/usr/bin/env perl + +# "Simplified Perl Status Poster" +# +# MIT License +# +# Copyright (c) 2023 Samir Parikh +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +use warnings; +use strict; +use JSON; +use POSIX qw( strftime ); +use feature qw( signatures ); +no warnings qw( experimental::signatures ); + +### declare constants +my $POST_DATA_FILENAME = "/home/compiler/programs/spsp/post_data"; +my $ID_LENGTH = 4; +my $ID = "id"; +my $TITLE = "title"; +my $STATUS = "status"; +my $TIMESTAMP = "timestamp"; + +sub generate_post_id ( $id_length ) +{ + my @characters = (0 .. 9, "a" .. "z"); + return join '', map $characters[ rand @characters ], 1 .. $id_length; +} + +chomp( my $status = do { local $/; <> } ); + +my %post = ( + $ID => generate_post_id( $ID_LENGTH ), + $TITLE => '', + $STATUS => $status, + $TIMESTAMP => strftime "%a %d %b %Y %H:%M %Z", localtime, +); + +#print "id: $post{ $ID }\n"; +#print "status: $post{ $STATUS }\n"; +#print "time: $post{ $TIMESTAMP }\n"; + +my $json = JSON->new; +my $json_post = $json->pretty->encode( \%post ); +print $json_post; + +### Open the data file for concatenation, and die upon failure +open my $post_data_filehandle, '>>', $POST_DATA_FILENAME + or die "Cannot open $POST_DATA_FILENAME for appending: $!\n"; + +### Insert the new post into the file +print $post_data_filehandle $json_post + or die "Error writing post to $POST_DATA_FILENAME: $!\n"; + +### Close the post data file +close $post_data_filehandle or die "Error closing $POST_DATA_FILENAME: $!\n"; + +exit; +