generated from gleich/go_template
/
github_scraper.go
62 lines (50 loc) 路 1.43 KB
/
github_scraper.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
package main
import (
"os"
"time"
"github.com/gleich/github_scraper/pkg/api"
"github.com/gleich/github_scraper/pkg/db"
"github.com/gleich/lumber/v2"
"github.com/go-co-op/gocron"
"github.com/shurcooL/githubv4"
"gorm.io/gorm"
)
func main() {
database, err := db.Connect()
if err != nil {
lumber.Fatal(err, "Failed to connect to database")
}
err = database.AutoMigrate(&db.Account{})
if err != nil {
lumber.Fatal(err, "Failed to auto migrate account")
}
client := api.Client()
accountAPIData, err := api.Account(client)
if err != nil {
lumber.Fatal(err, "Failed to fetch initial account information")
}
var account db.Account
database.FirstOrCreate(&account, accountAPIData)
s := gocron.NewScheduler(time.UTC)
scheduledCycle := func() { cycle(database, client, &account) }
if os.Getenv("DEV") == "true" {
_, err := s.Every(5).Seconds().Do(scheduledCycle)
if err != nil {
lumber.Error(err, "Failed to start scheduled cycle for every 5 seconds")
}
} else {
_, err := s.Every(5).Minutes().Do(scheduledCycle)
if err != nil {
lumber.Error(err, "Failed to start scheduled cycle for every 5 minutes")
}
}
s.StartBlocking()
}
func cycle(database *gorm.DB, client *githubv4.Client, account *db.Account) {
new_account_data, err := api.Account(client)
if err != nil {
lumber.Error(err, "Failed to load new account data")
}
database.Model(&account).Updates(new_account_data)
lumber.Success("Cycle completed")
}