/
lunchguiden.go
402 lines (347 loc) · 14.4 KB
/
lunchguiden.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Author: Rickard Andersson
rickard@0x539.se
twitter, github, 4sq: rickard2
http://0x539.se
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, either version 3 of the License, or
(at your option) any later version.
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/>
This is a simple application for downloading and parsing information from
a local Swedish lunch menu system called Lunchguiden (http://service.dt.se/lunch/lunch.asp)
It's used as part of a Android application to server clean and formatted
JSON data to the device, thus taking away the nasty work of parsing the data
from the Android device.
100809: First version ready for use. It took about three days to finish.
*/
package main
import (
"http"
"fmt"
"os"
"io/ioutil"
"log"
"regexp"
"strings"
"flag"
"crypto/md5"
"json"
"bytes"
)
// Three structs needed for JSON output
//
type DataStruct struct {
City string;
Week int;
Days [5]DayData;
}
type DayData struct {
Day int;
Name string;
Restaurants []RestData;
}
type RestData struct {
Name string;
ImageUrl string;
Description string;
Menu string;
}
// Input values
//
var url = flag.String("url", "", "URL to lunchguiden");
var out = flag.String("out", "", "Output file");
var city = flag.String("city", "", "Textual representation of the city");
var week = flag.Int("week", 0, "What week number to download");
func main() {
var (
res *http.Response;
err os.Error;
inData []byte;
)
// Textual names of the weekdays, needed for URL generation
//
weekdays := []string{ "Mandag", "Tisdag", "Onsdag", "Torsdag", "Fredag" };
// Parse and validate input
//
flag.Parse();
if *url == "" {
fmt.Println("ERROR: No URL specified");
flag.PrintDefaults();
return;
}
if *out == "" {
fmt.Println("ERROR: No output file specified");
flag.PrintDefaults();
return;
}
if *city == "" {
fmt.Println("ERROR: No city specified");
flag.PrintDefaults();
return;
}
if *week == 0 {
fmt.Println("ERROR: No week specified");
flag.PrintDefaults();
return;
}
// Beginning of the JSON data structure creation with
// basic information about this particular menu
//
jsonData := new(DataStruct);
jsonData.City = *city;
jsonData.Week = *week;
fmt.Printf("Downloading information for %s and week %i\n", *city, *week);
// Iterates all weekdays
//
for day := 0; day < 5; day++ {
// Downloads the current menu from the web
// NOTE: week variable in URL must be provided from the input
//
res, _, err = http.Get(fmt.Sprintf("%s&veckodag=%s", *url, weekdays[day]));
// No error, continue reading HTML data into the inData variable
//
if err == nil {
fmt.Printf("OK, response is %s length is %i\n", res.Status, res.ContentLength);
inData, err = ioutil.ReadAll(res.Body);
res.Body.Close();
} else {
log.Println(err);
}
// No error reading data from the HTML document, continue building the
// JSON data structure with current day and parse the HTML data
//
if err == nil {
jsonData.Days[day].Day = day;
jsonData.Days[day].Name = weekdays[day];
jsonData.Days[day].Restaurants = Parse(inData);
} else {
log.Println(err);
}
}
// Generate the JSON code from the data structure
//
var output = bytes.NewBuffer(make([]byte, 0));
var jsonOutput, _ = json.Marshal(jsonData);
json.Indent(output, jsonOutput, "", ""); // Indent with no prefix
// Convert the JSON data from *Buffer to a []byte slice
//
var outData = make([]byte, output.Len());
var n,_ = output.Read(outData);
// Compute the md5 hash value of the JSON data
//
var hashStr, hash = GenerateHash(outData);
fmt.Printf("MD5 is: %s\n", hashStr);
// Write JSON data to output file
//
fmt.Printf("Writing %i bytes to %s\n", n, *out);
err = ioutil.WriteFile(*out, outData, 0644);
if err != nil {
log.Println(err);
}
// Write MD5 hash to file
//
fmt.Printf("Writing md5 sum\n");
err = ioutil.WriteFile(fmt.Sprintf("%s.md5", *out), hash, 0644);
if err != nil {
log.Println(err);
}
}
// Function for parsing out the real information from the HTML document
//
func Parse(in []byte) ([]RestData) {
// Setup a string to work with and some regular expressions
//
var strData = string(in);
var rx_image = regexp.MustCompile("SRC=\"([^\"]+)\"");
var rx_text = regexp.MustCompile("<center>(.+)</center>");
var rx_html = regexp.MustCompile("<[^>]+>");
// The HTML document is split into sections of one resturant per
// item in the tds slice.
//
var tds = strings.Split(strData, "<TD WIDTH=\"130\" ALIGN=\"CENTER\" VALIGN=\"TOP\" BGCOLOR=\"#FFFFFF\">", -1);
// Create RestData slice to add restaurant data to, length is computed
// from the number of items in the tds slice.
// NOTE: Minus one is needed because tds[0] represent the data _before_
// The first restaurant entry appears.
//
var restaurant = make([]RestData, (len(tds) - 1));
// Iterate all restaurants from the HTML document
//
for i := 1; i < len(tds); i++ {
// Various regular expressions and splits in order to parse out
// the important information from all the junky HTML parts.
//
var image = rx_image.FindAllStringSubmatch(tds[i], -1);
var name = MatchRestaurant(image[0][1]);
var tmpText = rx_text.FindAllString(tds[i], -1);
var tmpMenu0 = strings.Split(tds[i], "<TD WIDTH=\"311\" VALIGN=\"TOP\" BGCOLOR=\"#FFFFFF\"><IMG SRC=\"../grafik/space.gif\" BORDER=0 width=\"1\" HEIGHT=\"5\">", -1);
var tmpMenu1 = strings.Split(tmpMenu0[1], "</TD>", -1);
var menu = strings.Replace(tmpMenu1[0], "<LI>", "* ", -1);
// Replace all <br> and <br/> tags with newlines instead (\n)
//
menu = strings.Replace(menu, "<BR>", "\n", -1);
menu = strings.Replace(menu, "<br/>", "\n", -1);
// Compute real index and add restaurant data to the RestData slice
//
var index = i - 1;
restaurant[index].ImageUrl = fmt.Sprintf("http://service.dt.se/lunch/%s", image[1]);
restaurant[index].Name = name;
restaurant[index].Menu = strings.TrimSpace(menu);
// If a "subtext" or description is found (the short text beneath
// the image in the menu). Parse out all HTML from it and save it
// to the RestData slice
//
if len(tmpText) > 0 {
var text = rx_html.ReplaceAllString(tmpText[0], " ");
restaurant[index].Description = text;
} else {
restaurant[index].Description = "";
}
}
return restaurant;
}
// Function for trying to determine the name of the current restaurants
// (Since that information isn't avalible on the web, only in the images)
//
func MatchRestaurant(in string) string {
// Very large two dimensional string slice for matching the
// image with the real name of the restaurant
// TODO: Implement with a HashTable
//
arr := [][]string {
// Falun
[]string { "lunchlogo/club-etage.gif", "Club Etage" },
[]string { "lunchlogo/chinathai.gif", "Restaurang China Thai" },
[]string { "lunchlogo/hemkop.gif", "Hemköp" },
[]string { "lunchlogo/LugnetMatEvent.gif", "Lugnet Mat & Event" },
[]string { "lunchlogo/Z-KROG.gif", "Z-krog" },
[]string { "lunchlogo/City_Life.gif", "City Life" },
[]string { "lunchlogo/geschwornergarden_09.gif", "Geschwornergärden" },
[]string { "lunchlogo/Gamla-staberg-2010.gif", "Gamla Staberg" },
[]string { "lunchlogo/koppis.gif", "Restaurang Koppis" },
[]string { "lunchlogo/carianna.gif", "Restaurang Cari Anna" },
[]string { "lunchlogo/marianns_05.gif", "Mariann's Saloon" },
[]string { "lunchlogo/dalasalen_dalreg.gif", "Dalasalen" },
[]string { "lunchlogo/kuselska-rappans.gif", "Küselska Krogen" },
[]string { "lunchlogo/framby-udde-2.gif", "Runns aktivitetscenter" },
[]string { "lunchlogo/hammars.gif", "Hammars" },
[]string { "lunchlogo/Restaurang_Chapeau_dor.gif", "Chapeau d'or" },
[]string { "lunchlogo/ah.gif", "Åh" },
[]string { "lunchlogo/haganas.gif", "Haganäs" },
[]string { "lunchlogo/Pitchers.gif", "Pitchers" },
[]string { "lunchlogo/Dossbergets-vardshus.gif", "Dössbergets värdshus" },
[]string { "lunchlogo/trotzgatan3.gif", "Trotzgatan 3" },
[]string { "lunchlogo/Victuscella.gif", "Victuscella" },
[]string { "lunchlogo/Scandic_lugnet.gif", "Scandic" },
[]string { "lunchlogo/HettoVilt.gif", "Hett & Vilt" },
[]string { "lunchlogo/Yrkesakademin.gif", "Yrkesakademin" },
// Borlange
[]string { "lunchlogo/BlgHV.gif", "Borlänge Hotel & Värdshus" },
[]string { "lunchlogo/liljan.gif", "Restaurang Liljan" },
[]string { "lunchlogo/Tzatziki-blge.gif", "Tzatziki" },
[]string { "lunchlogo/thai-o-sushi.gif", "Restaurang Thai & Sushi" },
[]string { "lunchlogo/Dalaflyget.gif", "Dalaflyget" },
[]string { "lunchlogo/subway.gif", "Subway" },
[]string { "lunchlogo/buskakersgastgiv.gif", "Buskåkers Gästgifvargård" },
[]string { "lunchlogo/matpalatset.gif", "Matpalatset" },
[]string { "lunchlogo/octaven_logo.gif", "Restaurang Octaven" },
[]string { "lunchlogo/bla_lagan.gif", "Blå Lågan" },
[]string { "lunchlogo/coop_forum.gif", "Coop Forum" },
[]string { "lunchlogo/Festmakarna06.gif", "Festmakarna" },
[]string { "lunchlogo/kok-nystrom.gif", "Kök Nyström restaurang & catering" },
[]string { "lunchlogo/Lilla-Krogen_2010.gif", "Gamla Lilla Krogen Werners" },
[]string { "lunchlogo/matopotatis.gif", "Mat & Potatis" },
[]string { "lunchlogo/Officerssalongen-2010.gif", "Officiersalongen" },
[]string { "lunchlogo/Restaurang-Fortuna-09.gif", "Restaurang Fortuna" },
[]string { "lunchlogo/Sushilovers.gif", "Sushi Lovers" },
[]string { "lunchlogo/travinn.gif", "Trav Inn" },
[]string { "lunchlogo/ya.gif", "Yrkesakademin" },
[]string { "lunchlogo/Scandic_blge.gif", "Scandic" },
[]string { "lunchlogo/TeknikdRest.gif", "Teknikdalens Restaurang" },
[]string { "lunchlogo/Broken-Dreams-borlange.gif", "Broken Dreams" },
[]string { "lunchlogo/Wild_West_Restaurang.gif", "Wild West Restaurang" },
[]string { "lunchlogo/The-Rock-House.gif", "The Rock House" },
[]string { "lunchlogo/Mathornan-Galaxen.gif", "Mathörnan Galaxen" },
[]string { "lunchlogo/bragematsalen.gif", "Brage Matsalen" },
[]string { "lunchlogo/matlagarna.gif", "Matlagarna" },
//Ludvika
[]string { "lunchlogo/Ahlens_cafe.gif", "Åhléns café" },
[]string { "lunchlogo/Gallerian.gif", "Restaurang & Cafe Gallerian" },
[]string { "lunchlogo/Hagge_Golfkrog_20105.gif", "Hagge Golfkrog" },
[]string { "lunchlogo/Kan-Elen-logo.gif", "Kan Elen" },
[]string { "lunchlogo/Piren_2009.gif", "Restaurang Piren" },
[]string { "lunchlogo/pizzeria_milano.gif", "Pizzeria Milano" },
[]string { "lunchlogo/silverdollar.gif", "Silverdollar" },
[]string { "lunchlogo/smedjebackens-wardshus.gif", "Smedjebackens Wärdshus" },
[]string { "lunchlogo/Stations_Kiosken.gif", "Stations Kiosken" },
[]string { "lunchlogo/stopet.gif", "Hotell & Värdshus Stopet" },
[]string { "lunchlogo/Sussis-Mat.gif", "Sussi's Mat & Catering" },
[]string { "lunchlogo/Wanbo-Herrgard.gif", "Wanbo Herrgård" },
[]string { "lunchlogo/Viljan-cafe.gif", "Viljan" },
[]string { "lunchlogo/Gourmet.gif", "Restaurang Gourmet Pizzeria" },
[]string { "lunchlogo/Kyrkogatan-no-9.gif", "Kyrkogatan no. 9" },
[]string { "lunchlogo/McDonalds2010.gif", "McDonalds" },
//Mora
[]string { "lunchlogo/Backa-Herrgard_09.gif", "Bäcka Herrgård" },
[]string { "lunchlogo/bykrogen2.gif", "Bykrogen" },
[]string { "lunchlogo/Cafe_Oscar.gif", "Restaurang & Café Oscar" },
[]string { "lunchlogo/Hotell-Alvdalen.gif", "Hotell Älvdalen" },
[]string { "lunchlogo/hotell-kung-gosta.gif", "Hotell Kung Gösta" },
[]string { "lunchlogo/moraparken.gif", "Mora Parken" },
[]string { "lunchlogo/Orsa_Stadshotell.gif", "Orsa Stadshotell" },
[]string { "lunchlogo/Strand-kok-o-bar.gif", "strand Kök & Bar" },
[]string { "lunchlogo/Vasagatan-32.gif", "Restaurang Vasagatan 32" },
[]string { "lunchlogo/Wasastugan.gif", "Restaurang Wasastugan" },
[]string { "lunchlogo/vi_pa_hornet.gif", "Vi på Hörnet" },
[]string { "lunchlogo/Orsa-Stadshotell.gif", "Orsa Stadshotell" },
[]string { "lunchlogo/FM-Mattson.gif", "FM Mattsson arena" },
[]string { "lunchlogo/Noret-Restaurang.gif", "Noret Restaurang & Pizzeria" },
[]string { "lunchlogo/Pasha-restaurang2010.gif", "Pasha Restaurang & Pizzeria" },
[]string { "lunchlogo/Famous-Moose-Restaurang.gif", "Famous Moose" },
[]string { "lunchlogo/Jacob.gif", "Jacob restaurang & bar" },
[]string { "lunchlogo/Ljungbergs-Sportsbar.gif", "Ljungbergs sportsbar" },
[]string { "lunchlogo/Wibe-Restaurangen.gif", "Wibe Restaurangen" },
//Sater/Hedemora
[]string { "lunchlogo/akropolis_sdt.gif", "Restaurang Akropolis" },
[]string { "lunchlogo/bla-lagunen.gif", "Blå Lagunen" },
[]string { "lunchlogo/lappens.gif", "Lappens Vägkrog" },
[]string { "lunchlogo/restaurang-skonvik.gif", "Restaurang Skönvik" },
[]string { "lunchlogo/The_Kings_Arms_2.gif", "The Kings Arms" },
[]string { "lunchlogo/Restaurang-Tjarna-Brunn.gif", "Restaurang Tjärna Brunn" },
[]string { "lunchlogo/tjarna-brunn.gif", "Restaurang Tjärna Brunn" },
[]string { "lunchlogo/Pizzeria-Athena.gif", "Pizzeria Athena" } };
// Simple string matching
//
for i := 0; i < len(arr); i++ {
if arr[i][0] == in {
return arr[i][1];
}
}
fmt.Printf("WARNING: Unable to match restaurant name to image %s! Code needs updating!\n", in);
return "";
}
// Simple function for generating the md5 hash of the input
// data both as a string and a []byte value.
//
func GenerateHash(inData []byte) (hashStr string, hash []byte) {
// Create new hash object
//
var h = md5.New();
// Input data and let it do it's work
h.Write(inData);
var sum = h.Sum();
// Convert the result to hexadecimal notation
//
hashStr = fmt.Sprintf("%x", sum);
// Make a []byte copy
//
hash = make([]byte, len(hashStr));
strings.NewReader(hashStr).Read(hash);
return;
}