You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const express = require('express');
  2. const app = express();
  3. const cron = require('node-cron');
  4. const {MongoClient} = require('mongodb');
  5. 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";
  6. const client = new MongoClient(uri);
  7. client.connect();
  8. createListing = async function (client, data){
  9. const result = await client.db("scraper").collection("scraping").insertOne(data);
  10. console.log(`New scraping created with the following id: ${result.insertedId}`);
  11. }
  12. var apartments = require('./apartments.js');
  13. var houses = require('./houses.js');
  14. app.set('json spaces', 2);
  15. const axios = require('axios');
  16. const cheerio = require('cheerio');
  17. const { map } = require('cheerio/lib/api/traversing');
  18. const url = 'https://www.apartments.com/two-west-chicago-il/jqn1nf6/';
  19. app.get('/', (req, res) => {
  20. axios(url).then(response => {
  21. const html = response.data;
  22. const $ = cheerio.load(html);
  23. var data = apartments.apartment($);
  24. res.json(data);
  25. });
  26. });
  27. app.get('/houses/*', (req, res) => {
  28. var url = req.params[0];
  29. axios(url).then(response => {
  30. const html = response.data;
  31. const $ = cheerio.load(html);
  32. var data = houses.house($);
  33. res.json(data);
  34. });
  35. });
  36. app.get('/filters/*', async (req, res) => {
  37. var url = req.params[0];
  38. const filterPage = await axios(url);
  39. const html = filterPage.data;
  40. const $ = cheerio.load(html);
  41. const propertyLins = $('#placardContainer .property-link').map(function () {
  42. return $(this).attr('href');
  43. }).get();
  44. var properties = [];
  45. for (const link of propertyLins){
  46. var response = await axios(link);
  47. var property = apartments.apartment(cheerio.load(response.data));
  48. properties.push(property);
  49. }
  50. res.json(properties);
  51. });
  52. app.get('/apartments/*', (req, res) => {
  53. var url = req.params[0];
  54. axios(url).then(response => {
  55. const html = response.data;
  56. const $ = cheerio.load(html);
  57. var data = apartments.apartment($);
  58. createListing(client, data);
  59. res.json(data);
  60. });
  61. });
  62. const port = 3333;
  63. var task = cron.schedule('* * * * *', function() {
  64. console.log(`Runned job...`)
  65. });
  66. var options = {
  67. host: 'http://localhost',
  68. port:port,
  69. path: '/apartments/https://www.apartments.com/essex-on-the-park-chicago-il/begd58b/',
  70. method: 'GET'
  71. };
  72. http.request(options, function(res) {
  73. console.log('STATUS: ' + res.statusCode);
  74. console.log('HEADERS: ' + JSON.stringify(res.headers));
  75. res.setEncoding('utf8');
  76. res.on('data', function (chunk) {
  77. console.log('BODY: ' + chunk);
  78. });
  79. }).end();
  80. task.start()
  81. task.stop();
  82. app.listen(port, () => {
  83. console.log(`Example app listening at http://localhost:${port}`)
  84. });