-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
122 lines (106 loc) · 3.77 KB
/
index.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
const fetch = require('node-fetch');
const nodemailer = require('nodemailer');
const MESSENTE_API_USERNAME = '<api-username-here>';
const MESSENTE_API_PASSWORD = '<api-password-here>';
// Provide your Gmail credentials
const GMAIL_CREDENTIALS = {
user: '<gmail-username-here>',
pass: '<gmail-password-here>',
};
const SMS_NOTIFICATION_RECIPIENTS = ['+372512345678', '+372598765432']; // <-- add your SMS recipients here
const EMAIL_NOTIFICATION_RECIPIENTS = ['[email protected]', '[email protected]']; // <-- add your e-mail recipients here
function sendEmail(text, recipients) {
if (!recipients || recipients.length === 0) {
return;
}
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
...GMAIL_CREDENTIALS,
},
});
transporter.sendMail(
{
from: GMAIL_CREDENTIALS.user,
to: recipients,
subject: 'Playstation 5',
text,
priority: 'high',
},
function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
},
);
}
async function sendSMS(text, recipients) {
const baseURL = 'https://api2.messente.com/send_sms/?';
for (const recipient of recipients) {
const params = {
username: MESSENTE_API_USERNAME,
password: MESSENTE_API_PASSWORD,
to: recipient,
text,
};
const queryString = Object.keys(params)
.map((key) => {
return `${key}=${encodeURIComponent(params[key])}`;
})
.join('&');
const url = `${baseURL}${queryString}`;
const response = await fetch(url);
const responseText = await response.text();
console.log(response.status, response.statusText, responseText);
}
}
async function main() {
const teliaURL = 'https://pood.telia.ee/api/products/mangukonsoolid/list/?clientType=private';
const teliaResponse = await fetch(teliaURL, {
method: 'POST',
headers: {
accept: 'application/json, text/plain, */*',
'content-type': 'application/json;charset=UTF-8',
},
body: JSON.stringify({
featureFilter: {},
sliderFilter: {},
commonFeatureFilter: [],
tags: [],
manufacturers: ['Sony'],
price: { min: 300, max: 1000 },
offset: 0,
limit: 5,
orderBy: 'priceMax', // Possible values: null, priceMax, priceMin, new
}),
timeout: 10_000,
});
if (teliaResponse.status === 200) {
const responseBody = await teliaResponse.json();
// NOTE: PS5 lineup has productCode starting with "CFI", like PS4 has "CUH"
const products = responseBody.products || [];
for (const product of products) {
const { productCode, name, prices, displayName } = product;
if (
/CFI/gi.test(productCode) ||
/PlayStation\s?5|PS\s?5/gi.test(name.et) ||
/PlayStation\s?5|PS\s?5/gi.test(displayName.et)
) {
const price = prices.currentPrice.amount;
const url = `https://pood.telia.ee${product.url}`;
const message = `${displayName.et}, hind: ${price} -> ${url}`;
await sendSMS(message, SMS_NOTIFICATION_RECIPIENTS);
sendEmail(message, EMAIL_NOTIFICATION_RECIPIENTS);
console.log(message);
}
}
} else {
console.log(teliaResponse.status, teliaResponse.statusText);
}
}
main()
.catch((err) => console.log(err))
.then(() => {})
.catch((err) => console.log(err));