mirror of
https://github.com/idanoo/go-flat-finder.git
synced 2024-12-04 06:03:16 +00:00
Test address lookup
This commit is contained in:
parent
aa4b421528
commit
9927075d15
@ -3,6 +3,7 @@ pipeline:
|
|||||||
image: golang:1.19.1
|
image: golang:1.19.1
|
||||||
commands:
|
commands:
|
||||||
- go mod tidy
|
- go mod tidy
|
||||||
|
- go build -o flatfinder ./cmd/flatfinder
|
||||||
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- lint
|
- lint
|
||||||
|
99
internal/flatfinder/chorus.go
Normal file
99
internal/flatfinder/chorus.go
Normal 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"
|
||||||
|
}
|
@ -37,13 +37,32 @@ func (c *LocalConfig) initDiscord() {
|
|||||||
func (c *LocalConfig) sendEmbeddedMessage(listing TradeMeListing) {
|
func (c *LocalConfig) sendEmbeddedMessage(listing TradeMeListing) {
|
||||||
log.Printf("New listing: %s", listing.Title)
|
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().
|
embed := discord.NewEmbedBuilder().
|
||||||
SetTitle(listing.Title).
|
SetTitle(listing.Title).
|
||||||
SetURL(fmt.Sprintf("https://trademe.co.nz/%d", listing.ListingID)).
|
SetURL(fmt.Sprintf("https://trademe.co.nz/%d", listing.ListingID)).
|
||||||
SetColor(1127128).
|
SetColor(1127128).
|
||||||
SetImage(listing.PictureHref).
|
SetImage(listing.PictureHref).
|
||||||
AddField("Location", listing.Address, true).
|
AddField(
|
||||||
AddField("Bedrooms", fmt.Sprintf("%d", listing.Bedrooms), true)
|
"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
|
// Only add address if token set
|
||||||
if c.GoogleApiToken != "" && c.GoogleLocation1 != "" {
|
if c.GoogleApiToken != "" && c.GoogleLocation1 != "" {
|
||||||
|
@ -57,8 +57,6 @@ func Launch() {
|
|||||||
|
|
||||||
// pollUpdates - check for new listings!
|
// pollUpdates - check for new listings!
|
||||||
func (c *LocalConfig) pollUpdates() {
|
func (c *LocalConfig) pollUpdates() {
|
||||||
log.Printf("Polling for updates")
|
|
||||||
|
|
||||||
err := Conf.searchTrademe()
|
err := Conf.searchTrademe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -164,7 +164,7 @@ func (c *LocalConfig) handleTrademeResponse(responseJson []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Query complete. Result count: %d", resultSet.TotalCount)
|
log.Printf("Query complete. Listings: %d", resultSet.TotalCount)
|
||||||
for _, result := range resultSet.List {
|
for _, result := range resultSet.List {
|
||||||
c.parseTrademeListing(result)
|
c.parseTrademeListing(result)
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ func (c *LocalConfig) loadConfig() {
|
|||||||
|
|
||||||
log.Printf("Loaded %d previously posted property IDs", len(c.PostedProperties))
|
log.Printf("Loaded %d previously posted property IDs", len(c.PostedProperties))
|
||||||
} else {
|
} else {
|
||||||
|
// Create empty map for first run
|
||||||
maps := make(map[int64]bool)
|
maps := make(map[int64]bool)
|
||||||
c.PostedProperties = maps
|
c.PostedProperties = maps
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user