-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcar.js
58 lines (57 loc) · 1.75 KB
/
car.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
const url = "https://www.kbb.com/cars/";
const regex = /"masterListName":"(.*?)".*?"topVehicles":\[(.*?)\]/gs;
const carRegex = /"name":"(.*?)",/g;
if (typeof $httpClient !== "undefined") {
$httpClient.get(url, function (error, response, data) {
if (error) {
console.log("Error:", error);
$done();
} else {
const results = [];
let match;
let categoryCounter = 1;
while ((match = regex.exec(data))) {
const category = match[1];
const carMatches = match[2].match(carRegex);
const carNames = carMatches.map((m, index) => {
const carName = m.replace(carRegex, "$1");
return `${index + 1}. ${carName}`;
});
results.push(`🚘•${category}\n${carNames.join("\n")}`);
categoryCounter++;
}
const notificationText = results.join("\n");
$notification.post("", "", notificationText);
$done();
}
});
} else if (typeof $task !== "undefined") {
const request = {
url: url,
};
$task.fetch(request).then(
function (response) {
const data = response.body;
const results = [];
let match;
let categoryCounter = 1;
while ((match = regex.exec(data))) {
const category = match[1];
const carMatches = match[2].match(carRegex);
const carNames = carMatches.map((m, index) => {
const carName = m.replace(carRegex, "$1");
return `${index + 1}. ${carName}`;
});
results.push(`🚘•${category}\n${carNames.join("\n")}`);
categoryCounter++;
}
const notificationText = results.join("\n");
$notify("", "", notificationText);
$done();
},
function (reason) {
console.log("Error:", reason.error);
$done();
}
);
}