-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
112 lines (100 loc) · 2.58 KB
/
helpers.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
const cheerio = require("cheerio");
const AWS = require("aws-sdk");
const Twilio = require("twilio");
const { differenceBy } = require("lodash");
const dynamo = new AWS.DynamoDB.DocumentClient();
async function sendTwilioTextMessage(numDogs) {
const {
TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN,
TWILIO_DEST_NUMS,
TWILIO_SRC_NUM: from,
} = process.env;
const toNums = TWILIO_DEST_NUMS.split(",");
const client = new Twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const messagePromises = toNums.map((to) =>
client.messages.create({
to,
from,
body: `there are ${numDogs} new dogs listed today, find them here: https://bit.ly/3jOgBVl`,
})
);
await Promise.all(messagePromises);
}
function getPetsFromDynamo() {
return dynamo
.scan({
TableName: process.env.DYNAMO_TABLE_NAME,
})
.promise();
}
function deletePetsFromDynamo(idToDelete) {
if (idToDelete) {
return dynamo
.delete({
TableName: process.env.DYNAMO_TABLE_NAME,
Key: {
dogId: idToDelete,
},
})
.promise();
} else return;
}
function savePetsToDynamo(dogs) {
return dynamo
.put({
TableName: process.env.DYNAMO_TABLE_NAME,
Item: {
dogId: new Date().toString(),
dogs,
},
})
.promise();
}
function petsDiff(newPets, oldPets) {
return differenceBy(newPets, oldPets, "id");
}
function getIframeFromPetUrl(html) {
const $ = cheerio.load(html);
const iFrameUrl = $("iframe").attr("src");
return iFrameUrl;
}
function getPetsFromIframeHTML(html) {
const $ = cheerio.load(html);
const dogs = [];
$(".list-item").each((index, dogInfoBlock) => {
const id = $(".list-animal-id", dogInfoBlock).text();
if (!id) return;
const url = `${process.env.THIRD_PARTY_URL}${id}`;
const photo = $(".list-animal-photo", dogInfoBlock)
.attr("src")
.replace("_TN1", ""); // remove the thumbnail declaration
const name = $("a", dogInfoBlock).text();
const [sex, spayedNeutered] = $(".list-animal-sexSN", dogInfoBlock)
.text()
.split("/");
const breed = $(".list-animal-breed", dogInfoBlock).text();
const age = $(".list-animal-age", dogInfoBlock).text();
dogs[index] = {
dateAdded: new Date(new Date().setHours(0, 0, 0, 0)).toISOString(),
id,
url,
photo,
name,
sex,
spayedNeutered,
breed,
age,
};
});
return dogs;
}
module.exports = {
getPetsFromIframeHTML,
getIframeFromPetUrl,
getPetsFromDynamo,
savePetsToDynamo,
deletePetsFromDynamo,
petsDiff,
sendTwilioTextMessage,
};