| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const express = require('express');
- const app = express();
- const cron = require('node-cron');
- const {MongoClient} = require('mongodb');
-
- const uri = "mongodb+srv://Nikola:Nikola@cluster0.jvqzh.mongodb.net/admin?authSource=admin&replicaSet=atlas-7exvp3-shard-0&w=majority&readPreference=primary&appname=MongoDB%20Compass&retryWrites=true&ssl=true";
- const client = new MongoClient(uri);
- client.connect();
- createListing = async function (client, data){
- const result = await client.db("scraper").collection("scraping").insertOne(data);
- console.log(`New scraping created with the following id: ${result.insertedId}`);
- }
- var apartments = require('./apartments.js');
- var houses = require('./houses.js');
-
- app.set('json spaces', 2);
-
- const axios = require('axios');
- const cheerio = require('cheerio');
- const { map } = require('cheerio/lib/api/traversing');
-
- const url = 'https://www.apartments.com/two-west-chicago-il/jqn1nf6/';
- app.get('/', (req, res) => {
- axios(url).then(response => {
- const html = response.data;
- const $ = cheerio.load(html);
-
- var data = apartments.apartment($);
- res.json(data);
- });
- });
-
-
- app.get('/houses/*', (req, res) => {
- var url = req.params[0];
- axios(url).then(response => {
- const html = response.data;
- const $ = cheerio.load(html);
-
- var data = houses.house($);
- res.json(data);
- });
- });
-
-
- app.get('/filters/*', async (req, res) => {
- var url = req.params[0];
- const filterPage = await axios(url);
- const html = filterPage.data;
- const $ = cheerio.load(html);
-
- const propertyLins = $('#placardContainer .property-link').map(function () {
- return $(this).attr('href');
- }).get();
-
- var properties = [];
- for (const link of propertyLins){
- var response = await axios(link);
-
- var property = apartments.apartment(cheerio.load(response.data));
- properties.push(property);
- }
-
- res.json(properties);
- });
- app.get('/apartments/*', (req, res) => {
- var url = req.params[0];
- axios(url).then(response => {
- const html = response.data;
- const $ = cheerio.load(html);
-
- var data = apartments.apartment($);
-
- createListing(client, data);
- res.json(data);
-
-
- });
- });
- const port = 3333;
- var task = cron.schedule('* * * * *', function() {
- console.log(`Runned job...`)
- });
- var options = {
- host: 'http://localhost',
- port:port,
- path: '/apartments/https://www.apartments.com/essex-on-the-park-chicago-il/begd58b/',
- method: 'GET'
- };
-
- http.request(options, function(res) {
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- });
- }).end();
-
-
-
-
- task.start()
-
- task.stop();
-
- app.listen(port, () => {
- console.log(`Example app listening at http://localhost:${port}`)
- });
-
|