-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
124 lines (97 loc) · 2.64 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
package main
import (
"errors"
"fmt"
"os"
"pin-creator/accessToken"
"pin-creator/config"
"pin-creator/pinterest"
"pin-creator/schedule"
log "github.com/sirupsen/logrus"
)
var cfg *config.Config
func main() {
readConfig()
log.Infof("Checking for pins to create in '%s'", cfg.ScheduleFilePath)
scheduleReader := schedule.NewScheduleReader(cfg.ScheduleFilePath)
nextPinData, err := scheduleReader.Next()
if err != nil {
log.Fatal(err.Error())
}
if nextPinData == nil {
log.Info("No pin scheduled for creation")
return
}
createPin(nextPinData)
err = scheduleReader.SetCreated(nextPinData.Index)
if err != nil {
log.Fatalf("Error setting pin created to true. Error: %s", err.Error())
}
}
func readConfig() {
args := os.Args
if len(args) != 2 {
log.Fatalf("config.yaml file not provided")
}
configFilePath := args[1]
cr := config.NewReader(configFilePath)
c, err := cr.Read()
if err != nil {
log.Fatal(err.Error())
}
cfg = c
}
func getToken() string {
fmt.Println(cfg)
tokenFileHandler := accessToken.NewAccessTokenFileHandler(cfg.AccessTokenPath)
log.Info("Reading access token from file")
token, err := tokenFileHandler.Read()
if err == nil {
return token
} else {
log.Info("No access token file found. Creating new token")
tokenCreator := accessToken.NewAccessAccessTokenCreator(cfg.BrowserPath, cfg.RedirectPort)
appId := os.Getenv("APP_ID")
appSecret := os.Getenv("APP_SECRET")
token, err := tokenCreator.NewToken(appId, appSecret)
if err != nil {
log.Fatalf("error creating new access token. Error: %s", err.Error())
}
log.Info("Writing access token to file")
tokenFileHandler.Write(token)
return token
}
}
func createPin(scheduledPinData *schedule.NextPinData) {
token := getToken()
client := pinterest.NewClient(token)
boards, err := client.ListBoards()
if err != nil {
log.Fatal(err.Error())
}
boardId, err := boardIdByName(boards, scheduledPinData.BoardName)
if err != nil {
log.Fatal(err.Error())
}
pinData := pinterest.PinData{
BoardId: boardId,
ImgPath: scheduledPinData.ImagePath,
Link: scheduledPinData.Link,
Title: scheduledPinData.Title,
Description: scheduledPinData.Description,
AltText: scheduledPinData.Description,
}
err = client.CreatePin(pinData)
if err != nil {
log.Fatal(err.Error())
}
log.Infof("Created Pin '%s' in board '%s'\n", pinData.Title, scheduledPinData.BoardName)
}
func boardIdByName(boards []pinterest.BoardInfo, boardName string) (string, error) {
for _, board := range boards {
if board.Name == boardName {
return board.Id, nil
}
}
return "", errors.New(fmt.Sprintf("board %s not found\n", boardName))
}