timetable-extension #1
@ -2,10 +2,8 @@ package cif
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"os"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.fjla.uk/owlboard/timetable-mgr/helpers"
 | 
			
		||||
	"git.fjla.uk/owlboard/timetable-mgr/log"
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
)
 | 
			
		||||
@ -105,38 +103,3 @@ func parseDaysRun(daysBinary *string) []string {
 | 
			
		||||
	}
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func debugWriteDownload(input *[]byte) {
 | 
			
		||||
	if helpers.Runtime == "debug" {
 | 
			
		||||
		log.Msg.Debug("Writing CIF Download to file")
 | 
			
		||||
		filepath := "./cif_debug_data/"
 | 
			
		||||
		filename := time.Now().In(londonTimezone).Format("2006-01-02_15:04:05_RawCIF")
 | 
			
		||||
 | 
			
		||||
		err := os.MkdirAll(filepath, 0777)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Msg.Error("Error creating directory", zap.Error(err))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		file, err := os.Create(filepath + filename + ".jsonl")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Msg.Error("Error creating file", zap.Error(err))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		defer file.Close()
 | 
			
		||||
 | 
			
		||||
		// Write data to file in smaller chunks
 | 
			
		||||
		bufferSize := 4096 // Adjust the buffer size as needed
 | 
			
		||||
		for i := 0; i < len(*input); i += bufferSize {
 | 
			
		||||
			end := i + bufferSize
 | 
			
		||||
			if end > len(*input) {
 | 
			
		||||
				end = len(*input)
 | 
			
		||||
			}
 | 
			
		||||
			_, err := file.Write((*input)[i:end])
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Msg.Error("Error writing data to file", zap.Error(err))
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										57
									
								
								cif/parse.go
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								cif/parse.go
									
									
									
									
									
								
							@ -1,7 +1,6 @@
 | 
			
		||||
package cif
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"io"
 | 
			
		||||
@ -11,61 +10,7 @@ import (
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Unmarshalls data into the correct types for processing
 | 
			
		||||
// - Currently not used
 | 
			
		||||
func parseCifData(data *[]byte) (*parsedData, error) {
 | 
			
		||||
	log.Msg.Debug("Starting CIF Data parsing")
 | 
			
		||||
	if data == nil {
 | 
			
		||||
		return nil, errors.New("unable to parse nil pointer")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Initialise data structures
 | 
			
		||||
	var parsed parsedData
 | 
			
		||||
	parsed.assoc = make([]upstreamApi.JsonAssociationV1, 0)
 | 
			
		||||
	parsed.sched = make([]upstreamApi.JsonScheduleV1, 0)
 | 
			
		||||
 | 
			
		||||
	// Create JSON Decoder
 | 
			
		||||
	decoder := json.NewDecoder(bytes.NewReader(*data))
 | 
			
		||||
 | 
			
		||||
	// Iterate over JSON Objects using stream decoder
 | 
			
		||||
	for decoder.More() {
 | 
			
		||||
		var obj map[string]json.RawMessage
 | 
			
		||||
		if err := decoder.Decode(&obj); err != nil {
 | 
			
		||||
			log.Msg.Error("Error decoding JSON String")
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Handle parsed data
 | 
			
		||||
		for key, value := range obj {
 | 
			
		||||
			switch key {
 | 
			
		||||
			case "JsonTimetableV1":
 | 
			
		||||
				var timetable upstreamApi.JsonTimetableV1
 | 
			
		||||
				if err := json.Unmarshal(value, &timetable); err != nil {
 | 
			
		||||
					log.Msg.Error("Error decoding JSONTimetableV1 object", zap.Error(err))
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				parsed.header = timetable
 | 
			
		||||
			case "JsonAssociationV1":
 | 
			
		||||
				var association upstreamApi.JsonAssociationV1
 | 
			
		||||
				if err := json.Unmarshal(value, &association); err != nil {
 | 
			
		||||
					log.Msg.Error("Error decoding JSONAssociationV1 object", zap.Error(err))
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				parsed.assoc = append(parsed.assoc, association)
 | 
			
		||||
			case "JsonScheduleV1":
 | 
			
		||||
				var schedule upstreamApi.JsonScheduleV1
 | 
			
		||||
				if err := json.Unmarshal(value, &schedule); err != nil {
 | 
			
		||||
					log.Msg.Error("Error decoding JSONScheduleV1 object", zap.Error(err))
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				parsed.sched = append(parsed.sched, schedule)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	log.Msg.Debug("CIF Parsing completed")
 | 
			
		||||
	return &parsed, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Accepts the CIF data as a stream and outputs parsed data
 | 
			
		||||
func parseCifDataStream(dataStream io.ReadCloser) (*parsedData, error) {
 | 
			
		||||
	log.Msg.Debug("STREAM-Starting CIF Datastream parsing")
 | 
			
		||||
	if dataStream == nil {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user