Skip to content

Instantly share code, notes, and snippets.

@zQueal
Created June 26, 2014 00:23
Show Gist options
  • Save zQueal/6427b241c0eda321065b to your computer and use it in GitHub Desktop.
Save zQueal/6427b241c0eda321065b to your computer and use it in GitHub Desktop.
Calculate eJuice recipes from CLI. Made By: http://www.reddit.com/user/j_hornsties
#!/usr/bin/perl -w
# Caclulate Volumes for ejuice
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use List::Util qw( sum );
use Getopt::Std;
my %opts;
my $flavorRef = {};
my $flavorVols = {};
my $totalVol; # Total Desired Volume
my $resultNic; # Total Desired Nicotine Level in mg/ml
my $nicBase; # Nicotine Base Solution Level in mg/ml
my $flavorVolTotal; # total ml of all flavors combined
my $pgPct = 0; # Percent PG for Batch
my $pgMl = 0;
my $nicBasePg = 0; # bool for nicotine base pg or vg
my $nicBaseType = 'VG'; # Nicotine-Base type, defualt VG
getopts('v:n:c:f:l:p:Ph',\%opts) or usage();
usage() unless (%opts);
parseOpts(\%opts);
### Formulas ###
# Total Volume of Nicotine Base solution
my $nicBaseVol = ($resultNic * $totalVol) / $nicBase;
if ($nicBaseVol =~ /(\d+)\.(\d{3,})/) {
my $dec = substr($2,0,2);
$nicBaseVol = $1 . '.' . $dec;
}
my $nicBaseVolPct = ($nicBaseVol / $totalVol) * 100;
# Total Volume of flavorings
while ( my ($kFlavor, $vPct) = each %$flavorRef ) {
# Convert % to ml for each flavor
my $vDec = $vPct / 100;
my $flavorMl = $vDec * $totalVol;
$flavorVols->{$kFlavor} = $flavorMl;
}
$flavorVolTotal = sum values %$flavorVols;
my $flavorVolPct = ($flavorVolTotal / $totalVol) * 100;
# PG
if ($pgPct != 0) {
my $pgDec = $pgPct / 100;
$pgMl = $pgDec * $totalVol;
}
# Total VG to use
my $resultVG = $totalVol - ($nicBaseVol + $flavorVolTotal + $pgMl);
my $resultVGPct = ($resultVG / $totalVol) * 100;
if ($nicBasePg == 1 && $pgPct != 0) {
$pgMl = $pgMl - $nicBaseVol;
$pgPct = ($pgMl / $totalVol) * 100;
}
printf("%0s\n\n", "juiceCalc.pl v0.2 Beta");
printf("%-22s %-15s\n", "Batch Size:", "$totalVol ml");
printf("%-22s %-15s\n", "Batch Nicotine Level:", "$resultNic mg/ml");
printf("%-22s %-15s\n", "Batch PG Ratio:", "${pgPct}%");
printf("%-22s %-15s\n", "Nicotine-Base Level:", "$nicBase mg/ml ($nicBaseType)");
printf("%-22s %-15s\n\n", 'Flavorings Total', "$flavorVolPct ml (${flavorVolPct}%)");
printf("\n%0s\n", "Results:");
printf("%0s\n", '================================================');
printf("%-1s %-18s %-15s %-9s %0s\n", "|", 'Item', 'Volume (ml)', 'Percent', "|");
printf("%0s\n", '================================================');
printf("%-1s %-18s %-15s %-9s %0s\n", "|", 'Nicotine-Base', $nicBaseVol, "${nicBaseVolPct}%", "|");
printf("%-1s %-18s %-15s %-9s %0s\n", "|",'VG Needed', $resultVG, "${resultVGPct}%", "|");
printf("%-1s %-18s %-15s %-9s %0s\n", "|",'PG Needed', $pgMl, "${pgPct}%", "|");
while ( my ($kFlavor, $vVol) = each %$flavorVols ) {
printf("%-1s %-18s %-15s %-9s %0s\n", "|", $kFlavor, $vVol, "$flavorRef->{$kFlavor}%", "|");
}
printf("%0s\n\n", '================================================');
sub parseOpts {
my $ref = shift;
if ($ref->{h}) {
usage();
}
if (int($ref->{v})) {
$totalVol = $ref->{v};
} else {
print "ERROR: Invalid or Missing Desired Volume!\njuicecalc.pl -h to see help.\n";
exit 1;
}
if (int($ref->{n})) {
$resultNic = $ref->{n};
} else {
print "ERROR: Invalid or Missing Desired Nicotine Level!\njuicecalc.pl -h to see help.\n";
exit 1;
}
if (int($ref->{c})) {
$nicBase = $ref->{c};
} else {
print "ERROR: Invalid or Missing Nicotine-Base Solution Concentration!\njuicecalc.pl -h to see help.\n";
exit 1;
}
if ($ref->{f}) {
parseFlavors($ref->{f});
} else {
parseFlavors('None-0');
}
if ($ref->{P}) {
$nicBasePg = 1;
$nicBaseType = 'PG';
}
if ($ref->{p}) {
$pgPct = int($ref->{p});
if ($pgPct > 100) {
print "ERROR: PG Cannot Exceed 100%\n";
exit 1;
}
}
return;
}
# flavor-<pct>
sub parseFlavors {
my $in = shift;
# Parse comma separated list
my @list = split(',',$in);
foreach (@list) {
if ($_ !~ /^(\w+)-(\d+)$/) {
print "Invalid Flavor Specification: $_\n";
exit 1;
}
my ($flavorName, $flavorPct) = split('-',$_);
$flavorRef->{$flavorName} = $flavorPct;
}
}
sub usage {
print "\n";
print STDERR << 'EOF';
Juice Calc v0.2 Beta
Usage:
juicecalc.pl
Required Options
-v : Total Desired Volume in ml
-n : Total Desired Nicotine Level in mg/ml
-c : Nicotine-Base Solution Nicotine Level in mg/ml
Other Options
-f <list> : Flavor List. See "Flavor List" Below
-P : If Nicotene-Base Solution is PG Based (Default VG Base)
-p : Desired Percent Propylene Glycol
-o <filename> : Save recipe to file
-h : This Help
Flavor List
The Flavor List is a comma separated list of flavor names
and the ratio of flavor volumes in percent.
The format is 'flavorname-percent', where flavorname is a
descriptive name and percent is an integer separated by a hyphen.
Example Recipe:
- 50ml Batch, 6 mg/ml Desired Strength, 36 mg/ml Nicotine-Base
- 5% Strawberry
- 5% Banana
perl juicecalc.pl -v 50 -n 6 -c 36 -f strawberry-5,banana-5
EOF
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment