-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
60 lines (55 loc) · 1.72 KB
/
import.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
const request = require('request-promise');
const cheerio = require('cheerio');
const mongoose = require('mongoose');
const CraigslistApt = require('./model/CraigslistApt');
const mongoDbUrl = require('./config/keys');
const url = require('./config/url');
async function scrapeApts() {
const result = await request.get(url);
const $ = await cheerio.load(result);
const apartments = $('.result-info')
.map((i, element) => {
const titleElement = $(element).find('.result-title');
const title = titleElement.text();
const url = titleElement.attr('href');
const timestamp = new Date(
$(element)
.find('.result-date')
.attr('datetime')
);
const price = $(element)
.find('.result-price')
.text();
const neighborhood = $(element)
.find('.result-hood')
.text();
const squareFeet = $(element).find('.housing');
const size = squareFeet.text().trim();
return { title, timestamp, url, price, neighborhood, size };
})
.get();
return apartments;
}
async function craigslistAptToMongoDb(aptArray) {
const promises = aptArray.map(async apartment => {
const aptFromDb = await CraigslistApt.findOne({ url: apartment.url });
if (!aptFromDb) {
const newApt = new CraigslistApt(apartment);
return newApt.save();
}
});
await Promise.all(promises);
}
async function main() {
try {
await mongoose.connect(mongoDbUrl, { useNewUrlParser: true });
console.log('Connected to mongodb');
const aptArray = await scrapeApts();
await craigslistAptToMongoDb(aptArray);
mongoose.disconnect();
console.log('disconnected from mongodb!');
} catch (err) {
console.error(err);
}
}
main();