-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
169 lines (149 loc) · 4.84 KB
/
init.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"io/ioutil"
"log"
"time"
firebase "firebase.google.com/go"
_ "github.com/lib/pq"
"google.golang.org/api/option"
"gopkg.in/yaml.v2"
)
const location = "Asia/Tokyo"
var (
// subjects.yaml で設定した教科情報
subjects subjectsYaml
// 現在の課題情報
hwStatus map[string][]interface{} = make(map[string][]interface{}, 0)
// 現在の課題リスト (ID)
hwList []string
// 前の課題情報
hwStatusPast map[string][]interface{} = make(map[string][]interface{}, 0)
// 前の課題リスト (ID)
hwListPast []string
)
// subjects.yaml のデータを格納する構造体
type subjectsYaml struct {
Subjects [][]string `yaml:"subjects"`
}
// GetHomeworks はAPIから取得したJSONを収納する構造体
type GetHomeworks struct {
Acquisition time.Time `json:"acquisition"`
Homeworks []HomeworkStruct `json:"assignments"`
}
// HomeworkStruct は1つの課題情報を収納する構造体
type HomeworkStruct struct {
Course string `json:"course"`
Subject string `json:"subject"`
SubjectID string `json:"subject_id"`
Name string `json:"name"`
ID string `json:"id"`
Due time.Time `json:"due"`
}
// SetSchedule はTimeTreeに追加するスケジュールを格納する構造体
type SetSchedule struct {
Data SetScheduleData `json:"data"`
}
// SetScheduleData はTimeTreeに追加するスケジュールの情報を格納する構造体
type SetScheduleData struct {
Attributes SetScheduleAttributes `json:"attributes"`
Relationships SetScheduleRelationships `json:"relationships"`
}
// SetScheduleAttributes は追加するスケジュールの属性を格納する構造体
type SetScheduleAttributes struct {
Category string `json:"category"`
Title string `json:"title"`
AllDay bool `json:"all_day"`
StartAt time.Time `json:"start_at"`
StartTimezone string `json:"start_timezone"`
EndAt time.Time `json:"end_at"`
EndTimezone string `json:"end_timezone"`
Description string `json:"description"`
}
// SetScheduleRelationships は追加するスケジュールの関連するものを格納する構造体
type SetScheduleRelationships struct {
Label SetScheduleLabel `json:"label"`
}
// SetScheduleLabel は追加するスケジュールのラベルを格納する構造体
type SetScheduleLabel struct {
Data SetScheduleLabelData `json:"data"`
}
// SetScheduleLabelData は追加するスケジュールのラベルのデータを格納する構造体
type SetScheduleLabelData struct {
ID string `json:"id"`
Type string `json:"type"`
}
// ResSetSchedule はスケジュール追加リクエストを出したときに返ってくるデータを収納する構造体
type ResSetSchedule struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Category string `json:"category"`
Title string `json:"title"`
AllDay bool `json:"all_day"`
StartAt time.Time `json:"start_at"`
StartTimezone string `json:"start_timezone"`
EndAt time.Time `json:"end_at"`
EndTimezone string `json:"end_timezone"`
Recurrences []interface{} `json:"recurrences"`
Description string `json:"description"`
Location string `json:"location"`
URL string `json:"url"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
} `json:"attributes"`
Relationships struct {
Creator struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
} `json:"creator"`
Label struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
} `json:"label"`
Attendees struct {
Data []struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
} `json:"attendees"`
} `json:"relationships"`
} `json:"data"`
}
func init() {
var err error
// subjects.yaml に格納されているデータを取得
yamlData, err := ioutil.ReadFile("subjects.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(yamlData, &subjects)
if err != nil {
panic(err)
}
// GAEはタイムゾーン指定できないので、Go側でタイムゾーンを指定する
loc, err := time.LoadLocation(location)
if err != nil {
loc = time.FixedZone(location, 9*60*60)
}
time.Local = loc
// Firebaseを初期化
ctx := context.Background()
sa := option.WithCredentialsFile("tcj2-kadai-store-ed48273c015c.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
// Firebaseから最後に起動していた時の課題情報を取得
dbGetKadai(ctx, client)
}