-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
259 lines (217 loc) · 6.72 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
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"database/sql"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/cenkalti/backoff/v4"
_ "github.com/go-sql-driver/mysql"
"github.com/irlndts/go-discogs"
"github.com/pkg/errors"
"strconv"
//"github.com/disintegration/imaging"
"log"
"os"
)
type App struct {
Discogs discogs.Discogs
DynamoDB *dynamodb.DynamoDB
DynamoDBTracksTable string
DynamoDBCursorTable string
SQLDriver *sql.DB
Backoff backoff.BackOff
}
type LocalLabel struct {
ID int
LabelID int
HighestReleaseID int `json:"highest_dg_release"`
LabelReleases int `json:"label_releases"`
LabelTracks int `json:"label_tracks"`
MasterReleasesCache map[int]discogs.Release
LastPage int `json:"last_page"`
DidInit sql.NullBool `json:"did_init"`
MaxPages int
}
func getApp() (App, error) {
// MySQL
dbHost := os.Getenv("DB_HOST")
dbUser := os.Getenv("DB_USERNAME")
dbPass := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
sqlDriver, err := sql.Open("mysql", dbUser+":"+dbPass+"@tcp("+dbHost+")/"+dbName+"?parseTime=true")
if err != nil {
return App{}, errors.Wrap(err, "failed to set up DB client")
}
// DynamoDB
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
dynamoClient := dynamodb.New(sess, &aws.Config{
Region: aws.String("eu-west-1"),
})
// Discogs
discogsToken := os.Getenv("DG_TOKEN")
discogsClient, err := discogs.New(&discogs.Options{
UserAgent: "Mirror.FM",
Token: discogsToken,
})
if err != nil {
return App{}, errors.Wrap(err, "failed to set up discogs client")
}
return App{
Discogs: discogsClient,
DynamoDB: dynamoClient,
DynamoDBTracksTable: "mirrorfm_dg_tracks",
DynamoDBCursorTable: "mirrorfm_cursors",
SQLDriver: sqlDriver,
Backoff: backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 100),
}, nil
}
func Handler(ctx context.Context, evt events.SNSEvent) error {
app, err := getApp()
if err != nil {
return errors.Wrap(err, "failed to start up app")
}
var rowId int
labelId, rowId, err := app.findNextLabelToProcess(evt)
if err != nil {
return errors.Wrap(err, "failed to find next label to process")
}
label, err := app.GetLabel(labelId)
if err != nil {
return errors.Wrap(err, "failed to retrieve label")
}
err = app.UpdateLabelWithThumbnail(label)
if err != nil {
return errors.Wrap(err, "failed to update label with thumbnail")
}
localLabel, err := app.GetLabelInfo(label.ID)
if err != nil {
return errors.Wrap(err, "failed to get label info")
}
for {
releases, err := app.GetLabelReleases(localLabel.LastPage, labelId)
if err != nil {
return errors.Wrap(err, "failed to get label releases")
}
localLabel.MaxPages = releases.Pagination.Pages
log.Printf("Page %d/%d\n", localLabel.LastPage, localLabel.MaxPages)
uniqueMasterReleases, skipped, err := app.populateUniqueMasterReleases(releases, localLabel)
if err != nil {
return errors.Wrap(err, "failed to populate unique master releases")
}
log.Printf("Skipped %d, kept: %+v\n", skipped, uniqueMasterReleases)
err = app.persistReleasesTracks(localLabel, uniqueMasterReleases)
if err != nil {
return errors.Wrap(err, "failed to persist releases tracks")
}
localLabel.LastPage += 1
isLastPage := localLabel.LastPage > localLabel.MaxPages
// Save stats and cursors after each page, so lambda timeouts are no problem!
err = app.UpdateLabelWithStats(localLabel, isLastPage)
if err != nil {
return err
}
if isLastPage {
break
}
}
if rowId > 0 {
return app.SaveCursor("from_discogs_last_successful_label", rowId)
}
return nil
}
func (client *App) findNextLabelToProcess(evt events.SNSEvent) (int, int, error) {
if len(evt.Records) > 0 {
labelIdStr := evt.Records[0].SNS.Message
res, err := strconv.Atoi(labelIdStr)
if err != nil {
return 0, 0, errors.Wrap(err, "failed to convert label LabelID to int")
}
return res, 0, nil
}
cursor, err := client.GetCursor("from_discogs_last_successful_label")
if err != nil {
return 0, 0, errors.Wrap(err, "failed to retrieve cursor")
}
fmt.Printf("last was %d\n", cursor)
label, err := client.GetNextLabel(cursor)
if err != nil {
return 0, 0, err
}
return label.LabelID, label.ID, nil
}
func (client *App) populateUniqueMasterReleases(releases *discogs.LabelReleases, localLabel LocalLabel) ([]int, int, error) {
var uniqueMasterReleases []int
var skipped int
for _, labelRelease := range releases.Releases {
id := labelRelease.ID
if isReleaseAlreadyStored(id, localLabel) {
skipped += 1
continue
}
localLabel.HighestReleaseID = id
release, err := client.GetRelease(id)
if err != nil {
return uniqueMasterReleases, skipped, err
}
var masterID int
if release.MasterID == 0 {
masterID = id
} else {
masterID = release.MasterID
}
if _, ok := localLabel.MasterReleasesCache[masterID]; !ok {
uniqueMasterReleases = append(uniqueMasterReleases, masterID)
localLabel.MasterReleasesCache[masterID] = *release
log.Printf("%d => %d\n", id, masterID)
}
}
return uniqueMasterReleases, skipped, nil
}
func (client *App) persistReleasesTracks(localLabel LocalLabel, uniqueMasterReleases []int) error {
for _, masterReleaseId := range uniqueMasterReleases {
if alreadyStored, err := client.isMasterReleaseAlreadyStored(localLabel.LabelID, masterReleaseId); err != nil {
return err
} else if alreadyStored {
fmt.Println("Already stored")
continue
}
fmt.Printf("tracks in %d %d\n", masterReleaseId, len(localLabel.MasterReleasesCache[masterReleaseId].Tracklist))
err := client.AddTracks(localLabel.MasterReleasesCache[masterReleaseId], masterReleaseId, localLabel.LabelID)
if err != nil {
return err
}
localLabel.LabelReleases += 1
localLabel.LabelTracks += len(localLabel.MasterReleasesCache[masterReleaseId].Tracklist)
}
return nil
}
func isReleaseAlreadyStored(releaseId int, localLabel LocalLabel) bool {
// Here we assume that old releases that were recently added to a label
// will have a new/high release LabelID.
return localLabel.DidInit.Bool && releaseId <= localLabel.HighestReleaseID
}
func main() {
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") != "" {
lambda.Start(Handler)
} else {
// Also handle loop
err := Handler(context.TODO(), events.SNSEvent{
Records: []events.SNSEventRecord{
//{
// SNS: events.SNSEntity{
// Message: "720419", // 77423
// },
//},
},
})
if err != nil {
fmt.Println(err.Error())
}
}
}