mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-06 01:15:57 -06:00
Add scan function for downloading every form variation
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.env
|
||||
properties.html
|
||||
park
|
||||
forms/*
|
||||
@@ -6,14 +6,6 @@ A Discord bot for easily registering parking at the Register2Park parking system
|
||||
- Register against the location using the make, model, and plate.
|
||||
- Another command can register your email and acquire an email confirmation.
|
||||
|
||||
### Syntax
|
||||
|
||||
```
|
||||
/register <make> <model> <plate>
|
||||
/location <location>
|
||||
/email <email>
|
||||
```
|
||||
|
||||
## Feature Ideas
|
||||
|
||||
- Register commands in new guilds automatically
|
||||
|
||||
65
scan.go
Normal file
65
scan.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Scan() {
|
||||
locations := GetLocations()
|
||||
total := len(locations)
|
||||
|
||||
for i, location := range locations {
|
||||
log.Printf("[%6.2f] Fetching \"%s\" ", float64(i+1)/float64(total)*100, location.name)
|
||||
|
||||
body := fmt.Sprintf("propertyIdSelected=%d&propertySource=parking-snap", location.id)
|
||||
req := BuildRequestWithBody("POST", "/register-get-vehicle-form", nil, bytes.NewBufferString(body))
|
||||
SetTypicalHeaders(req, nil, nil, false)
|
||||
onRequest(req)
|
||||
|
||||
res, _ := client.Do(req)
|
||||
onResponse(res)
|
||||
html, _ := io.ReadAll(res.Body)
|
||||
|
||||
html_path := filepath.Join("./forms", fmt.Sprintf("%d.html", location.id))
|
||||
|
||||
// Check that file exists and has more than 16 bytes
|
||||
stats, err := os.Stat(html_path)
|
||||
if err != nil && errors.Is(err, os.ErrNotExist) {
|
||||
// File does not exist, create it
|
||||
file, err := os.Create(html_path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(html)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("DONE")
|
||||
} else if stats.Size() < 16 {
|
||||
// File exists, but is empty
|
||||
file, err := os.OpenFile(html_path, os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(html)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("OVERWRITE")
|
||||
} else {
|
||||
// File exists and is not empty, do nothing
|
||||
log.Println("SKIP")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user