-
Notifications
You must be signed in to change notification settings - Fork 1
/
drive.go
119 lines (87 loc) · 2.54 KB
/
drive.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
package main
import (
"github.com/tealeg/xlsx"
"google.golang.org/api/drive/v2"
"io"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/net/context"
)
var eventFilePath = os.Getenv("HOME") + "/Event Schedule.xlsx"
const (
MIN_NECESSARY_CELL_SIZE = 3
EXPECTED_EXPORT_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
func downloadFile(d *drive.Service, t http.RoundTripper, f *drive.File) {
downloadUrl := f.DownloadUrl
if downloadUrl == "" {
log.Fatal("An error occurred: File is not downloadable")
}
req, _ := http.NewRequest("GET", downloadUrl, nil)
resp, _ := t.RoundTrip(req)
defer resp.Body.Close()
file, _ := os.Create(eventFilePath)
defer file.Close()
if _, err := io.Copy(file, resp.Body); err != nil {
log.Fatal(err)
}
}
func getEventListInEventFile() []EventInformation {
eventListFromFile := make([]EventInformation, 20)
xlFile, err := xlsx.OpenFile(eventFilePath)
if err != nil {
log.Fatalf("Can not read file: %v", err)
}
for i, sheet := range xlFile.Sheets {
for _, row := range sheet.Rows {
if len(row.Cells) >= MIN_NECESSARY_CELL_SIZE {
date, _ := row.Cells[0].FormattedValue()
title, _ := row.Cells[1].FormattedValue()
description, _ := row.Cells[2].FormattedValue()
if isConvenientEvent(i, date, description) {
eventListFromFile = append(eventListFromFile, EventInformation{
date: date,
title: title,
description: description,
location: EVENT_LOCATION,
})
}
}
}
}
return eventListFromFile
}
func isConvenientEvent(cellIndex int, date, description string) bool {
return cellIndex == 0 && date != "" && date != "Date" && description != "";
}
func getDownloadUrlByName(driveService *drive.Service, name string) string {
file, err := driveService.Files.List().Do()
if err != nil {
log.Fatalf("Unable to retrieve files.", err)
}
for _, f := range file.Items {
if f.Title == name {
exportLinks := f.ExportLinks
return exportLinks[EXPECTED_EXPORT_MIME_TYPE]
}
}
panic("File Not Found in Google Drive")
}
func createDriveService(client *http.Client) (*drive.Service, error) {
return drive.New(client)
}
func createTransport() *oauth2.Transport {
cacheFile, err := tokenCacheFile()
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
config := getConfig()
token, err := tokenFromFile(cacheFile)
tokenSource := config.TokenSource(context.Background(), token)
return &oauth2.Transport{
Source:tokenSource,
Base:http.DefaultTransport,
}
}