Example #1
0
func indentAndPrint(buf *bytes.Buffer, js []uint8, lineNum int64) {
	jsErr := json.Indent(buf, js, "", "  ")
	if jsErr != nil {
		malformedJson(jsErr, js, lineNum)
	}
	os.Stdout.Write(buf.Bytes())
	os.Stdout.Write(newline)
	buf.Reset()
}
Example #2
0
func printJSON(j []byte) {
	var w bytes.Buffer
	err := json.Indent(&w, j, "| ", "        ")
	if err != nil {
		fmt.Printf("mon,json,err: %s\nORIG:\n%s\n", err, string(j))
		return
	}
	fmt.Printf("MON:\n%s\n", w.String())
}
Example #3
0
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)
	}
}