From 9927075d1511758e9ca42efb1bbd37fabee34f27 Mon Sep 17 00:00:00 2001 From: idanoo Date: Tue, 13 Sep 2022 21:56:18 +1200 Subject: [PATCH] Test address lookup --- .woodpecker/.build.yml | 1 + internal/flatfinder/chorus.go | 99 ++++++++++++++++++++++++++++++++++ internal/flatfinder/discord.go | 23 +++++++- internal/flatfinder/main.go | 2 - internal/flatfinder/trademe.go | 2 +- internal/flatfinder/utils.go | 1 + 6 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 internal/flatfinder/chorus.go diff --git a/.woodpecker/.build.yml b/.woodpecker/.build.yml index 8238ac6..4fdae15 100644 --- a/.woodpecker/.build.yml +++ b/.woodpecker/.build.yml @@ -3,6 +3,7 @@ pipeline: image: golang:1.19.1 commands: - go mod tidy + - go build -o flatfinder ./cmd/flatfinder depends_on: - lint diff --git a/internal/flatfinder/chorus.go b/internal/flatfinder/chorus.go new file mode 100644 index 0000000..dc325bc --- /dev/null +++ b/internal/flatfinder/chorus.go @@ -0,0 +1,99 @@ +package flatfinder + +import ( + "fmt" + "io" + "log" + "net/http" + "net/url" +) + +type ChorusAddressLookupResponse struct { +} + +func chorusAddressLookup(address string) string { + log.Printf("Querying address: %s", address) + lookupURL := fmt.Sprintf( + "https://api.chorus.co.nz/addresslookup/v1/addresses/?fuzzy=true&q=%s", + url.QueryEscape(address), + ) + //curl 'https://api.chorus.co.nz/addresslookup/v1/addresses/?fuzzy=true&q=35%20Rosalind' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'X-Chorus-Client-Id: 82d4b4a8050c4d5e97c5f06120ef9c04' -H 'X-Chorus-Client-Secret: 8899c64746474Cf18849c6B721b5Db51' -H 'X-Transaction-Id: ca5ef871-9b3f-4af4-958d-ee9c51094e08' -H 'Origin: https://www.chorus.co.nz' + // Build HTTP request + client := http.Client{} + req, err := http.NewRequest("GET", lookupURL, nil) + if err != nil { + log.Print(err) + return "UNK" + } + + // Magic numbers - May need to dynamically receive these + req.Header.Set("X-Chorus-Client-Id", "82d4b4a8050c4d5e97c5f06120ef9c04") + req.Header.Set("X-Chorus-Client-Secret", "8899c64746474Cf18849c6B721b5Db51") + req.Header.Set("X-Transaction-Id", "ca5ef871-9b3f-4af4-958d-ee9c51094e08") + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", "https://tinker.nz/idanoo/flat-finder") + + // Do the request + resp, err := client.Do(req) + if err != nil { + log.Print(err) + return "UNK" + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNonAuthoritativeInfo { + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + log.Print(err) + return "UNK" + } + + log.Println(string(bodyBytes)) + return "N/A" + } else { + log.Print("Invalid response from API: " + resp.Status) + } + + return "UNK" +} + +// getAvailableSpeeds - Checks if VDSL/FIBRE is available +func getAvailableSpeeds(address string) string { + return chorusAddressLookup(address) + + // // Build HTTP request + // client := http.Client{} + // req, err := http.NewRequest("GET", chorusURL, nil) + // if err != nil { + // log.Print(err) + // return "UNK" + // } + + // req.Header.Set("Content-TypeContent-Type", "application/json") + // req.Header.Set("User-Agent", "https://tinker.nz/idanoo/flat-finder") + + // // Do the request + // resp, err := client.Do(req) + // if err != nil { + // log.Print(err) + // return "UNK" + // } + // defer resp.Body.Close() + + // if resp.StatusCode == http.StatusOK { + // bodyBytes, err := io.ReadAll(resp.Body) + // if err != nil { + // log.Print(err) + // return "UNK" + // } + + // log.Println(string(bodyBytes)) + + // return "N/A" + // } else { + // log.Print("Invalid response from API: " + resp.Status) + // } + + return "UNK" +} diff --git a/internal/flatfinder/discord.go b/internal/flatfinder/discord.go index 38d9d8c..21d4421 100644 --- a/internal/flatfinder/discord.go +++ b/internal/flatfinder/discord.go @@ -37,13 +37,32 @@ func (c *LocalConfig) initDiscord() { func (c *LocalConfig) sendEmbeddedMessage(listing TradeMeListing) { log.Printf("New listing: %s", listing.Title) + hasFibre := getAvailableSpeeds( + fmt.Sprintf( + "%s, %s, %s", + strings.TrimSpace(listing.Address), + strings.TrimSpace(listing.Suburb), + strings.TrimSpace(listing.Region), + ), + ) + embed := discord.NewEmbedBuilder(). SetTitle(listing.Title). SetURL(fmt.Sprintf("https://trademe.co.nz/%d", listing.ListingID)). SetColor(1127128). SetImage(listing.PictureHref). - AddField("Location", listing.Address, true). - AddField("Bedrooms", fmt.Sprintf("%d", listing.Bedrooms), true) + AddField( + "Location", + fmt.Sprintf( + "[%s](https://maps.google.com/maps?z=12&t=m&q=loc:%f+%f)", + listing.Address, + listing.GeographicLocation.Latitude, + listing.GeographicLocation.Longitude, + ), + true, + ). + AddField("Bedrooms", fmt.Sprintf("%d", listing.Bedrooms), true). + AddField("Has Fibre", hasFibre, true) // Only add address if token set if c.GoogleApiToken != "" && c.GoogleLocation1 != "" { diff --git a/internal/flatfinder/main.go b/internal/flatfinder/main.go index 769fa08..5fa455d 100644 --- a/internal/flatfinder/main.go +++ b/internal/flatfinder/main.go @@ -57,8 +57,6 @@ func Launch() { // pollUpdates - check for new listings! func (c *LocalConfig) pollUpdates() { - log.Printf("Polling for updates") - err := Conf.searchTrademe() if err != nil { log.Println(err) diff --git a/internal/flatfinder/trademe.go b/internal/flatfinder/trademe.go index 5e03036..1e31b89 100644 --- a/internal/flatfinder/trademe.go +++ b/internal/flatfinder/trademe.go @@ -164,7 +164,7 @@ func (c *LocalConfig) handleTrademeResponse(responseJson []byte) error { return err } - log.Printf("Query complete. Result count: %d", resultSet.TotalCount) + log.Printf("Query complete. Listings: %d", resultSet.TotalCount) for _, result := range resultSet.List { c.parseTrademeListing(result) } diff --git a/internal/flatfinder/utils.go b/internal/flatfinder/utils.go index a266cf5..d82144c 100644 --- a/internal/flatfinder/utils.go +++ b/internal/flatfinder/utils.go @@ -39,6 +39,7 @@ func (c *LocalConfig) loadConfig() { log.Printf("Loaded %d previously posted property IDs", len(c.PostedProperties)) } else { + // Create empty map for first run maps := make(map[int64]bool) c.PostedProperties = maps }