-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (206 loc) · 6.51 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/gofiber/fiber/v2/log"
"github.com/tantoony/spotify-status-api/config"
"github.com/tantoony/spotify-status-api/database"
"github.com/tantoony/spotify-status-api/functions"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
var (
app *fiber.App
)
func main() {
router_auth := app.Group("/auth")
router_auth.Use(func(c *fiber.Ctx) error {
secretKey := c.GetReqHeaders()["Authorization"]
if len(secretKey) == 0 {
return c.SendString("No secret key provided")
}
if secretKey[0] == "" {
return c.SendString("Invalid secret key")
}
authorization_token := secretKey[0]
c.Locals("token", authorization_token)
return c.Next()
})
router_auth.Post("/spotify", func(c *fiber.Ctx) error {
data := functions.AuthorizeSpotify(c.Query("code"))
var authformat functions.AuthorizationResponse
if err := json.Unmarshal([]byte(data), &authformat); err != nil {
fmt.Println("error unmarshal authorization response")
fmt.Println(err)
}
if len(authformat.Error) > 0 {
return c.SendString(authformat.Error)
}
access_token := authformat.AccessToken
user := functions.FetchSpotifyUser(access_token)
var usermeta functions.UserMetaResponse
if err := json.Unmarshal([]byte(user), &usermeta); err != nil {
fmt.Println("error unmarshal usermeta response")
fmt.Println(err)
}
userformat := functions.UserResponse{
User: usermeta,
}
if len(userformat.User.Error) > 0 {
return c.SendString(userformat.User.Error)
}
c.JSON(userformat)
document, err := functions.FindUserDocumentByID(c, userformat.User.ID)
if err != nil {
fmt.Println("error find user document by id")
fmt.Println(err)
}
if err := functions.CreateAuthDocument(document.ID, 1, access_token); err != nil {
fmt.Println("error create auth document")
fmt.Println(err)
}
database.Redis.Set("key_spotify:"+document.ID, functions.Cryptit(access_token, false))
database.Redis.Expire("key_spotify:"+document.ID, 3600*time.Second)
return nil
})
router_spotify := app.Group("/spotify")
/* router_spotify.Use(func(c *fiber.Ctx) error {
secretKey := c.GetReqHeaders()["Authorization"]
if len(secretKey) == 0 {
return c.SendString("No secret key provided")
}
if secretKey[0] == "" {
return c.SendString("Invalid secret key")
}
authorization_token := secretKey[0]
c.Locals("token", authorization_token)
return c.Next()
}) */
router_spotify.Use("/:id", func(c *fiber.Ctx) error {
c.Locals("date", time.Now().String())
param := c.Params("id")
if len(param) == 0 {
return c.SendString("No id provided")
}
var docId string
if _id, err := database.Redis.Get("spotify:" + param); err != nil {
doc, _ := functions.FindUserDocumentByID(c, param)
docId = doc.ID
} else {
docId = _id
}
var token string
if key, err := database.Redis.Get("key_spotify:" + docId); err != nil || len(key) == 0 {
fmt.Println("Refreshing token...")
auth, _ := functions.FindAuthDocumentByRefID(docId, 1)
token = functions.RefreshToken(auth.Context)
} else {
token = functions.Cryptit(key, true)
}
c.Locals("token", token)
return c.Next()
})
router_spotify.Get("/", func(c *fiber.Ctx) error {
s := c.Locals("token")
if s == nil {
return c.SendString("No id provided")
} else {
s := s.(string)
return c.SendString(s)
}
})
router_spotify.Get("/:id", func(c *fiber.Ctx) error {
if c.Locals("token") == nil {
return c.SendString("No token provided")
}
now_playing_response := functions.UserPlaying(c.Locals("token").(string))
if len(now_playing_response) == 0 {
return c.SendString("No song playing")
}
var now_playing functions.UserPlayingResponse
if err := json.Unmarshal([]byte(now_playing_response), &now_playing); err != nil {
fmt.Println("error unmarshal now_playing response")
fmt.Println(err)
}
queue_response := functions.UserQueue(c.Locals("token").(string))
var queue functions.UserPlayerQueueResponse
if len(queue_response) == 0 {
return c.SendString("No song in queue")
}
if err := json.Unmarshal([]byte(queue_response), &queue); err != nil {
fmt.Println("error unmarshal queue response")
fmt.Println(err)
}
var response = new(functions.SpotifyResponse)
response.IsActive = now_playing.IsPlaying
response.Type = now_playing.Device.Type
response.ShuffleState = now_playing.ShuffleState
response.RepeatState = now_playing.RepeatState
response.IsPlaying = now_playing.IsPlaying
response.TimeStamp = time.Now().UnixMilli()
response.Song = now_playing.Item.Name
response.Progress.From = GTS(now_playing.ProgressMs)
response.Progress.To = GTS(now_playing.Item.DurationMs)
response.Artists = make([]functions.Artist, 0)
for _, artist := range now_playing.Item.Artists {
response.Artists = append(response.Artists, functions.Artist{
Name: artist.Name,
Url: artist.ExternalURLs.Spotify,
})
}
response.ProgressMs = now_playing.ProgressMs
response.DurationMs = now_playing.Item.DurationMs
response.Image.Url = now_playing.Item.Album.Images[0].URL
response.Image.Height = now_playing.Item.Album.Images[0].Height
response.Image.Width = now_playing.Item.Album.Images[0].Width
response.Url = now_playing.Item.ExternalURLs.Spotify
response.ReqTime = c.Locals("date").(string)
response.Queue = make([]functions.QueuedSong, 0)
for _, item := range queue.Queue {
response.Queue = append(response.Queue, functions.QueuedSong{
Name: item.Name,
Artists: item.Artists[0].Name,
Image: functions.Image{
Url: item.Album.Images[0].URL,
Height: item.Album.Images[0].Height,
Width: item.Album.Images[0].Width,
},
})
}
//response.Queue = response.Queue[1:]
return c.JSON(response)
})
if err := app.Listen(config.PORT); err != nil {
log.Info("Oops... Server is not running! Reason: %v", err)
} else {
log.Info("Server is running on port 3000...")
}
}
func init() {
config.InitializeEnv()
if err := database.MongoConnection(config.MONGO_URI, config.MONGO_DBNAME); err != nil {
log.Fatalf("Error connecting to MongoDB")
} else {
fmt.Println("MongoDB successfully connected...")
}
if err := database.RedisConnection(config.REDIS_URI); err != nil {
log.Fatalf("Error connecting to Redis")
} else {
fmt.Println("Redis successfully connected...")
}
app = fiber.New()
app.Use(cors.New())
}
func GTS(ms int) string {
seconds := ms / 1000
minutes := seconds / 60
seconds = seconds % 60
var str string
if seconds < 10 {
str = "0"
} else {
str = ""
}
return fmt.Sprintf("%d:%s%d", minutes, str, seconds)
}