summaryrefslogtreecommitdiff
path: root/cgi-bin/recipe
blob: 4f134c091a19115895d6d1f6bcbe569eb1feae20 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/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;
  }