Ver código fonte

Implement new Amica site for Minerva

Toni Fadjukoff 10 anos atrás
pai
commit
e565dee22b
1 arquivos alterados com 37 adições e 198 exclusões
  1. 37 198
      amica.pl

+ 37 - 198
amica.pl Ver arquivo

@@ -1,25 +1,11 @@
1 1
 use vars qw(@day_names);
2
-
3
-# erkkeri is gone - could remove a lot of this ugliness
2
+use JSON;
4 3
 
5 4
 my $erkkeri_title = "(TTY) Amica Erkkeri";
6 5
 my @restaurant_info = (
7
-  [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle" ]
8
-  #[ $erkkeri_title, "http://www.amica.fi/erkkeri", "", "left" ]
6
+  [ "(TaY) Amica Minerva", "http://www.amica.fi/minerva", "", "middle", "http://www.amica.fi/modules/json/json/Index?costNumber=0815" ]
9 7
 );
10 8
 
11
-my ($parse_func, $day_id, $week, $erkkeri);
12
-my (@cur_day_foods, @week_foods);
13
-
14
-sub amica_parse_to_eof {
15
-}
16
-
17
-sub amica_finish_day {
18
-  push @week_foods, [@cur_day_foods];
19
-  @cur_day_foods = ();
20
-  $day_id = $day_id + 1;
21
-}
22
-
23 9
 sub utf8_to_8859 {
24 10
   $_ = shift;
25 11
 
@@ -30,208 +16,61 @@ sub utf8_to_8859 {
30 16
   return $_;
31 17
 }
32 18
 
33
-sub amica_parse_line {
34
-  my $text = shift;
35
-  
36
-  $text =~ tr/\r\n\t/   /;
37
-  $text =~ s/( | )*$//;
38
-  $text =~ s/ *(\d+,\d+ *\/ *)?\d+,\d+ *$//;
39
-  $text =~ s/^( | )*//;
40
-  $text =~ s/( | )*$//;
41
-  $text =~ s/valinnan mukaan$//;
42
-  if ($text =~ /^(.*) \(([^\)]+)\)$/) {
43
-    my ($name, $allergy) = ($1, $2);
44
-    $name =~ s/( )*$//;
45
-    $allergy =~ s/\*veg\./ eriks: Ve/g;
46
-    $allergy =~ s/veg/Ve/gi;
47
-    $allergy =~ s/\*([A-Z]+)/ eriks: $1/;
48
-    return "$name ($allergy)\n";
49
-  } elsif ($text ne "") {
50
-    return "$text\n";
51
-  }
52
-  return "";
53
-}
54
-
55
-sub amica_parse_split {
56
-  my $text = shift;
57
-  
58
-  my $food = "";
59
-  for (;;) {
60
-    $i = index($text, ")");
61
-    last if ($i == -1);
62
-    
63
-    my $line = substr($text, 0, $i + 1);
64
-    $food .= amica_parse_line($line);
65
-    $text = substr($text, $i + 1);
66
-  }
67
-  $food .= amica_parse_line($text);
68
-  push @cur_day_foods, $food if ($food ne "");
69
-}
70
-
71
-sub amica_parse_more_food {
72
-  my $token = shift;
73
-  
74
-  if ($token->[0] eq 'T') {
75
-    my $text = utf8_to_8859($token->[1]);
76
-    my $next_day_name = $day_names[$day_id+1];
77
-    
78
-    if ($day_id < 6 && $text =~ /^$next_day_name\b/i) {
79
-      # day changed
80
-      amica_finish_day();
81
-    } else {
82
-      amica_parse_split($text);
83
-    }
84
-  } elsif ($token->[0] eq 'S' && $token->[1] eq 'h2') {
85
-    amica_finish_day();
86
-    $parse_func = \&amica_parse_to_eof;
87
-  }
88
-}
89
-
90
-sub amica_parse_first_day {
91
-  my $token = shift;
92
-  
93
-  if ($token->[0] eq 'T') {
94
-    for ($day_id = 0; $day_id < 7; $day_id++) {
95
-      my $dayname = $day_names[$i];
96
-      last if ($token->[1] =~ /^$dayname\b/i);
97
-    }
98
-    $day_id = 0 if ($day_id == 7);
99
-    $parse_func = \&amica_parse_more_food;
100
-  }
101
-}
102
-
103
-sub amica_parse_to_start {
104
-  my $token = shift;
105
-  
106
-  if ($token->[0] eq 'S' && $token->[1] eq 'p') {
107
-    $parse_func = \&amica_parse_first_day;
108
-  }
109
-}
110
-
111
-sub get_week {
112
-  my ($mday, $mon) = @_;
113
-
114
-  my @l = localtime;
115
-  my @l2 = (0, 0, 0, $mday, $mon-1, $l[5], 0, 0, -1);
116
-  @l = localtime(mktime(@l2));
117
-  return strftime("%V", @l);
118
-}
119
-
120
-sub amica_parse_date {
121
-  my $token = shift;
122
-  
123
-  if ($token->[0] eq 'T') {
124
-    my $text = $token->[1];
125
-    if ($text =~ /^(\d\d\d\d)-(\d?\d)-(\d?\d) /) {
126
-      my ($mday, $mon) = ($3, $2);
127
-      $week = get_week($mday, $mon);
128
-    }
129
-  } elsif ($token->[0] eq 'E' && $token->[1] eq 'h2') {
130
-    $parse_func = \&amica_parse_to_start;
131
-  }
132
-}
133
-
134
-sub amica_parse_to_date {
135
-  my $token = shift;
136
-  
137
-  if ($token->[0] eq 'S' && $token->[1] eq 'h2') {
138
-    my %attrs = %{$token->[2]};
139
-    if ($attrs{'id'} =~ /HeadingMenu/) {
140
-      $parse_func = \&amica_parse_date;
141
-    }
142
-  }
143
-}
144
-
145 19
 sub parse_amica {
146 20
   my ($fname, $info_ref) = @_;
147
-  my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
148
-  my $title = @{$info_ref}[0];
21
+  open(FILE, $fname);
22
+  my $json = do { local $/; <FILE> };
23
+  close(FILE);
149 24
   
150
-  $week = "";
151
-  $day_id = 0;
152
-  @cur_day_foods = ();
153
-  @week_foods = ();
154
-
155
-  $parse_func = \&amica_parse_to_date;
156
-  while (my $token = $p->get_token) {
157
-    &$parse_func($token);
158
-  }
159
-  return [ $title, "", $week, [ @week_foods ], $info_ref ];
160
-}
161
-
162
-sub parse_amica_get_finnish_url {
163
-  my ($fname) = @_;
164
-  my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
165
-
166
-  my @l = localtime;
167
-  my $this_week = strftime("%V", @l);
168
-
169
-  my $state = 0;
170
-  my $last_url = "";
171
-  my $week = "";
172
-  while (my $token = $p->get_token) {
173
-    if ($token->[0] eq 'S' && $token->[1] eq 'meta') {
174
-      my %attrs = %{$token->[2]};
175
-      if ($attrs{'name'} eq "TITLE") {
176
-        if ($attrs{'content'} =~ /(\d+)\.(\d+)\.? *- *(\d+)\.(\d+)/) {
177
-          my ($mday, $mon) = ($1, $2);
178
-          $week = get_week($mday, $mon);
179
-        } elsif ($attrs{'content'} =~ /(\d+)\.? *- *(\d+)\.(\d+)/) {
180
-          my ($mday, $mon) = ($1, $3);
181
-          $week = get_week($mday, $mon);
182
-        }
183
-	return "" if ($attrs{'content'} !~ /English/i && $week == $this_week);
25
+  my $title = @{$info_ref}[0];
26
+  my $week = `date +%V`;
27
+  my @cur_day_foods = ();
28
+  my @week_foods = ();
29
+
30
+  my $data = from_json($json);
31
+  my $MenusForDays = $data->{'MenusForDays'};
32
+  foreach my $MenuForDay (@$MenusForDays) {
33
+    my $SetMenus = $MenuForDay->{'SetMenus'};
34
+    foreach my $SetMenu (@$SetMenus) {
35
+      my $Components = $SetMenu->{'Components'};
36
+      foreach my $Component (@$Components) {
37
+        $cur_food .= "\n" if $cur_food ne "";
38
+        $cur_food .= $Component;
184 39
       }
185
-    } elsif ($token->[0] eq 'S' && $token->[1] eq 'a') {
186
-      my %attrs = %{$token->[2]};
187
-      $last_url = $attrs{'href'};
188
-    } elsif ($token->[0] eq 'T' && $token->[1] =~ /^ruokalista (\d+)\.(\d+)\.? *- *(\d+)\.(\d+)/i) {
189
-      my ($mday, $mon) = ($1, $2);
190
-      $week = get_week($mday, $mon);
191
-      return $last_url if ($week == $this_week);
192
-    } elsif ($token->[0] eq 'T' && $token->[1] =~ /^ruokalista (\d+)\.? *- *(\d+)\.(\d+)/i) {
193
-      my ($mday, $mon) = ($1, $3);
194
-      $week = get_week($mday, $mon);
195
-      return $last_url if ($week == $this_week);
196
-    } elsif ($token->[0] eq 'T' && $token->[1] =~ /^ruokalista (viikko|vko) (\d+)/i) {
197
-      $week = $2;
198
-      return $last_url if ($week == $this_week);
40
+      print $cur_food . "\n";
41
+      push @cur_day_foods, utf8_to_8859($cur_food) if ($cur_food ne "");
42
+      $cur_food = "";
199 43
     }
44
+    push @week_foods, [@cur_day_foods];
45
+    @cur_day_foods = ();
200 46
   }
201
-  return "";
202
-}
203 47
 
204
-sub parse_amica_url {
205
-  my ($fname) = @_;
206
-  my $p = HTML::TokeParser->new($fname) or die("Can't open file $fname");
207
-  
208
-  my $state = 0;
209
-  while (my $token = $p->get_token) {
210
-    if ($token->[0] eq 'S') {
211
-      my %attrs = %{$token->[2]};
212
-      if ($token->[1] eq 'td' && $attrs{'title'} eq 'Ruokalistat' && $state == 0) {
213
-	$state = 1;
214
-      } elsif ($token->[1] eq 'a' && $state == 1) {
215
-	return $attrs{'href'};
216
-      }
217
-    }
218
-  }
219
-  return "";
48
+  return [ $title, "", $week, [ @week_foods ], $info_ref ];
220 49
 }
221 50
 
222 51
 sub get_amica_restaurant {
223 52
   my $use_old = shift;
224 53
   my $count = 0;
225 54
   my @restaurants = ();
55
+  my $weekDay = `date +%w`;
56
+  chomp($weekDay);
57
+  my $first_day = $weekDay == 1 
58
+    ? `date --date="today" +%Y-%m-%d`
59
+    : `date --date="last monday" +%Y-%m-%d`;
60
+  chomp($first_day);
61
+  my $last_day = $weekDay == 0
62
+    ? `date --date="today" +%Y-%m-%d`
63
+    : `date --date="next sunday" +%Y-%m-%d`;
64
+  chomp($last_day);
226 65
   foreach my $i (@restaurant_info) {
227 66
     my @info = @{$i};
228 67
     my $temp_fname = "amica$count.temp.html";
229
-    my $url = $info[1];
68
+    my $url = "${info[4]}&firstDay=$first_day&lastDay=$last_day&language=fi";
230 69
     if (!-f $temp_fname || !$use_old) {
231 70
       system("wget -q --timeout=10 -O $temp_fname.tmp '$url' && mv $temp_fname.tmp $temp_fname") if ($url ne "");
232 71
     }
233 72
     if (-f $temp_fname) {
234
-      $info[1] = $url;
73
+      $info[4] = $url;
235 74
       push @restaurants, parse_amica($temp_fname, \@info);
236 75
     }
237 76
     $count++;