diff --git a/api.go b/api.go new file mode 100644 index 0000000..87fa04d --- /dev/null +++ b/api.go @@ -0,0 +1,82 @@ +package main + +import ( + "log" + "net/http" + "net/http/cookiejar" + "time" +) + +var ( + client *http.Client + cookies *cookiejar.Jar + requestCounter uint + lastReload int64 +) + +func init() { + cookies, err := cookiejar.New(nil) + if err != nil { + log.Fatal(err) + } + + client = &http.Client{Jar: cookies} +} + +func onRequest(req *http.Request) { + log.Printf("GET %s", req.URL.String()) + requestCounter++ +} + +func onResponse(res *http.Response) { + log.Printf("%s %d %s", res.Status, res.ContentLength, res.Header["Content-Type"]) +} + +func buildURL(path string, params map[string]string) string { + return baseURL + path +} + +// Reloads the current session and completes the initial connection process that procedes bot operations. +// This should be done regularly, such as after 10 requests or 15 minutes. +func reload(name string) { + // Ring the doorbell + req := BuildRequest("GET", "/", nil) + onRequest(req) + res, _ := client.Do(req) + onResponse(res) + + // This request provides a PHPSESSID cookie + req = BuildRequest("GET", "https://api.parkingsnap.com/supportedLanguages?target=en&siteName=", nil) + client.Do(req) + + // TODO: Verify that a PHPSESSID cookie is present + + if len(name) > 0 { + // TODO: GET https://www.register2park.com/register-get-properties-from-name + // TODO: GET https://www.register2park.com/register?key=678zv9zzylvw + // TODO: GET https://www.register2park.com/register-get-properties-from-name + } +} + +// Attempts to reload the current session based on a given location's name. +// This uses the current request counter and last reload time to determine if a reload is necessary. +func tryReload(name string) { + currentTime := time.Now().Unix() + lastReloadDiff := currentTime - lastReload + + if requestCounter >= 10 { + log.Println("Reloading session due to request count...") + } else if lastReloadDiff >= 15*60 { + log.Println("Reloading session due to time...") + } else { + return + } + + reload(name) + lastReload = currentTime + requestCounter = 0 +} + +func register(location uint, code string, make string, model string, plate string) { + +} diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7d1da0c --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY= +github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=