-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweeklyProgressEmail.js
414 lines (356 loc) · 16.2 KB
/
weeklyProgressEmail.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
const request = require('request');
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
async function getSecrets(secretName) {
const region = "us-east-1";
const client = new AWS.SecretsManager({
region: region
});
try {
console.log('Getting secrets');
let secret;
const data = await client.getSecretValue({ SecretId: secretName }).promise();
if (data.SecretString) secret = data.SecretString;
return secret ? JSON.parse(secret) : secret;
} catch (err) {
if (err.code === 'ResourceNotFoundException') {
console.log(`The requested secret ${secretName} was not found`);
} else if (err.code === 'InvalidRequestException') {
console.log(`The request was invalid due to: ${err.message}`);
} else if (err.code === 'InvalidParameterException') {
console.log(`The request had invalid params: ${err.message}`);
}
}
}
async function refreshSpotifyAccessToken(spotifySecrets, refreshToken) {
var authOptions = {
url: "https://accounts.spotify.com/api/token",
headers: {
Authorization: `Basic ${Buffer.from(
`${spotifySecrets.SPOTIFY_CLIENT_ID}:${spotifySecrets.SPOTIFY_CLIENT_SECRET}`
).toString("base64")}`,
},
form: {
grant_type: "refresh_token",
refresh_token: refreshToken,
},
json: true,
};
return new Promise((resolve, reject) => {
request.post(authOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
var token = body.access_token;
resolve(token);
}
});
});
}
async function getUser (pk) {
const queryParams = {
TableName: 'radia-users',
KeyConditionExpression: 'pk = :pk and sk = :sk',
ExpressionAttributeValues: {
':pk': pk,
':sk': `Auth|${pk}`
},
};
const user = await docClient.query(queryParams).promise();
return user.Items[0];
}
async function getSpotifyNewMusic(accessToken) {
var options = {
url: `https://api.spotify.com/v1/browse/new-releases?limit=50`,
headers: {
Authorization: `Bearer ${accessToken}`,
},
json: true
};
return new Promise((resolve, reject) => {
request.get(options, function (error, response, body) {
if (error) {
resolve({error: true, message: "Something went wrong with the request. Try again.", statusCode: 500});
}
if (!error && response.statusCode === 401) {
resolve({error: true, message: "Unauthorized", statusCode: 401});
}
if (!error && response.statusCode === 200) {
resolve(body);
}
});
});
}
async function getRelatedSpotifyArtists(accessToken, id) {
var options = {
url: `https://api.spotify.com/v1/artists/${id}/related-artists`,
headers: {
Authorization: `Bearer ${accessToken}`,
},
json: true
};
return new Promise((resolve, reject) => {
request.get(options, function (error, response, body) {
if (error) {
resolve({error: true, message: "Something went wrong with the request. Try again.", statusCode: 500});
}
if (!error && response.statusCode === 401) {
resolve({error: true, message: "Unauthorized", statusCode: 401});
}
if (!error && response.statusCode === 200) {
resolve(body);
}
});
});
}
async function getSpotifyArtistTopTracks(accessToken, id) {
var options = {
url: `https://api.spotify.com/v1/artists/${id}/top-tracks?market=us`,
headers: {
Authorization: `Bearer ${accessToken}`,
},
json: true
};
return new Promise((resolve, reject) => {
request.get(options, function (error, response, body) {
if (error) {
resolve({error: true, message: "Something went wrong with the request. Try again.", statusCode: 500});
}
if (!error && response.statusCode === 401) {
resolve({error: true, message: "Unauthorized", statusCode: 401});
}
if (!error && response.statusCode === 200) {
resolve(body);
}
});
});
}
async function getUserCollectiblesWithinLastSevenDays(pk, sevenDaysAgo) {
const queryParams = {
TableName: 'radia-users',
KeyConditionExpression: 'pk = :pk and begins_with(sk, :sk)',
FilterExpression: 'updated > :sevenDaysAgo',
ExpressionAttributeValues: {
':pk': pk,
':sk': `Collectible|spotify|`,
':sevenDaysAgo': sevenDaysAgo
},
};
const collectibles = await docClient.query(queryParams).promise();
return collectibles;
}
async function getUserStreamedMillisecondsCollectiblesWithinLastSevenDays(pk, sevenDaysAgo) {
const queryParams = {
TableName: 'radia-users',
KeyConditionExpression: 'pk = :pk and begins_with(sk, :sk)',
FilterExpression: 'updated > :sevenDaysAgo',
ExpressionAttributeValues: {
':pk': pk,
':sk': `Collectible|spotify|streamedMilliseconds`,
':sevenDaysAgo': sevenDaysAgo
},
};
const collectibles = await docClient.query(queryParams).promise();
return collectibles;
}
async function getUserAlbumsWithinLastSevenDays(pk, sevenDaysAgo) {
const queryParams = {
TableName: 'radia-users',
KeyConditionExpression: 'pk = :pk and begins_with(sk, :sk)',
FilterExpression: 'updated > :sevenDaysAgo',
ExpressionAttributeValues: {
':pk': pk,
':sk': `Album|`,
':sevenDaysAgo': sevenDaysAgo
},
};
const albums = await docClient.query(queryParams).promise();
return albums;
}
async function getUserTracksWithinLastSevenDays(pk, sevenDaysAgo) {
const queryParams = {
TableName: 'radia-users',
KeyConditionExpression: 'pk = :pk and begins_with(sk, :sk)',
FilterExpression: 'updated > :sevenDaysAgo',
ExpressionAttributeValues: {
':pk': pk,
':sk': `Track|`,
':sevenDaysAgo': sevenDaysAgo
},
};
const tracks = await docClient.query(queryParams).promise();
return tracks;
}
const calculateProgress = (collectible) => {
if (collectible.streamedMilliseconds <= 3600000 ) {
if (parseInt(((collectible.streamedMilliseconds / 3600000) * 100).toFixed(1)) >= 1)
return ((collectible.streamedMilliseconds / 3600000) * 100).toFixed(0);
else
return ((collectible.streamedMilliseconds / 3600000) * 100).toFixed(1);
}
if (collectible.streamedMilliseconds >= 3600000 && collectible.streamedMilliseconds < 3600000 * 5) {
return ((collectible.streamedMilliseconds / (3600000 * 5)) * 100).toFixed(0);
}
if (collectible.streamedMilliseconds >= 3600000 * 5 && collectible.streamedMilliseconds < 3600000 * 10) {
return ((collectible.streamedMilliseconds / (3600000 * 10)) * 100).toFixed(0);
}
if (collectible.streamedMilliseconds >= 3600000 * 10 && collectible.streamedMilliseconds < 3600000 * 15) {
return ((collectible.streamedMilliseconds / (3600000 * 15)) * 100).toFixed(0);
}
if (collectible.streamedMilliseconds >= 3600000 * 15 && collectible.streamedMilliseconds < 3600000 * 25) {
return ((collectible.streamedMilliseconds / (3600000 * 25)) * 100).toFixed(0);
}
};
const msToTime = (duration) => {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (hours && hours !== '00')
return `${hours} hours ${minutes} minutes`;
if (minutes && minutes !== '00')
return `${minutes} minutes`;
if (seconds && seconds !== '00')
return `${seconds} seconds`;
};
const calculateTimeLeft = (collectible) => {
if (collectible.streamedMilliseconds <= 3600000 ) {
const timeLeft = msToTime(3600000 - collectible.streamedMilliseconds);
return {timeLeft, name: `${collectible.artist.name} - 1 Hour Listening`};
}
if (collectible.streamedMilliseconds >= 3600000 && collectible.streamedMilliseconds < 3600000 * 5) {
const timeLeft = msToTime(3600000 * 5 - collectible.streamedMilliseconds);
return {timeLeft, name: `${collectible.artist.name} - 5 Hours Listening`};
}
if (collectible.streamedMilliseconds >= 3600000 * 5 && collectible.streamedMilliseconds < 3600000 * 10) {
const timeLeft = msToTime(3600000 * 10 - collectible.streamedMilliseconds);
return {timeLeft, name: `${collectible.artist.name} - 10 Hours Listening`};
}
if (collectible.streamedMilliseconds >= 3600000 * 10 && collectible.streamedMilliseconds < 3600000 * 15) {
const timeLeft = msToTime(3600000 * 15 - collectible.streamedMilliseconds);
return {timeLeft, name: `${collectible.artist.name} - 15 Hours Listening`};
}
if (collectible.streamedMilliseconds >= 3600000 * 15 && collectible.streamedMilliseconds < 3600000 * 25) {
const timeLeft = msToTime(3600000 * 25 - collectible.streamedMilliseconds);
return {timeLeft, name: `${collectible.artist.name} - 25 Hours Listening`};
}
};
const sendEmail = (radiaSecrets, emailAddress, data) => {
return new Promise((resolve, reject) => {
var options = {
url: "https://qk8wia3761.execute-api.us-east-1.amazonaws.com/prod/email/send",
headers: {
'x-api-key': radiaSecrets.RADIA_SERVER_API_KEY,
},
json: {
"templateName": "Weekly Progress Email",
"subject": "Your Weekly Progress 📊",
"templateContent": [
{"name": "artist_count", "content": data.numberOfArtists},
{"name": "album_count", "content": data.numberOfAlbums},
{"name": "track_count", "content": data.numberOfTracks},
{"name": "collectible_count", "content": data.numberOfCompletedCollectibles},
{"name": "hours_away_01", "content": data.closeToEarning[0].timeLeft},
{"name": "hours_away_02", "content": data.closeToEarning[1].timeLeft},
{"name": "hours_away_03", "content": data.closeToEarning[2].timeLeft},
{"name": "nft_name_01", "content": data.closeToEarning[0].name},
{"name": "nft_name_02", "content": data.closeToEarning[1].name},
{"name": "nft_name_03", "content": data.closeToEarning[2].name},
{"name": "top_pick_img_01", "content": `<a href="https://beta.radia.world/artist/${data.topPicks[0].artists[0].id}" target="_blank" rel="noopener noreferrer"><img src="${data.topPicks[0].images[0].url}" style="border-radius: 8px; border: 0px; width: 156px; height: 156px; margin: 0px;"/></a>`},
{"name": "top_pick_img_02", "content": `<a href="https://beta.radia.world/artist/${data.topPicks[1].artists[0].id}" target="_blank" rel="noopener noreferrer"><img src="${data.topPicks[1].images[0].url}" style="border-radius: 8px; border: 0px; width: 156px; height: 156px; margin: 0px;"/></a>`},
{"name": "top_pick_img_03", "content": `<a href="https://beta.radia.world/artist/${data.topPicks[2].artists[0].id}" target="_blank" rel="noopener noreferrer"><img src="${data.topPicks[2].images[0].url}" style="border-radius: 8px; border: 0px; width: 156px; height: 156px; margin: 0px;"/></a>`},
{"name": "top_pick_text_01", "content": `<span style="font-family:'Urbanist'"><span style="font-weight:bold;">${data.topPicks[0].artists[0].name}</span> <br/> ${data.topPicks[0].name}</span>`},
{"name": "top_pick_text_02", "content": `<span style="font-family:'Urbanist'"><span style="font-weight:bold;">${data.topPicks[1].artists[0].name}</span> <br/> ${data.topPicks[1].name}</span>`},
{"name": "top_pick_text_03", "content": `<span style="font-family:'Urbanist'"><span style="font-weight:bold;">${data.topPicks[2].artists[0].name}</span> <br/> ${data.topPicks[2].name}</span>`}
],
"emailAddress": emailAddress,
}
};
request.post(options, function (error, response, body) {
console.log(response, body);
if (error) {
console.log("Error posting to radia-server /email/send.", error);
resolve({success: false, error});
}
if (!error && response.statusCode === 200) {
resolve({success: true, transaction: body});
}
});
});
};
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
exports.handler = async (event, context) => {
try {
const spotifySecretName = "prod/Radia/Spotify";
const spotifySecrets = await getSecrets(spotifySecretName);
const radiaSecretName = "prod/Radia/API";
const radiaSecrets = await getSecrets(radiaSecretName);
const partitionKey = event.pk;
console.log("user ID", partitionKey);
const refreshToken = event.refresh_token;
const accessToken = await refreshSpotifyAccessToken(spotifySecrets, refreshToken);
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).valueOf();
// Get streamedMilliseconds user Collectibles within the last 7 days
// Filter by collectibles that have been updated
const streamedMillisecondsCollectibles = await getUserStreamedMillisecondsCollectiblesWithinLastSevenDays(partitionKey, sevenDaysAgo);
const filteredCollectibles = streamedMillisecondsCollectibles.Items.filter(collectible => collectible.updated > collectible.created);
const numberOfArtists = filteredCollectibles.length;
// Get Albums updated within the last 7 days
const albums = await getUserAlbumsWithinLastSevenDays(partitionKey, sevenDaysAgo);
const numberOfAlbums = albums.Count;
// Get Tracks updated within the last 7 days
const tracks = await getUserTracksWithinLastSevenDays(partitionKey, sevenDaysAgo);
const numberOfTracks = tracks.Count;
// Get earned collectibles within the last 7 days
const allCollectibles = await getUserCollectiblesWithinLastSevenDays(partitionKey, sevenDaysAgo);
const completedCollectibles = allCollectibles.Items.filter(collectible => collectible.status === 'readyToMint' || "transaction" in collectible);
const numberOfCompletedCollectibles = completedCollectibles.length;
// Get top three milliseconds streamed closest to earning collectibles.
const closeToEarning = [];
const inProgressFilteredCollectibles = allCollectibles.Items.filter(collectible => !("status" in collectible));
const sortedInProgressCollectibles = inProgressFilteredCollectibles.sort((a, b) => calculateProgress(b) - calculateProgress(a)).slice(0, 3);
// Loop through collectibles, calculate time left until earning
for (const collectible of sortedInProgressCollectibles) {
const timeLeft = calculateTimeLeft(collectible);
closeToEarning.push(timeLeft);
// Note: if we ever want to use top tracks related to the users taste as a top pick, then use these.
// const relatedArtists = await getRelatedSpotifyArtists(accessToken, collectible.artist.id);
// const randomIndexArtists = Math.floor(Math.random()*relatedArtists.artists.length);
// const artist = relatedArtists.artists[randomIndexArtists];
// const topTracks = await getSpotifyArtistTopTracks(accessToken, artist.id);
// const randomIndexTracks = Math.floor(Math.random()*topTracks.tracks.length);
// topPicks.push(topTracks.tracks[randomIndexTracks]);
// await wait(500);
}
// Get 3 random new music picks
const topPicks = [];
const newMusic = await getSpotifyNewMusic(accessToken);
for (const _ of [1,2,3]) {
const randomIndex = Math.floor(Math.random()*newMusic.albums.items.length);
const pick = newMusic.albums.items[randomIndex];
topPicks.push(pick);
}
console.log("sesing email with thisdata", numberOfArtists, numberOfAlbums, numberOfTracks, numberOfCompletedCollectibles, closeToEarning, topPicks);
const data = {
numberOfArtists,
numberOfAlbums,
numberOfTracks,
numberOfCompletedCollectibles,
closeToEarning,
topPicks
};
//TODO: turning off for spotify review
const user = await getUser(partitionKey);
try {
console.log("Sending email to:", user.email, "Email opt:", user.emailOptIn);
if (user.emailOptIn === undefined || user.emailOptIn === true)
await sendEmail(radiaSecrets, user.email, data);
} catch (e) {
console.log(e);
}
return {success: true};
} catch (err) {
return {error: err};
}
};