Last active
January 1, 2016 11:29
-
-
Save thomaswardiii/8138707 to your computer and use it in GitHub Desktop.
Bacon ipsum API command line script. Help and ~/.bacon coming soon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use HTTP::Tiny; | |
use JSON; | |
use Getopt::Long; | |
use Config::Simple ('-lc'); | |
use File::HomeDir; | |
my $BASE_URL = "http://baconipsum.com/api/?"; | |
my $paras = 0; | |
my $sentences = 0; | |
# bools | |
my $ipsum = 0; | |
my $meat = 0; | |
my $filler = 0; | |
# Windows ini style file! must be in [bacon] section | |
Config::Simple->import_from(File::HomeDir->my_home . "/.bacon", \%Config); | |
if (exists($Config{'bacon.paragraphs'})) | |
{ | |
$paras = $Config{'bacon.paragraphs'}; | |
} | |
if (exists($Config{'bacon.sentences'})) | |
{ | |
$sentences = $Config{'bacon.sentences'}; | |
} | |
if (exists($Config{'bacon.ipsum'})) | |
{ | |
$ipsum = 1; | |
} | |
if (exists($Config{'bacon.filler'})) | |
{ | |
$filler = 1; | |
$meat = 0; | |
} | |
if (exists($Config{'bacon.meat'})) | |
{ | |
$meat = 1; | |
$filler = 0; | |
} | |
GetOptions( | |
"paragraphs|p=i" => \$paras, | |
"sentences|s=i" => \$sentences, | |
"ipsum|i" => sub { $ipsum = 1 }, | |
"filler|f" => sub { $filler = 1; $meat = 0; }, | |
"meat|m" => sub { $meat = 1; $filler = 0; }, | |
"help|h" => sub { HelpMessage() } | |
); | |
$url = $BASE_URL . "type=" . | |
($filler ? "meat-and-filler" : "all-meat") . | |
($paras > 0 ? "¶s=$paras" : "") . | |
($sentences > 0 ? "&sentences=$sentences" : "") . | |
($ipsum ? "&start-with-lorem=1" : ""); | |
$http = HTTP::Tiny->new(); | |
$response = $http->get($url); | |
if ($response->{success}) | |
{ | |
$json = JSON->new(); | |
@json_data = @{$json->utf8->decode($response->{content})}; | |
foreach (@json_data) | |
{ | |
print $_ . "\n\n"; | |
} | |
} | |
else | |
{ | |
print "Unable to get bacon! Status: ", $response->{status}, "\n"; | |
print "Reason: ", $response->{reason}, "\n"; | |
} | |
sub HelpMessage() | |
{ | |
print "Usage: $0 (-m|--meat)|(-f|--filler) [-p|--paragraphs #] [-s|--sentences #] [-l|--lorem] [-h|--help]\n\n"; | |
print "-m|--meat: All Meat (default)\n"; | |
print "-f|--filler: Meat and Filler\n"; | |
print "-p|--paragraphs #: Number of paragraphs (default: 5)\n"; | |
print "-s|--sentences #: Number of sentences (overrides paragraphs)\n"; | |
print "-i|--ipsum: Start with 'Bacon ipsum dolor sit amet'\n"; | |
print "-h|--help: This help\n"; | |
exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment