-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (117 loc) · 3.08 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
148
149
150
151
package main
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/everdev/mack"
"github.com/therecipe/qt/widgets"
)
var (
path string
root string = "/Users/rekfuki/Documents/Coding/ImageShare/"
)
func main() {
logf, err := os.OpenFile(root+"bot-erros.log",
os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer logf.Close()
log.SetOutput(logf)
widgets.NewQApplication(len(os.Args), os.Args)
NewImageShareForm().Show()
var config Configuration
loadJSON(root+"config.json", &config)
var auth Authentication
loadJSON(root+"auth.json", &auth)
auth.Access_Token, auth.Refresh_Token, auth.Expires_In = getAccessToken(auth)
path = config.Path
previousSS := config.PreviousSS
log.Print(previousSS)
log.Print(path)
hasUpdated := false
go func() {
for {
hasUpdated, previousSS = update(path, previousSS)
if hasUpdated {
upload(config.Path, previousSS, auth.Access_Token)
}
time.Sleep(time.Second * 1)
}
}()
widgets.QApplication_Exec()
// sc := make(chan os.Signal, 1)
// signal.Notify(
// sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
// <-sc
config.PreviousSS = previousSS
file, err := json.Marshal(config)
checkErr(err)
err = ioutil.WriteFile(root+"config.json", file, 0644)
checkErr(err)
}
func update(path string, previousSS string) (bool, string) {
files, err := ioutil.ReadDir(path)
checkErr(err)
latestSS := files[len(files)-1].Name()
if latestSS != previousSS {
previousSS = latestSS
return true, previousSS
}
return false, previousSS
}
func getAccessToken(auth Authentication) (string, string, int64) {
authUrl := "https://api.imgur.com/oauth2/token"
resp, err := http.PostForm(authUrl,
url.Values{
"refresh_token": {auth.Refresh_Token}, "client_id": {auth.Client_Id},
"client_secret": {auth.Client_Secret}, "grant_type": {"refresh_token"}})
checkErr(err)
var authResp AuthResponse
err = json.NewDecoder(resp.Body).Decode(&authResp)
checkErr(err)
return authResp.Access_Token, authResp.Refresh_Token, authResp.Expires_In
}
func upload(path string, ssPath string, accessToken string) {
file, err := ioutil.ReadFile(path + ssPath)
checkErr(err)
imgString := base64.StdEncoding.EncodeToString(file)
authUrl := "https://api.imgur.com/3/image"
req, err := http.NewRequest("POST", authUrl, strings.NewReader(imgString))
checkErr(err)
req.Header.Add("Authorization", "Bearer "+accessToken)
client := &http.Client{}
resp, err := client.Do(req)
checkErr(err)
var uploadResponse UploadResponse
err = json.NewDecoder(resp.Body).Decode(&uploadResponse)
if err != nil {
switch err.Error() {
case "EOF":
upload(path, ssPath, accessToken)
return
default:
checkErr(err)
return
}
}
// checkErr(err)
if uploadResponse.Success {
link := uploadResponse.Data.Link
mack.SetClipboard(link)
mack.Beep(1)
err = mack.Notify(link, "Image has been uploaded to imgur")
checkErr(err)
updateHistoryList(link, ssPath, 1)
} else {
mack.Beep(1)
err = mack.Notify("Image failed to upload")
checkErr(err)
}
}