From 892012bc1cfb9ae39b289b405f564503b9c492f4 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Tue, 29 Apr 2025 09:40:39 +0200 Subject: [PATCH] Split off efa_client module --- efa_client.go | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 139 ------------------------------------------------ 2 files changed, 143 insertions(+), 139 deletions(-) create mode 100644 efa_client.go diff --git a/efa_client.go b/efa_client.go new file mode 100644 index 0000000..f25db0f --- /dev/null +++ b/efa_client.go @@ -0,0 +1,143 @@ +package main + +import "net/http" +import "net/url" +import "strings" + +type EFAClient interface { + GetName() string + BuildRequest(string) (*http.Request, error) +} + +var allEfaClients []EFAClient = []EFAClient{ + BwegtEFAClient{}, + VRNEFAClient{}, + KVVEFAClient{}, + VAGEFAClient{}, +} + +type VRNEFAClient struct { +} + +func (c VRNEFAClient) GetName() string { + return "VRN" +} + +func (c VRNEFAClient) BuildRequest(stopId string) (*http.Request, error) { + // Create request object + req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil) + if err != nil { + return nil, err + } + + // Configure our request + query := url.Values{} + query.Set("coordOutputFormat", "EPSG:4326") + query.Set("depType", "stopEvents") + query.Set("includeCompleteStopSeq", "0") + query.Set("limit", "10") + query.Set("locationServerActive", "0") + query.Set("mode", "direct") + query.Set("name_dm", stopId) + query.Set("outputFormat", "json") + query.Set("type_dm", "stop") + query.Set("useOnlyStops", "1") + query.Set("useRealtime", "1") + req.URL.RawQuery = query.Encode() + + return req, nil +} + +type KVVEFAClient struct { +} + +func (c KVVEFAClient) GetName() string { + return "KVV" +} + +func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { + form := url.Values{} + form.Set("action", "XSLT_DM_REQUEST") + form.Set("name_dm", stopId) + form.Set("type_dm", "stop") + form.Set("useRealtime", "1") + form.Set("limit", "10") + form.Set("mode", "direct") + form.Set("useRealtime", "1") + form.Set("outputFormat", "json") + body := strings.NewReader(form.Encode()) + + req, err := http.NewRequest("POST", "https://www.kvv.de/tunnelEfaDirect.php", body) + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "coolio/1.0") + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + return req, nil +} + +type BwegtEFAClient struct { +} + +func (c BwegtEFAClient) GetName() string { + return "bwegt" +} + +func (c BwegtEFAClient) BuildRequest(stopId string) (*http.Request, error) { + // Create request object + req, err := http.NewRequest("GET", "https://www.bwegt.de/bwegt-efa/XML_DM_REQUEST", nil) + if err != nil { + return nil, err + } + + // Configure our request + query := url.Values{} + query.Set("coordOutputFormat", "EPSG:4326") + query.Set("depType", "stopEvents") + query.Set("includeCompleteStopSeq", "0") + query.Set("limit", "10") + query.Set("locationServerActive", "0") + query.Set("mode", "direct") + query.Set("name_dm", stopId) + query.Set("outputFormat", "json") + query.Set("type_dm", "stop") + query.Set("useOnlyStops", "1") + query.Set("useRealtime", "1") + req.URL.RawQuery = query.Encode() + + return req, nil +} + +type VAGEFAClient struct { +} + +func (c VAGEFAClient) GetName() string { + return "VAG" +} + +func (c VAGEFAClient) BuildRequest(stopId string) (*http.Request, error) { + // Create request object + req, err := http.NewRequest("GET", "https://efa.vagfr.de/vagfr3/XSLT_DM_REQUEST", nil) + if err != nil { + return nil, err + } + + // Configure our request + query := url.Values{} + query.Set("coordOutputFormat", "EPSG:4326") + query.Set("depType", "stopEvents") + query.Set("includeCompleteStopSeq", "0") + query.Set("limit", "10") + query.Set("locationServerActive", "0") + query.Set("mode", "direct") + query.Set("name_dm", stopId) + query.Set("outputFormat", "json") + query.Set("type_dm", "stop") + query.Set("useOnlyStops", "1") + query.Set("useRealtime", "1") + req.URL.RawQuery = query.Encode() + + return req, nil +} diff --git a/main.go b/main.go index 053ec7e..3332b42 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import "errors" import "fmt" import "log" import "net/http" -import "net/url" import "os" import "slices" import "strings" @@ -47,144 +46,6 @@ type DMDateTime struct { Minute string `json:"minute"` } -type EFAClient interface { - GetName() string - BuildRequest(string) (*http.Request, error) -} - -var allEfaClients []EFAClient = []EFAClient{ - BwegtEFAClient{}, - VRNEFAClient{}, - KVVEFAClient{}, - VAGEFAClient{}, -} - -type VRNEFAClient struct { -} - -func (c VRNEFAClient) GetName() string { - return "VRN" -} - -func (c VRNEFAClient) BuildRequest(stopId string) (*http.Request, error) { - // Create request object - req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil) - if err != nil { - return nil, err - } - - // Configure our request - query := url.Values{} - query.Set("coordOutputFormat", "EPSG:4326") - query.Set("depType", "stopEvents") - query.Set("includeCompleteStopSeq", "0") - query.Set("limit", "10") - query.Set("locationServerActive", "0") - query.Set("mode", "direct") - query.Set("name_dm", stopId) - query.Set("outputFormat", "json") - query.Set("type_dm", "stop") - query.Set("useOnlyStops", "1") - query.Set("useRealtime", "1") - req.URL.RawQuery = query.Encode() - - return req, nil -} - -type KVVEFAClient struct { -} - -func (c KVVEFAClient) GetName() string { - return "KVV" -} - -func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { - form := url.Values{} - form.Set("action", "XSLT_DM_REQUEST") - form.Set("name_dm", stopId) - form.Set("type_dm", "stop") - form.Set("useRealtime", "1") - form.Set("limit", "10") - form.Set("mode", "direct") - form.Set("useRealtime", "1") - form.Set("outputFormat", "json") - body := strings.NewReader(form.Encode()) - - req, err := http.NewRequest("POST", "https://www.kvv.de/tunnelEfaDirect.php", body) - if err != nil { - return nil, err - } - - req.Header.Set("User-Agent", "coolio/1.0") - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - return req, nil -} - -type BwegtEFAClient struct { -} - -func (c BwegtEFAClient) GetName() string { - return "bwegt" -} - -func (c BwegtEFAClient) BuildRequest(stopId string) (*http.Request, error) { - // Create request object - req, err := http.NewRequest("GET", "https://www.bwegt.de/bwegt-efa/XML_DM_REQUEST", nil) - if err != nil { - return nil, err - } - - // Configure our request - query := url.Values{} - query.Set("coordOutputFormat", "EPSG:4326") - query.Set("depType", "stopEvents") - query.Set("includeCompleteStopSeq", "0") - query.Set("limit", "10") - query.Set("locationServerActive", "0") - query.Set("mode", "direct") - query.Set("name_dm", stopId) - query.Set("outputFormat", "json") - query.Set("type_dm", "stop") - query.Set("useOnlyStops", "1") - query.Set("useRealtime", "1") - req.URL.RawQuery = query.Encode() - - return req, nil -} - -type VAGEFAClient struct { -} - -func (c VAGEFAClient) GetName() string { - return "VAG" -} - -func (c VAGEFAClient) BuildRequest(stopId string) (*http.Request, error) { - // Create request object - req, err := http.NewRequest("GET", "https://efa.vagfr.de/vagfr3/XSLT_DM_REQUEST", nil) - if err != nil { - return nil, err - } - - // Configure our request - query := url.Values{} - query.Set("coordOutputFormat", "EPSG:4326") - query.Set("depType", "stopEvents") - query.Set("includeCompleteStopSeq", "0") - query.Set("limit", "10") - query.Set("locationServerActive", "0") - query.Set("mode", "direct") - query.Set("name_dm", stopId) - query.Set("outputFormat", "json") - query.Set("type_dm", "stop") - query.Set("useOnlyStops", "1") - query.Set("useRealtime", "1") - req.URL.RawQuery = query.Encode() - - return req, nil -} - func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) { req, err := c.BuildRequest(stopId) if err != nil {