mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-09 16:08:12 -06:00
Working reload() cookie initialization, location parsing, location caching
This commit is contained in:
40
api.go
40
api.go
@@ -2,11 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
@@ -16,6 +16,7 @@ var (
|
|||||||
client *http.Client
|
client *http.Client
|
||||||
requestCounter uint
|
requestCounter uint
|
||||||
lastReload int64
|
lastReload int64
|
||||||
|
parsePattern = regexp.MustCompile("\\s*(.+)\\n\\s+(.+)\\n\\s+(\\d+)\\s*")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -48,6 +49,15 @@ func reload(name string) {
|
|||||||
res, _ := client.Do(req)
|
res, _ := client.Do(req)
|
||||||
onResponse(res)
|
onResponse(res)
|
||||||
|
|
||||||
|
// Ring the second doorbell (seems to be a way of validating whether a client is a 'browser' or not)
|
||||||
|
req = BuildRequest("GET", "/index.php", map[string]string{
|
||||||
|
"width": "1920",
|
||||||
|
"height": "1080",
|
||||||
|
})
|
||||||
|
onRequest(req)
|
||||||
|
res, _ = client.Do(req)
|
||||||
|
onResponse(res)
|
||||||
|
|
||||||
// TODO: Verify that a PHPSESSID cookie is present
|
// TODO: Verify that a PHPSESSID cookie is present
|
||||||
|
|
||||||
if len(name) > 0 {
|
if len(name) > 0 {
|
||||||
@@ -102,26 +112,17 @@ func GetLocations() []Location {
|
|||||||
|
|
||||||
tryReload("")
|
tryReload("")
|
||||||
|
|
||||||
body := "propertyNameEntered="
|
body := "propertyNameEntered=" // Empty, so we get all locations
|
||||||
req := BuildRequestWithBody("GET", "/register-get-properties-from-name", nil, bytes.NewBufferString(body))
|
req := BuildRequestWithBody("POST", "/register-get-properties-from-name", nil, bytes.NewBufferString(body))
|
||||||
SetTypicalHeaders(req, nil, nil, true)
|
SetTypicalHeaders(req, nil, nil, true)
|
||||||
|
|
||||||
onRequest(req)
|
onRequest(req)
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
fmt.Println(DebugRequest(res.Request))
|
|
||||||
onResponse(res)
|
onResponse(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// print response body
|
|
||||||
response_body, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
fmt.Println(DebugResponse(res))
|
|
||||||
fmt.Printf("%s\n", response_body)
|
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
@@ -130,10 +131,19 @@ func GetLocations() []Location {
|
|||||||
|
|
||||||
locations := make([]Location, 0, 150)
|
locations := make([]Location, 0, 150)
|
||||||
|
|
||||||
// Find all input.property
|
|
||||||
doc.Find("input.property").Each(func(i int, s *goquery.Selection) {
|
doc.Find("input.property").Each(func(i int, s *goquery.Selection) {
|
||||||
log.Printf("%s", s.Text())
|
matches := parsePattern.FindStringSubmatch(s.Parent().Text())
|
||||||
|
id, _ := strconv.ParseUint(matches[3], 10, 32)
|
||||||
|
|
||||||
|
locations = append(locations, Location{
|
||||||
|
id: uint(id),
|
||||||
|
name: matches[1],
|
||||||
|
address: matches[2],
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
cachedLocations = locations
|
||||||
|
cacheExpiry = time.Now().Add(time.Hour * 3)
|
||||||
|
|
||||||
return locations
|
return locations
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user