mirror of
https://github.com/idanoo/NZCovidBot
synced 2025-07-02 19:52:14 +00:00
Order by exposure date
This commit is contained in:
parent
903012231a
commit
a884ed7f46
2 changed files with 39 additions and 4 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -15,10 +16,11 @@ type UpdatedLocations struct {
|
||||||
|
|
||||||
// Updated data
|
// Updated data
|
||||||
type UpdatedRow struct {
|
type UpdatedRow struct {
|
||||||
ChangeType string // ADDED, REMOVED, MODIFIED
|
ChangeDate time.Time // To order b
|
||||||
DiscordData string // Formatted Row data
|
ChangeType string // ADDED, REMOVED, MODIFIED
|
||||||
TwitterData string // Formatted Row data
|
DiscordData string // Formatted Row data
|
||||||
SlackData string // Formatted Row data
|
TwitterData string // Formatted Row data
|
||||||
|
SlackData string // Formatted Row data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Struct of updated locations
|
// Struct of updated locations
|
||||||
|
@ -26,7 +28,10 @@ var updatedLocations UpdatedLocations
|
||||||
|
|
||||||
// parseCsvRow Build into struct for output later
|
// parseCsvRow Build into struct for output later
|
||||||
func parseCsvRow(changeType string, data string) {
|
func parseCsvRow(changeType string, data string) {
|
||||||
|
parsedTime := parseTimeFromRow(data)
|
||||||
|
|
||||||
newRow := UpdatedRow{
|
newRow := UpdatedRow{
|
||||||
|
ChangeDate: parsedTime,
|
||||||
ChangeType: changeType,
|
ChangeType: changeType,
|
||||||
DiscordData: formatCsvDiscordRow(data),
|
DiscordData: formatCsvDiscordRow(data),
|
||||||
TwitterData: formatCsvTwitterRow(data),
|
TwitterData: formatCsvTwitterRow(data),
|
||||||
|
@ -36,6 +41,12 @@ func parseCsvRow(changeType string, data string) {
|
||||||
updatedLocations.Locations = append(updatedLocations.Locations, newRow)
|
updatedLocations.Locations = append(updatedLocations.Locations, newRow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func orderRowDataByDate() {
|
||||||
|
sort.Slice(updatedLocations.Locations, func(i, j int) bool {
|
||||||
|
return updatedLocations.Locations[i].ChangeDate.Before(updatedLocations.Locations[j].ChangeDate)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// formatCsvDiscordRow Format the string to a tidy string for the interwebs
|
// formatCsvDiscordRow Format the string to a tidy string for the interwebs
|
||||||
func formatCsvDiscordRow(data string) string {
|
func formatCsvDiscordRow(data string) string {
|
||||||
c := parseRawRowData(data)
|
c := parseRawRowData(data)
|
||||||
|
@ -54,6 +65,28 @@ func formatCsvSlackRow(data string) string {
|
||||||
return fmt.Sprintf("*%s* %s on _%s_ - _%s_", c[2], c[3], c[0], c[1])
|
return fmt.Sprintf("*%s* %s on _%s_ - _%s_", c[2], c[3], c[0], c[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseTimeFromRow(data string) time.Time {
|
||||||
|
r := csv.NewReader(strings.NewReader(data))
|
||||||
|
r.Comma = ','
|
||||||
|
fields, err := r.Read()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
c := make([]string, 0)
|
||||||
|
c = append(c, fields...)
|
||||||
|
|
||||||
|
starttime := c[4]
|
||||||
|
st, err := time.Parse("02/01/2006, 3:04 pm", starttime)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
return time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
|
||||||
// Returns []string of parsed
|
// Returns []string of parsed
|
||||||
func parseRawRowData(data string) []string {
|
func parseRawRowData(data string) []string {
|
||||||
output := make([]string, 0)
|
output := make([]string, 0)
|
||||||
|
|
|
@ -34,6 +34,8 @@ func Lesgoooo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func postTheUpdates() {
|
func postTheUpdates() {
|
||||||
|
orderRowDataByDate()
|
||||||
|
|
||||||
// Twitter
|
// Twitter
|
||||||
go postToTwitter()
|
go postToTwitter()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue