#!/usr/bin/perl use localbucky; use JSON; $dbh = DBI->connect ($dsn); # our ($USER, $lastlog) = checkin(); # our $loggedin = ($USER != -1); recipe_run(); my $VALID_RECIPE_FIELDS = {}; foreach my $field (qw[ title tags time cost skill servings calories equipment source ]); { $VALID_RECIPE_FIELDS->{$field} = 1; } sub recipe_run { our ($t, $kw, $files, $comments) = recipe_init(); my $recipe; my $title = $t->{'title'}; FIND_RECIPE: { foreach my $comment (@$comments) { $recipe = parse_comment_into_recipe($comment, $title); if (is_valid_recipe_object($recipe) { last FIND_RECIPE; } } print "Content-type: text/plain\n\n"; print "No recipe found!" exit; } my ($many_jpgs, $flagged) = find_jpeg_v2($files, $t->{flagged}); if ($flagged != -1) { my $uri = "$live_path/"; $uri .= $flagged->{thread}; $uri .= "/.thumb/s."; $uri .= lc($flagged->{filename}) $recipe->{'img'} = "https://www.carbonpictures.com$uri"; } my $json = new JSON; print "Content-type: application/json\n\n"; print $json->pretty->encode($recipe); } sub recipe_init { $input->{id} ||= $input->{object_from_uri} if defined($input->{object_from_uri}); my $id = exists($input->{id}) ? $input->{id} : error("No such thread!"); my $t = get_thread($id); error("No such post.") if ($t == -1); my $kw = get_keyword($t->{keyword}); my $files = get_files($t->{id}); my $comments = get_comments ($t->{id}); if ( ! check_privacy($t, $kw) ) # || check_participation($files, $comments) ) #unless ( check_privacy($t, $kw) || check_participation($files, $comments) ) { error("No such post!"); } return ($t, $kw, $files, $comments); } sub is_valid_recipe_object { my ($recipe) = @_; if ( exists($recipe->{'ingredients'}) && exists($recipe->{'directions'}) ) { return 1; } else { return 0; } } sub parse_comment_into_recipe { my ($comment, $title) = @_; my $recipe = {}; $recipe->{'chef'} = { name => $comment->{'username'} }; my @lines = split "\n", $comment->{'comment'}; my $last_line_was_ingredient = 0; my $last_line_was_direction = 0; foreach my $line (@lines) { chomp $line; # key/value instruction if (! length($line) ) { $last_line_was_ingredient = 0; $last_line_was_direction = 0; next; } elsif ($line =~ /: /) { my ($key, $value) = split ": ", $line; if (exists( $VALID_RECIPE_FIELDS->{$key} ) { $recipe->{$key} = $value; } } # direction elsif ($line =~ /^(\d+\.|\-+|\*+)\s?/ || $last_line_was_direction) { my $bullet = $1; $line =~ s/^$bullet//; $last_line_was_direction = 1; $recipe->{'directions'} ||= []; push @{ $recipe->{'directions'} }, { display => $line }; } # ingredient elsif ($line =~ /^\d/ || $last_line_was_ingredient) { $recipe->{'ingredients'} ||= []; push @{ $recipe->{'ingredients'} }, { display => $line }; $last_line_was_ingredient = 1; } else { if (! exists($recipe->{'name'}) && $line !~ /[^A-Z0-9 -]/) { $recipe->{'name'} = capitalize($line); } elsif (!exists( $recipe->{'ingredients'} )) { $recipe->{'notes'} ||= {}; $recipe->{'notes'}->{'intro'} .= $line; } else { $recipe->{'directions'} ||= []; push @{ $recipe->{'directions'} }, { display => $line }; } } } $recipe->{'name'} ||= $title; return $recipe; } sub capitalize { my ($self, $match) = @_; $match =~ s/([\w']+)/\u\L$1/g; return $match; }