-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (129 loc) · 3.72 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
)
// 環境変数のグローバル変数宣言
var (
LINE_TOKEN string
GITHUB_TOKEN string
GITHUB_USER string
URL = "https://api.github.com/graphql"
QUERY string
)
// Define a struct to match the JSON structure
type Contributions struct {
Contributions [][]struct {
Date string `json:"date"`
ContributionCount int `json:"contributionCount"`
} `json:"contributions"`
}
// Define a struct for storing the data you need
type ContributionData struct {
Date string
ContributionCount int
}
type GithubContribution struct {
Data struct {
User struct {
ContributionsCollection struct {
ContributionCalendar struct {
Weeks []struct {
ContributionDays []struct {
Color string `json:"color"`
ContributionCount int `json:"contributionCount"`
Date string `json:"date"`
Weekday int `json:"weekday"`
} `json:"contributionDays"`
} `json:"weeks"`
} `json:"contributionCalendar"`
} `json:"contributionsCollection"`
} `json:"user"`
} `json:"data"`
}
func init() {
// .envファイルの読み込み
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// 環境変数の取得
LINE_TOKEN = os.Getenv("LINE_TOKEN")
GITHUB_TOKEN = os.Getenv("GITHUB_TOKEN")
GITHUB_USER = os.Getenv("GITHUB_USER")
// GraphQLのクエリ
QUERY = fmt.Sprintf(`
{
user(login: "%s") {
contributionsCollection {
contributionCalendar {
weeks {
contributionDays {
color
contributionCount
date
weekday
}
}
}
}
}
}`, GITHUB_USER)
}
func main() {
requestBody, err := json.Marshal(map[string]string{"query": QUERY})
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("POST", URL, strings.NewReader(string(requestBody)))
if err != nil {
log.Fatal(err)
}
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", GITHUB_TOKEN))
client := new(http.Client)
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var githubContribution GithubContribution
if err := json.NewDecoder(response.Body).Decode(&githubContribution); err != nil {
log.Fatal(err)
}
yesterdayContribution := 0
todayContribution := 0
for _, week := range githubContribution.Data.User.ContributionsCollection.ContributionCalendar.Weeks {
for _, day := range week.ContributionDays {
// 昨日の日付を取得
if day.Date == time.Now().AddDate(0, 0, -1).Format("2006-01-02") {
fmt.Print("昨日のコントリビューション数:")
fmt.Println(day.ContributionCount)
yesterdayContribution = day.ContributionCount
}
if day.Date == time.Now().Format("2006-01-02") {
fmt.Print("今日のコントリビューション数:")
fmt.Println(day.ContributionCount)
todayContribution = day.ContributionCount
}
}
}
fmt.Print("昨日までの連続コントリビューション日数:")
var continueDays = 0
for _, week := range githubContribution.Data.User.ContributionsCollection.ContributionCalendar.Weeks {
for _, day := range week.ContributionDays {
if day.ContributionCount == 0 && day.Date != time.Now().Format("2006-01-02") {
continueDays = 0
} else if day.Date != time.Now().Format("2006-01-02") {
continueDays++
}
}
}
fmt.Println(continueDays)
SendLine(yesterdayContribution, continueDays, todayContribution)
}