This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
beerxml-parse.pl
executable file
·122 lines (106 loc) · 3.42 KB
/
beerxml-parse.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/bin/perl
#
# Read BeerXML from file or stdin, convert to Speidel recipe
# Optionally upload recipe to Speidel
# Inspired from
# https://forum.braumeisters.net/viewtopic.php?f=11&t=1418&start=30
use strict;
use Getopt::Long;
use XML::LibXML::Simple ();
use Data::Dumper;
use Getopt::Long;
use LWP::UserAgent;
use URI::Escape;
my $debug = 0;
my $recipe_no = 9; # Recipe index
my $speidel; # IP/hostname of speidel
GetOptions
(
'debug' => \$debug,
'recipe_no:i' => \$recipe_no,
'speidel:s' => \$speidel,
);
my $file = shift || '-';
my %options;
my $xs = XML::LibXML::Simple->new(%options);
my $data = $xs->XMLin($file, %options);
print STDERR "Parsed XML:", Dumper $data if $debug;
my $recipe = $data->{RECIPE};
my $recipe_name = $recipe->{NAME};
my $mashin_temp;
my @mash_steps = ([0,0],[0,0],[0,0],[0,0],[0,0]); # Initialize mash steps
my @recipe_mash_steps;
if (ref $recipe->{MASH}->{MASH_STEPS}->{MASH_STEP} eq 'ARRAY') {
@recipe_mash_steps = @{$recipe->{MASH}->{MASH_STEPS}->{MASH_STEP}};
$mashin_temp = $recipe->{MASH}->{MASH_STEPS}->{MASH_STEP}->[0]->{STEP_TEMP};
} elsif ($recipe->{MASH}->{MASH_STEPS}->{MASH_STEP}) {
@recipe_mash_steps = $recipe->{MASH}->{MASH_STEPS}->{MASH_STEP};
$mashin_temp = $recipe->{MASH}->{MASH_STEPS}->{MASH_STEP}->{STEP_TEMP};
} elsif (ref $recipe->{MASH}->{MASH_STEP} eq 'ARRAY') {
@recipe_mash_steps = @{$recipe->{MASH}->{MASH_STEP}};
$mashin_temp = $recipe->{MASH}->{MASH_STEP}->[0]->{STEP_TEMP};
} else {
@recipe_mash_steps = $recipe->{MASH}->{MASH_STEP};
$mashin_temp = $recipe->{MASH}->{MASH_STEP}->{STEP_TEMP};
}
my $i = 0;
die "No mash steps found" unless (scalar @recipe_mash_steps > 0);
foreach my $mash (@recipe_mash_steps) {
next if (defined $mash->{TYPE}
and
(
$mash->{TYPE} ne 'Infusion' and
$mash->{TYPE} ne 'Sparge'));
$mash_steps [$i++] = [$mash->{STEP_TEMP}, $mash->{STEP_TIME}];
}
my $mash_steps_string = join '', map {sprintf "X%dX%d", $_->[0], $_->[1]} @mash_steps;
my $boil_time = $recipe->{BOIL_TIME} || 60;
my $boil_temp = 100;
my @hop_additions = (0,0,0,0,0,0); # Initialize hop additions
$i = 0;
my @hop_steps;
if (ref $recipe->{HOPS}->{HOP} eq 'ARRAY') {
@hop_steps = @{$recipe->{HOPS}->{HOP}};
} else {
@hop_steps = $recipe->{HOPS}->{HOP};
}
# Herb additions
if (ref $recipe->{MISCS}->{MISC} eq 'ARRAY') {
push @hop_steps, @{$recipe->{MISCS}->{MISC}};
} else {
push @hop_steps, $recipe->{MISCS}->{MISC};
}
foreach my $hop (sort {$b->{TIME} <=> $a->{TIME}} @hop_steps) {
next unless ($hop->{USE} eq 'Boil');
next if grep {$hop->{TIME} == $_} @hop_additions; # Don't repeat ourselves
@hop_additions[$i++] = $hop->{TIME};
}
my $hop_steps_string = join '', map {sprintf "X%d", $_} @hop_additions;
$recipe_name =~ s/ /_/g;
my $recipe_string = sprintf "%dX%d%sX%dX%d%s.%s",
$recipe_no,
$mashin_temp,
$mash_steps_string,
$boil_time,
$boil_temp,
$hop_steps_string,
$recipe_name;
#uri_escape($recipe_name);
printf "Recipe:\n$recipe_string\n" if $debug;
if ($speidel) {
my $ua = LWP::UserAgent->new;
my $response = $ua->post
(
'http://'.$speidel.'/rz.txt',
{
rz => $recipe_string,
},
);
if ($response->is_success) {
print $response->decoded_content; # or whatever
} else {
die $response->status_line;
}
} else {
print "$recipe_string\n";
}