-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing.js
65 lines (55 loc) · 1.94 KB
/
listing.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
const axios = require('axios');
const cheerio = require('cheerio');
const stringHelper = require('string-helper');
module.exports.handler = async (event, context, callback) => {
const listingId = event.pathParameters.id;
const kslUrl = `https://classifieds.ksl.com/listing/${listingId}`;
// Load Classified page and pass a custom User-Agent as the KSL server throws a "403 Unauthorized" otherwise.
const res = await axios.get(kslUrl, { headers: { 'User-Agent': stringHelper.makeid(10) } });
/*
if (res.statusCode !== 200) {
let response = {
statusCode: res.statusCode,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({ "listing": {}, "kslUrl": kslUrl })
};
return response;
}*/
const $ = cheerio.load(res.data, { xmlMode: false, ignoreWhitespace: true, });
const data = $('script:not([src])').get()[7].children[0].data
console.log("data:", data);
let response = {
statusCode: 404,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({ "listing": {}, "kslUrl": kslUrl })
};
if (data.indexOf("window.detailPage = window.detailPage") !== -1) {
let listingJson = data.match(/detailPage.listingData = ({[^;]*})/g)[0];
// remove invalid json stuff
listingJson = listingJson.replace('detailPage.listingData = ', '')
.replace('dealershipId: 0,', 'dealershipId: 0')
.replace("'normal'", '"normal"')
.replace("''", '""');
console.log("unformatted listing:", listingJson);
doctoredJson = stringHelper.fixJson(listingJson);
console.log("doctored json:", doctoredJson);
if (stringHelper.isValidJson(doctoredJson)) {
json = JSON.parse(doctoredJson);
}
response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({ "listing": json, "kslUrl": kslUrl }),
};
}
return response;
};