-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (56 loc) · 1.55 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/code-with-brian/frugal-thinker-sync/strava"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file:", err)
os.Exit(1)
}
}
func main() {
db, err := gorm.Open("sqlite3", os.Getenv("SQLITE_PATH"))
if err != nil {
fmt.Println("Error connecting to the database:", err)
os.Exit(1)
}
defer db.Close()
db.AutoMigrate(&strava.Activity{})
credentials := strava.StravaCredentials{
ClientID: os.Getenv("STRAVA_CLIENT_ID"),
ClientSecret: os.Getenv("STRAVA_SECRET"),
RefreshToken: os.Getenv("STRAVA_REFRESH_TOKEN"),
GrantType: "refresh_token",
}
newToken, err := strava.RefreshToken(credentials)
if err != nil {
fmt.Println("Error refreshing token:", err)
os.Exit(1)
}
activities, err := strava.FetchActivities(newToken.AccessToken)
if err != nil {
fmt.Println("Error fetching activities:", err)
os.Exit(1)
}
// Debug print to check the fetched activities
fmt.Printf("Fetched %d activities\n", len(activities))
for _, activity := range activities {
var existingActivity strava.Activity
if db.Where("id = ?", activity.ID).First(&existingActivity).RecordNotFound() {
err := db.Create(&activity).Error
if err != nil {
fmt.Println("Error saving activity to the database:", err)
} else {
fmt.Printf("Activity %d saved successfully\n", activity.ID)
}
} else {
fmt.Printf("Activity %d already exists, skipping\n", activity.ID)
}
}
}