commit 1c5c2ae37450c234b8fc651c767bf90d362f0a03 Author: Peder Vatn Austad Date: Mon May 20 12:26:18 2024 +0200 0.1 or smth diff --git a/format.json b/format.json new file mode 100644 index 0000000..ad614c0 --- /dev/null +++ b/format.json @@ -0,0 +1,197 @@ +{ + "Siri": { "count": 1, "children": { "Siri": 1, "ServiceDelivery": 1 } }, + "ServiceDelivery": { + "count": 1, + "children": { + "ResponseTimestamp": 1, + "ProducerRef": 1, + "EstimatedTimetableDelivery": 1 + } + }, + "ResponseTimestamp": { "count": 2, "children": {} }, + "ProducerRef": { "count": 1, "children": {} }, + "EstimatedTimetableDelivery": { + "count": 1, + "children": { "ResponseTimestamp": 1, "EstimatedJourneyVersionFrame": 1 } + }, + "EstimatedJourneyVersionFrame": { + "count": 1, + "children": { "RecordedAtTime": 1, "EstimatedVehicleJourney": 3247 } + }, + "RecordedAtTime": { "count": 3248, "children": {} }, + "EstimatedVehicleJourney": { + "count": 3247, + "children": { + "RecordedAtTime": 3247, + "LineRef": 3247, + "DirectionRef": 3247, + "DatedVehicleJourneyRef": 426, + "VehicleMode": 1061, + "OriginRef": 2123, + "OriginName": 811, + "DestinationRef": 1034, + "DestinationName": 692, + "OperatorRef": 1429, + "ProductCategoryRef": 757, + "ServiceFeatureRef": 755, + "Monitored": 3247, + "DataSource": 3247, + "VehicleRef": 1500, + "EstimatedCalls": 3102, + "IsCompleteStopSequence": 3221, + "FramedVehicleJourneyRef": 2812, + "Cancellation": 720, + "JourneyPatternRef": 253, + "JourneyPatternName": 253, + "PublishedLineName": 339, + "DirectionName": 332, + "OriginAimedDepartureTime": 253, + "DestinationAimedArrivalTime": 253, + "BlockRef": 241, + "VehicleJourneyRef": 388, + "RecordedCalls": 1100, + "Occupancy": 192, + "DestinationDisplayAtOrigin": 53, + "SituationRef": 5, + "EstimatedVehicleJourneyCode": 9, + "ExtraJourney": 9, + "RouteRef": 7, + "GroupOfLinesRef": 7, + "ExternalLineRef": 7, + "InCongestion": 3, + "PredictionInaccurate": 29, + "JourneyNote": 8, + "Via": 1 + } + }, + "LineRef": { "count": 3247, "children": {} }, + "DirectionRef": { "count": 3247, "children": {} }, + "DatedVehicleJourneyRef": { "count": 3238, "children": {} }, + "VehicleMode": { "count": 1061, "children": {} }, + "OriginRef": { "count": 2123, "children": {} }, + "OriginName": { "count": 811, "children": {} }, + "DestinationRef": { "count": 1034, "children": {} }, + "DestinationName": { "count": 692, "children": {} }, + "OperatorRef": { "count": 1429, "children": {} }, + "ProductCategoryRef": { "count": 757, "children": {} }, + "ServiceFeatureRef": { "count": 755, "children": {} }, + "Monitored": { "count": 3247, "children": {} }, + "DataSource": { "count": 3247, "children": {} }, + "VehicleRef": { "count": 1500, "children": {} }, + "EstimatedCalls": { "count": 3102, "children": { "EstimatedCall": 66627 } }, + "EstimatedCall": { + "count": 66627, + "children": { + "StopPointRef": 66627, + "Order": 66627, + "StopPointName": 35396, + "RequestStop": 7872, + "AimedDepartureTime": 63523, + "ExpectedDepartureTime": 61532, + "DepartureStatus": 25305, + "DeparturePlatformName": 7621, + "DepartureBoardingActivity": 19064, + "DepartureStopAssignment": 3045, + "Extensions": 771, + "AimedArrivalTime": 64881, + "ExpectedArrivalTime": 62532, + "ArrivalStatus": 25975, + "ArrivalPlatformName": 8088, + "ArrivalBoardingActivity": 19156, + "ArrivalStopAssignment": 7370, + "CallNote": 52, + "Cancellation": 40359, + "DestinationDisplay": 27578, + "ExpectedDeparturePredictionQuality": 27331, + "ExpectedArrivalPredictionQuality": 27514, + "TimingPoint": 2692, + "SituationRef": 50, + "PredictionInaccurate": 2683, + "Occupancy": 220 + } + }, + "StopPointRef": { "count": 90814, "children": {} }, + "Order": { "count": 90814, "children": {} }, + "StopPointName": { "count": 52335, "children": {} }, + "RequestStop": { "count": 7872, "children": {} }, + "AimedDepartureTime": { "count": 87565, "children": {} }, + "ExpectedDepartureTime": { "count": 62220, "children": {} }, + "DepartureStatus": { "count": 25305, "children": {} }, + "DeparturePlatformName": { "count": 8475, "children": {} }, + "DepartureBoardingActivity": { "count": 19064, "children": {} }, + "DepartureStopAssignment": { + "count": 3045, + "children": { "AimedQuayRef": 3045, "ExpectedQuayRef": 3045 } + }, + "AimedQuayRef": { "count": 10415, "children": {} }, + "ExpectedQuayRef": { "count": 10415, "children": {} }, + "Extensions": { "count": 771, "children": { "StopsAtAirport": 771 } }, + "StopsAtAirport": { "count": 771, "children": {} }, + "AimedArrivalTime": { "count": 88059, "children": {} }, + "ExpectedArrivalTime": { "count": 63294, "children": {} }, + "ArrivalStatus": { "count": 25975, "children": {} }, + "ArrivalPlatformName": { "count": 9121, "children": {} }, + "ArrivalBoardingActivity": { "count": 19156, "children": {} }, + "ArrivalStopAssignment": { + "count": 7370, + "children": { "AimedQuayRef": 7370, "ExpectedQuayRef": 7370, "ActualQuayRef": 3 } + }, + "CallNote": { "count": 52, "children": {} }, + "IsCompleteStopSequence": { "count": 3221, "children": {} }, + "FramedVehicleJourneyRef": { + "count": 2812, + "children": { "DataFrameRef": 2812, "DatedVehicleJourneyRef": 2812 } + }, + "DataFrameRef": { "count": 2812, "children": {} }, + "Cancellation": { "count": 46294, "children": {} }, + "JourneyPatternRef": { "count": 253, "children": {} }, + "JourneyPatternName": { "count": 253, "children": {} }, + "PublishedLineName": { "count": 339, "children": {} }, + "DirectionName": { "count": 332, "children": {} }, + "OriginAimedDepartureTime": { "count": 253, "children": {} }, + "DestinationAimedArrivalTime": { "count": 253, "children": {} }, + "BlockRef": { "count": 241, "children": {} }, + "DestinationDisplay": { "count": 27578, "children": {} }, + "VehicleJourneyRef": { "count": 388, "children": {} }, + "RecordedCalls": { "count": 1100, "children": { "RecordedCall": 24187 } }, + "RecordedCall": { + "count": 24187, + "children": { + "StopPointRef": 24187, + "Order": 24187, + "Cancellation": 5215, + "AimedDepartureTime": 24042, + "ActualDepartureTime": 23387, + "AimedArrivalTime": 23178, + "ActualArrivalTime": 22444, + "StopPointName": 16939, + "ArrivalPlatformName": 1033, + "ExpectedArrivalTime": 762, + "ExpectedDepartureTime": 688, + "DeparturePlatformName": 854, + "PredictionInaccurate": 249, + "Occupancy": 94 + } + }, + "ActualDepartureTime": { "count": 23387, "children": {} }, + "ActualArrivalTime": { "count": 22444, "children": {} }, + "ExpectedDeparturePredictionQuality": { "count": 27331, "children": { "PredictionLevel": 27331 } }, + "PredictionLevel": { "count": 54845, "children": {} }, + "ExpectedArrivalPredictionQuality": { "count": 27514, "children": { "PredictionLevel": 27514 } }, + "TimingPoint": { "count": 2692, "children": {} }, + "SituationRef": { "count": 55, "children": { "SituationSimpleRef": 55 } }, + "SituationSimpleRef": { "count": 55, "children": {} }, + "Occupancy": { "count": 506, "children": {} }, + "DestinationDisplayAtOrigin": { "count": 53, "children": {} }, + "PredictionInaccurate": { "count": 2961, "children": {} }, + "EstimatedVehicleJourneyCode": { "count": 9, "children": {} }, + "ExtraJourney": { "count": 9, "children": {} }, + "RouteRef": { "count": 7, "children": {} }, + "GroupOfLinesRef": { "count": 7, "children": {} }, + "ExternalLineRef": { "count": 7, "children": {} }, + "ActualQuayRef": { "count": 3, "children": {} }, + "InCongestion": { "count": 3, "children": {} }, + "JourneyNote": { "count": 8, "children": {} }, + "Via": { "count": 1, "children": { "PlaceName": 1 } }, + "PlaceName": { "count": 1, "children": {} } + } \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d990866 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ti1 + +go 1.22.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6b2d229 --- /dev/null +++ b/main.go @@ -0,0 +1,308 @@ +package main + +import ( + "encoding/json" + "encoding/xml" + "fmt" + "log" + "net/http" +) + +type Data struct { + XMLName xml.Name `xml:"Siri"` + ServiceDelivery struct { + ResponseTimestamp string `xml:"ResponseTimestamp"` + ProducerRef string `xml:"ProducerRef"` + EstimatedTimetableDelivery []struct { + ResponseTimestamp string `xml:"ResponseTimestamp"` + EstimatedJourneyVersionFrame struct { + RecordedAtTime string `xml:"RecordedAtTime"` + EstimatedVehicleJourney []struct { + RecordedAtTime string `xml:"RecordedAtTime"` + LineRef string `xml:"LineRef"` + DirectionRef string `xml:"DirectionRef"` + DatedVehicleJourneyRef string `xml:"DatedVehicleJourneyRef"` + VehicleMode string `xml:"VehicleMode"` + OriginRef string `xml:"OriginRef"` + OriginName string `xml:"OriginName"` + DestinationRef string `xml:"DestinationRef"` + DestinationName string `xml:"DestinationName"` + OperatorRef string `xml:"OperatorRef"` + ProductCategoryRef string `xml:"ProductCategoryRef"` + ServiceFeatureRef string `xml:"ServiceFeatureRef"` + Monitored string `xml:"Monitored"` + DataSource string `xml:"DataSource"` + VehicleRef string `xml:"VehicleRef"` + EstimatedCalls []struct { + EstimatedCall []struct { + StopPointRef string `xml:"StopPointRef"` + Order string `xml:"Order"` + StopPointName string `xml:"StopPointName"` + RequestStop string `xml:"RequestStop"` + AimedDepartureTime string `xml:"AimedDepartureTime"` + ExpectedDepartureTime string `xml:"ExpectedDepartureTime"` + DepartureStatus string `xml:"DepartureStatus"` + DeparturePlatformName string `xml:"DeparturePlatformName"` + DepartureBoardingActivity string `xml:"DepartureBoardingActivity"` + DepartureStopAssignment struct { + AimedQuayRef string `xml:"AimedQuayRef"` + ExpectedQuayRef string `xml:"ExpectedQuayRef"` + ActualQuayRef string `xml:"ActualQuayRef"` + } `xml:"DepartureStopAssignment"` + Extensions struct { + StopsAtAirport string `xml:"StopsAtAirport"` + } `xml:"Extensions"` + AimedArrivalTime string `xml:"AimedArrivalTime"` + ExpectedArrivalTime string `xml:"ExpectedArrivalTime"` + ArrivalStatus string `xml:"ArrivalStatus"` + ArrivalPlatformName string `xml:"ArrivalPlatformName"` + ArrivalBoardingActivity string `xml:"ArrivalBoardingActivity"` + ArrivalStopAssignment struct { + AimedQuayRef string `xml:"AimedQuayRef"` + ExpectedQuayRef string `xml:"ExpectedQuayRef"` + ActualQuayRef string `xml:"ActualQuayRef"` + } `xml:"ArrivalStopAssignment"` + CallNote string `xml:"CallNote"` + Cancellation string `xml:"Cancellation"` + DestinationDisplay string `xml:"DestinationDisplay"` + ExpectedDeparturePredictionQuality struct { + PredictionLevel string `xml:"PredictionLevel"` + } `xml:"ExpectedDeparturePredictionQuality"` + ExpectedArrivalPredictionQuality struct { + PredictionLevel string `xml:"PredictionLevel"` + } `xml:"ExpectedArrivalPredictionQuality"` + TimingPoint string `xml:"TimingPoint"` + SituationRef string `xml:"SituationRef"` + PredictionInaccurate string `xml:"PredictionInaccurate"` + Occupancy string `xml:"Occupancy"` + } `xml:"EstimatedCall"` + } `xml:"EstimatedCalls"` + IsCompleteStopSequence string `xml:"IsCompleteStopSequence"` + FramedVehicleJourneyRef struct { + DataFrameRef string `xml:"DataFrameRef"` + DatedVehicleJourneyRef string `xml:"DatedVehicleJourneyRef"` + } `xml:"FramedyfguVehicleJourneyRef"` + Cancellation string `xml:"Cancellation"` + JourneyPatternRef string `xml:"JourneyPatternRef"` + JourneyPatternName string `xml:"JourneyPatternName"` + PublishedLineName string `xml:"PublishedLineName"` + DirectionName string `xml:"DirectionName"` + OriginAimedDepartureTime string `xml:"OriginAimedDepartureTime"` + DestinationAimedArrivalTime string `xml:"DestinationAimedArrivalTime"` + BlockRef string `xml:"BlockRef"` + VehicleJourneyRef string `xml:"VehicleJourneyRef"` + RecordedCalls []struct { + RecordedCall []struct { + StopPointRef string `xml:"StopPointRef"` + Order string `xml:"Order"` + Cancellation string `xml:"Cancellation"` + AimedDepartureTime string `xml:"AimedDepartureTime"` + ActualDepartureTime string `xml:"ActualDepartureTime"` + AimedArrivalTime string `xml:"AimedArrivalTime"` + ActualArrivalTime string `xml:"ActualArrivalTime"` + StopPointName string `xml:"StopPointName"` + ArrivalPlatformName string `xml:"ArrivalPlatformName"` + ExpectedArrivalTime string `xml:"ExpectedArrivalTime"` + ExpectedDepartureTime string `xml:"ExpectedDepartureTime"` + DeparturePlatformName string `xml:"DeparturePlatformName"` + PredictionInaccurate string `xml:"PredictionInaccurate"` + Occupancy string `xml:"Occupancy"` + } `xml:"RecordedCall"` + } `xml:"RecordedCalls"` + Occupancy string `xml:"Occupancy"` + DestinationDisplayAtOrigin string `xml:"DestinationDisplayAtOrigin"` + PredictionInaccurate string `xml:"PredictionInaccurate"` + EstimatedVehicleJourneyCode string `xml:"EstimatedVehicleJourneyCode"` + ExtraJourney string `xml:"ExtraJourney"` + RouteRef string `xml:"RouteRef"` + GroupOfLinesRef string `xml:"GroupOfLinesRef"` + ExternalLineRef string `xml:"ExternalLineRef"` + InCongestion string `xml:"InCongestion"` + JourneyNote string `xml:"JourneyNote"` + Via struct { + PlaceName string `xml:"PlaceName"` + } `xml:"Via"` + } `xml:"EstimatedVehicleJourney"` + } `xml:"EstimatedJourneyVersionFrame"` + } `xml:"EstimatedTimetableDelivery"` + } `xml:"ServiceDelivery"` +} + +func main() { + // Fetch data from entur + resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=1") + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + + data := &Data{} // Initialize your struct + + decoder := xml.NewDecoder(resp.Body) // Create a new XML decoder + err = decoder.Decode(data) // Decode the XML data into your struct + if err != nil { + log.Fatal(err) + } + + printData(data) + +} + +func printData(data *Data) { + fmt.Println(data.ServiceDelivery.ResponseTimestamp) + fmt.Println(data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) + for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney { + if journey.RecordedAtTime != "" { + fmt.Println(journey.RecordedAtTime) + } + if journey.LineRef != "" { + fmt.Println(journey.LineRef) + } + if journey.DirectionRef != "" { + fmt.Println(journey.DirectionRef) + } + if journey.DataSource != "" { + fmt.Println(journey.DataSource) + } + + if journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef != "" { + fmt.Println(journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef) + } else if journey.DatedVehicleJourneyRef != "" { + fmt.Println(journey.DatedVehicleJourneyRef) + } else { + fmt.Println("evj." + journey.EstimatedVehicleJourneyCode) + } + + if journey.VehicleMode != "" { + fmt.Println(journey.VehicleMode) + } + if journey.FramedVehicleJourneyRef.DataFrameRef != "" { + fmt.Println(journey.FramedVehicleJourneyRef.DataFrameRef) + } + if journey.OriginRef != "" { + fmt.Println(journey.OriginRef) + } + if journey.DestinationRef != "" { + fmt.Println(journey.DestinationRef) + } + if journey.OperatorRef != "" { + fmt.Println(journey.OperatorRef) + } + if journey.VehicleRef != "" { + fmt.Println(journey.VehicleRef) + } + if journey.Cancellation != "" { + fmt.Println(journey.Cancellation) + } + + // Create a slice to hold the JSON objects + var jsonArray []map[string]interface{} + + // Iterate over the EstimatedVehicleJourney slice + for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney { + // Create a map to hold the JSON object + jsonObject := make(map[string]interface{}) + + // Check if OriginName exists and add it to the JSON object + if journey.OriginName != "" { + jsonObject["OriginName"] = journey.OriginName + } + + if journey.DestinationName != "" { + jsonObject["DestinationName"] = journey.DestinationName + } + + if journey.ProductCategoryRef != "" { + jsonObject["ProductCategoryRef"] = journey.ProductCategoryRef + } + + if journey.ServiceFeatureRef != "" { + jsonObject["ServiceFeatureRef"] = journey.ServiceFeatureRef + } + + if journey.Monitored != "" { + jsonObject["Monitored"] = journey.Monitored + } + + if journey.JourneyPatternRef != "" { + jsonObject["JourneyPatternRef"] = journey.JourneyPatternRef + } + + if journey.JourneyPatternName != "" { + jsonObject["JourneyPatternName"] = journey.JourneyPatternName + } + + if journey.PublishedLineName != "" { + jsonObject["PublishedLineName"] = journey.PublishedLineName + } + + if journey.DirectionName != "" { + jsonObject["DirectionName"] = journey.DirectionName + } + + if journey.OriginAimedDepartureTime != "" { + jsonObject["OriginAimedDepartureTime"] = journey.OriginAimedDepartureTime + } + + if journey.DestinationAimedArrivalTime != "" { + jsonObject["DestinationAimedArrivalTime"] = journey.DestinationAimedArrivalTime + } + + if journey.BlockRef != "" { + jsonObject["BlockRef"] = journey.BlockRef + } + + if journey.VehicleJourneyRef != "" { + jsonObject["VehicleJourneyRef"] = journey.VehicleJourneyRef + } + + if journey.Occupancy != "" { + jsonObject["Occupancy"] = journey.Occupancy + } + + if journey.DestinationDisplayAtOrigin != "" { + jsonObject["DestinationDisplayAtOrigin"] = journey.DestinationDisplayAtOrigin + } + + if journey.ExtraJourney != "" { + jsonObject["ExtraJourney"] = journey.ExtraJourney + } + + if journey.RouteRef != "" { + jsonObject["RouteRef"] = journey.RouteRef + } + + if journey.GroupOfLinesRef != "" { + jsonObject["GroupOfLinesRef"] = journey.GroupOfLinesRef + } + + if journey.ExternalLineRef != "" { + jsonObject["ExternalLineRef"] = journey.ExternalLineRef + } + + if journey.InCongestion != "" { + jsonObject["InCongestion"] = journey.InCongestion + } + + if journey.PredictionInaccurate != "" { + jsonObject["PredictionInaccurate"] = journey.PredictionInaccurate + } + + if journey.JourneyNote != "" { + jsonObject["JourneyNote"] = journey.JourneyNote + } + + jsonArray = append(jsonArray, jsonObject) + } + + // Convert the JSON array to a JSON string + jsonString, err := json.Marshal(jsonArray) + if err != nil { + log.Fatal(err) + } + + // Print the JSON string + fmt.Println(string(jsonString)) + + } +}