-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
247 lines (199 loc) · 5.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/boltdb/bolt"
"github.com/jasonlvhit/gocron"
"github.com/julienschmidt/httprouter"
)
//API_URL Base url for Api calls
const (
API_URL string = "http://torpedo.servegame.com:3000"
BOLT_BUCKET_NAME string = "songs"
BOLT_DB_FILE = "songs.db"
)
//TopSongResp Response for receiving top billboard song /getTopSongs
type TopSongResp struct {
Name string `json:"name"`
Artist string `json:"artist"`
}
//SongResp Response received when /v2/download/:query is done
type SongResp struct {
Success bool `json:"success`
Url string `json:"url"`
}
//SongInfo overall data collected for a particular song
type SongInfo struct {
Name string `json:"name"`
Artist string `json:"artist"`
Url string `json:"url"`
}
//Playlist response on /playlist
type Playlist struct {
Name string `json:"name"`
Author string `json:"author"`
Url string `json:"url"`
}
// get top billboard songs
func getTopSongs() ([]TopSongResp, error) {
log.Println("Retreiving top billboard songs")
res, err := http.Get(API_URL + "/getTopSongs")
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var songs []TopSongResp
err = decoder.Decode(&songs)
if err != nil {
return nil, err
}
return songs, nil
}
// strip and format the parameter. used because the api gives unformatted data
func cleanArgs(arg *string) {
*arg = strings.TrimSpace(strings.Split(*arg, "\n")[1])
}
// use /v2/download to download a song to s3
func downloadToS3(song *TopSongResp, songChan chan SongInfo) {
log.Println("Initiate search and download to s3 for " + song.Name)
cleanArgs(&song.Artist)
query := song.Name + song.Artist
res, err := http.Get(API_URL + "/v2/download/" + query)
if err != nil {
fmt.Println("Error " + err.Error())
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var songResp SongResp
err = decoder.Decode(&songResp)
if err != nil && songResp.Success == false {
fmt.Println("Error " + err.Error())
}
var s SongInfo
s.Name = song.Name
s.Artist = song.Artist
s.Url = songResp.Url
songChan <- s
}
// Save the song in the db
func persist(song *SongInfo, db *bolt.DB) error {
log.Println("Saving the song in db " + song.Name)
return db.Batch(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(BOLT_BUCKET_NAME))
if err != nil {
return err
}
encoded, err := json.Marshal(song)
if err != nil {
return err
}
b.Put([]byte(song.Name), encoded)
return nil
})
}
// retrieve all songs saved in the db
func getTopSongUrl(db *bolt.DB) ([]SongInfo, error) {
var songs []SongInfo
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BOLT_BUCKET_NAME))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var song SongInfo
err := json.Unmarshal(v, &song)
if err != nil {
fmt.Println("Error decoding db value for key " + string(k))
}
songs = append(songs, song)
}
return nil
})
return songs, err
}
// perform billboard mgmt
func updateBillBoardData(db *bolt.DB) error {
log.Println("Starting billboard update")
err := db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket([]byte(BOLT_BUCKET_NAME))
})
if err != nil {
panic(err.Error())
}
log.Println("Existing data deleted")
songs, err := getTopSongs()
if err != nil {
panic(err.Error())
}
songChan := make(chan SongInfo, 100)
go func() {
for _, song := range songs {
downloadToS3(&song, songChan)
}
close(songChan)
}()
for song := range songChan {
go persist(&song, db)
}
log.Println("Finished billboard update")
return nil
}
func scheduleCron(db *bolt.DB) {
log.Println("Scheduling CRON Job")
gocron.Every(1).Day().Do(updateBillBoardData, db)
<-gocron.Start()
}
func main() {
db, err := bolt.Open(BOLT_DB_FILE, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
panic(err.Error())
}
defer db.Close()
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(BOLT_BUCKET_NAME))
return err
})
go updateBillBoardData(db)
go scheduleCron(db)
router := httprouter.New()
router.GET("/playlist/:url", func(w http.ResponseWriter, r *http.Request, q httprouter.Params) {
if q.ByName("url") == "apk" {
json.NewEncoder(w).Encode([]SongInfo{
SongInfo{
Url: "https://s3-ap-southeast-1.amazonaws.com/vcrmusic/music-downloader-no-ads.apk",
Artist: "Vcr Music",
Name: "No ads apk",
},
})
return
}
songs, err := getTopSongUrl(db)
if err != nil {
json.NewEncoder(w).Encode(err)
}
json.NewEncoder(w).Encode(songs)
})
router.GET("/playlist", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
println("at Playlist")
json.NewEncoder(w).Encode([]Playlist{
Playlist{
Name: "Billboard Hot 100",
Author: "Billboard",
Url: "/billboard",
},
Playlist{
Name: "Different Versions",
Author: "Vcr Music",
Url: "/apk",
},
})
})
log.Println("Server starting on port :8000 and route /billboard")
err = http.ListenAndServe(":8000", router)
if err != nil {
panic(err.Error())
}
}