-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaw.js
126 lines (111 loc) · 3.17 KB
/
aw.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
const path = require('path');
const request = require('request');
const cheerio = require('cheerio');
const notifier = require('node-notifier');
const opn = require('opn');
const mailConfig = require('./config');
/*
config.js // <-- Create this file first
config = {
host: 'smtp.gmail.com',
port: 587,
secure: true,
auth: {
user: '[email protected]',
pass: 'pass'
}
}
*/
const subscribers = require('./subscribers');
/*
subscriber = [emailA, emailB, ...];
*/
const nodemailer = require('nodemailer');
// Parameters
const url = `https://www.amazon.co.jp/s/rh=i0aps,p_n_shipping_option-bin:2493950051,k:nintendo switch`;
const itemName = `ネオンブルー`;
const searchInterval = 8; // Minutes
let FUSE = true;
// Functions
let searchAmazon = function(url, itemName, callback) {
console.log("Searching Amazon for: ", itemName);
request(url, (err, res, body) => {
if(err) console.error(err);
let [itemFound, itemUrl] = parseResult(body, itemName);
notifyUser(itemFound, itemUrl);
console.log("Search complete.");
if(callback) {
callback(itemFound);
}
});
}
let parseResult = function(res, item) {
let itemFound = false;
let itemUrl = '';
let $ = cheerio.load(res);
$('.s-access-detail-page').each((i, ele) => {
if($(ele).attr('title').indexOf(item) != -1) {
itemFound = true;
itemUrl = $(ele).attr('href');
}
});
return [itemFound, itemUrl];
}
let notifyUser = function(itemFound, itemUrl) {
notifier.notify({
title: itemFound ? 'Switch 有货啦!(测试)' : "Switch 暂时没货。。。",
message: itemFound ? '点我买买买' : '再等等?',
sound: true, // Only Notification Center or Windows Toasters
wait: true // Wait with callback, until user action is taken against notification
}, function (err, response) {
// Response is response from notification
});
notifier.on('click', function (notifierObject, options) {
opn(itemUrl);
});
notifier.on('timeout', (notifierObject, options)=>{});
if(itemFound) {
let sendNotificationEmail = function(itemUrl) {
let mail = {
from: mailConfig.auth.user,
sender: "[email protected]", // lol
to: subscribers,
subject: itemName + " is available now.",
text: itemName + " is available now. Item url: " + itemUrl,
html: `<a href="${itemUrl}">Item link</a>`
}
transporter.sendMail(mail);
console.log("Email sent.");
FUSE = false;
};
let transporter = nodemailer.createTransport(mailConfig);
transporter.verify((err, res) => {
if(err) {
console.log(err);
FUSE = false;
} else {
console.log("Email server connected.");
sendNotificationEmail(itemUrl);
}
});
}
};
// Main
let searchLoopInterval = -1;
searchAmazon(url, itemName);
searchLoopInterval = setInterval(() => {
if(FUSE) {
searchAmazon(url, itemName, (itemFound) => {
if(!itemFound)
console.log("Next search time: " + new Date((new Date()).getTime() + searchInterval * 60 * 1000).toString());
else {
FUSE = false;
console.log("Item found. Stop searching.")
clearInterval(searchLoopInterval);
}
});
} else {
console.log("Item found. Stop searching.")
clearInterval(searchLoopInterval);
}
}, searchInterval * 60 * 1000);