/
data.go
129 lines (101 loc) · 3.44 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/antihax/optional"
"github.com/eholzbach/strava"
_ "github.com/mattn/go-sqlite3"
)
// connectDB creates a connection to sqlite db, creates the table if it does not exist, and returns type DB
func connectDB(dbpath string) (*sql.DB, error) {
// open db
db, err := sql.Open("sqlite3", dbpath)
if err != nil {
return nil, err
}
// create table if it doesn't exist
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS sp (ID INTEGER PRIMARY KEY, Name VARCHAR, Distance FLOAT, MovingTime INT, ElapsedTime INT, TotalElevationGain FLOAT, Type VARCHAR, StravaID INT UNIQUE, StartDate DATETIME, StartDateLocal DATETIME, Timezone VARCHAR, MapId VARCHAR, MapPolyline VARCHAR, MapSummaryPolyline VARCHAR, AverageSpeed FLOAT, MaxSpeed FLOAT, AveragePower FLOAT, Kilojoules FLOAT, GearId VARCHAR )")
if err != nil {
return nil, err
}
statement.Exec()
return db, err
}
// getPolylines queries the db for polyline data and returns it in a slice of strings
func getPolylines(config Config, db *sql.DB) ([]string, error) {
var data []string
// query db for all polylines
rows, err := db.Query("SELECT MapPolyline FROM sp;")
if err != nil {
return data, err
}
defer rows.Close()
for rows.Next() {
var p string
err = rows.Scan(&p)
if err != nil {
return data, err
}
data = append(data, p)
}
return data, err
}
// updateDB checks for new strava data and writes it into the database
func updateDB(oauth context.Context, config Config, db *sql.DB) (err error) {
// get time of most recent activity in db
row, err := db.Query("SELECT StartDate FROM sp ORDER BY ID DESC LIMIT 1;")
if err != nil {
return err
}
var ts time.Time
for row.Next() {
err = row.Scan(&ts)
if err != nil {
return err
}
}
// if empty set to epoch
t, _ := time.Parse("2006-01-02 15:04:05", "0001-01-01 00:00:00")
if ts == t {
ts, _ = time.Parse("1/2/2006 15:04:05", "1/1/1970 12:00:00")
}
// strava api after function expects int
timestamp := int(ts.Unix())
// build strava api client
client := strava.NewAPIClient(strava.NewConfiguration())
// get new activities from strava
opts := &strava.GetLoggedInAthleteActivitiesOpts{
PerPage: optional.NewInt32(200),
After: optional.NewInt32(int32(timestamp)),
}
activities, _, err := client.ActivitiesApi.GetLoggedInAthleteActivities(oauth, opts)
if err != nil {
fmt.Println(err)
return err
}
// write new activities to db
for _, v := range activities {
// only bicycles, only if gps data
if *v.Type_ == "Ride" && len(v.Map_.SummaryPolyline) > 0 {
// get full activity
opts := &strava.GetActivityByIdOpts{
IncludeAllEfforts: optional.NewBool(true),
}
a, _, err := client.ActivitiesApi.GetActivityById(oauth, v.Id, opts)
if err != nil {
return err
}
statement, err := db.Prepare("INSERT OR IGNORE INTO sp (Name, Distance, MovingTime, ElapsedTime, TotalElevationGain, Type, StravaID, StartDate, StartDateLocal, Timezone, MapId, MapPolyline, MapSummaryPolyline, AverageSpeed, MaxSpeed, Kilojoules, GearId) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
}
_, err = statement.Exec(a.Name, a.Distance, a.MovingTime, a.ElapsedTime, a.TotalElevationGain, a.Type_, a.Id, a.StartDate, a.StartDateLocal, a.Timezone, a.Map_.Id, a.Map_.Polyline, a.Map_.SummaryPolyline, a.AverageSpeed, a.MaxSpeed, a.Kilojoules, a.GearId)
if err != nil {
return err
}
}
}
return nil
}