From 1b9339c3713e71f840d29432b0050c783166df33 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Mon, 3 Jan 2022 18:30:45 +1300 Subject: [PATCH] Use reference for lastUpdatedTime --- internal/nzcovidbot/api.go | 9 +++++---- internal/nzcovidbot/nzcovidbot.go | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/internal/nzcovidbot/api.go b/internal/nzcovidbot/api.go index d508878..298f615 100644 --- a/internal/nzcovidbot/api.go +++ b/internal/nzcovidbot/api.go @@ -56,7 +56,7 @@ func fetchAPILocations() (ApiResponse, error) { } // Set user-agent info - req.Header.Set("User-Agent", "NZCovidBot/1.0 (https://m2.nz)") + req.Header.Set("User-Agent", "NZCovidBot/1.1 (https://m2.nz)") // Fire off the request resp, err := client.Do(req) @@ -82,6 +82,7 @@ func getNewAPILocations() { // Set lastUpdate time at the start of the request so we don't miss any locations // posted right after we poll newPollTime := time.Now() + previousPollTime := *lastUpdated // Pull latest data locations, err := fetchAPILocations() @@ -95,7 +96,7 @@ func getNewAPILocations() { // Iterate over the data and only find new locations for _, item := range locations.Items { - if item.PublishedAt.Unix() > lastUpdated.Unix() { + if item.PublishedAt.Unix() > previousPollTime.Unix() { // Clone the item to put in our own lil slice copy := item newItems[item.Location.City] = append(newItems[item.Location.City], copy) @@ -121,11 +122,11 @@ func getNewAPILocations() { postTheUpdates() } - updateLastUpdated(newPollTime) + updateLastUpdated(&newPollTime) } // updateLastUpdated - Creates/Updates lastUpdated.txt -func updateLastUpdated(newUpdated time.Time) { +func updateLastUpdated(newUpdated *time.Time) { // Make sure to update the global var for next poll lastUpdated = newUpdated diff --git a/internal/nzcovidbot/nzcovidbot.go b/internal/nzcovidbot/nzcovidbot.go index 5aab8fa..cfb98aa 100644 --- a/internal/nzcovidbot/nzcovidbot.go +++ b/internal/nzcovidbot/nzcovidbot.go @@ -10,7 +10,7 @@ import ( ) // Time of last succesful poll -var lastUpdated time.Time +var lastUpdated *time.Time // Main func func Lesgoooo() { @@ -40,7 +40,7 @@ func Lesgoooo() { } // getLastPollTime - If run previously, get last TS, otherwise Now() -func getLastPollTime() time.Time { +func getLastPollTime() *time.Time { // Set default of *now* if never run so we don't spam everything lastPoll := time.Now() @@ -50,19 +50,19 @@ func getLastPollTime() time.Time { b, err := ioutil.ReadAll(file) if err != nil { log.Printf("Unable to read lastUpdated.txt: %s", err) - return lastPoll + return &lastPoll } i, err := strconv.ParseInt(string(b), 10, 64) if err != nil { log.Printf("Unable to read lastUpdated.txt: %s", err) - return lastPoll + return &lastPoll } lastPoll = time.Unix(i, 0) } - return lastPoll + return &lastPoll } func postTheUpdates() {