Test address lookup

This commit is contained in:
idanoo 2022-09-13 21:56:18 +12:00
parent aa4b421528
commit 9927075d15
Signed by: idanoo
GPG Key ID: 387387CDBC02F132
6 changed files with 123 additions and 5 deletions

View File

@ -3,6 +3,7 @@ pipeline:
image: golang:1.19.1
commands:
- go mod tidy
- go build -o flatfinder ./cmd/flatfinder
depends_on:
- lint

View File

@ -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"
}

View File

@ -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 != "" {

View File

@ -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)

View File

@ -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)
}

View File

@ -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
}