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.

app.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const express = require('express');
  2. var cors = require('cors');
  3. const app = express();
  4. // app.use(bodyParser.urlencoded({ extended: false }));
  5. app.use(express.json());
  6. app.use(cors());
  7. // app.use(bodyParser.raw());
  8. // const cron = require('node-cron');
  9. // const {MongoClient} = require('mongodb');
  10. // 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";
  11. // const client = new MongoClient(uri);
  12. // client.connect();
  13. // createListing = async function (client, data){
  14. // const result = await client.db("scraper").collection("scraping").insertOne(data);
  15. // console.log(`New scraping created with the following id: ${result.insertedId}`);
  16. // }
  17. // var apartments = require('./apartments.js');
  18. // var houses = require('./houses.js');
  19. // app.set('json spaces', 2);
  20. // const axios = require('axios');
  21. // const cheerio = require('cheerio');
  22. // const url = 'https://www.apartments.com/two-west-chicago-il/jqn1nf6/';
  23. // app.get('/', (req, res) => {
  24. // axios(url).then(response => {
  25. // const html = response.data;
  26. // const $ = cheerio.load(html);
  27. // var data = apartments.apartment($);
  28. // res.json(data);
  29. // });
  30. // });
  31. // app.get('/houses/*', (req, res) => {
  32. // var url = req.params[0];
  33. // axios(url).then(response => {
  34. // const html = response.data;
  35. // const $ = cheerio.load(html);
  36. // var data = houses.house($);
  37. // res.json(data);
  38. // });
  39. // });
  40. // app.get('/filters/*', async (req, res) => {
  41. // var url = req.params[0];
  42. // const filterPage = await axios(url);
  43. // const html = filterPage.data;
  44. // const $ = cheerio.load(html);
  45. // const propertyLins = $('#placardContainer .property-link').map(function () {
  46. // return $(this).attr('href');
  47. // }).get();
  48. // var properties = [];
  49. // for (const link of propertyLins){
  50. // var response = await axios(link);
  51. // var property = apartments.apartment(cheerio.load(response.data));
  52. // properties.push(property);
  53. // }
  54. // res.json(properties);
  55. // });
  56. // app.get('/apartments/*', (req, res) => {
  57. // var url = req.params[0];
  58. // axios(url).then(response => {
  59. // const html = response.data;
  60. // const $ = cheerio.load(html);
  61. // var data = apartments.apartment($);
  62. // createListing(client, data);
  63. // res.json(data);
  64. // });
  65. // });
  66. app.get("/scrapes", (req, res) => {
  67. res.json(
  68. [
  69. {
  70. id: 1,
  71. location: "Chicago, IL",
  72. count: 21,
  73. estimate: Date.now(),
  74. sourceUrl: "https://www.apartments.com",
  75. filters: [
  76. { name: "price", value: "1000"},
  77. { name: "beds", value: "2"},
  78. ],
  79. status: "requested"
  80. },
  81. {
  82. id: 2,
  83. location: "New York, NY",
  84. count: 21,
  85. estimate: Date.now(),
  86. sourceUrl: "https://www.apartments.com",
  87. filters: [
  88. { name: "lifestyle", value: "2"},
  89. ],
  90. status: "pending"
  91. },{
  92. id: 3,
  93. location: "Los Angeles, CA",
  94. count: 21,
  95. estimate: Date.now(),
  96. sourceUrl: "https://www.apartments.com",
  97. filters: [
  98. { name: "type", value: "apartments"},
  99. ],
  100. status: "done"
  101. }
  102. ]
  103. )
  104. });
  105. app.get("/scrapes/:id", (req, res) => {
  106. const id = req.params.id;
  107. //todo: get data from mongo
  108. res.json(id);
  109. });
  110. app.post("/scrapes/", (req, res) => {
  111. const location = req.body.location;
  112. console.log(req.body)
  113. const price = req.body.price;
  114. const beds = req.body.beds;
  115. const type = req.body.type;
  116. const lifestyle = req.body.lifestyle;
  117. // query builder
  118. //todo: save data into the database
  119. res.json({
  120. id: 1,
  121. location: location,
  122. filters:[
  123. { name: 'price', value: price },
  124. { name: 'beds', value: beds },
  125. { name: 'type', value: type },
  126. { name: 'lifestyle', value: lifestyle },
  127. ]
  128. });
  129. });
  130. app.patch("/scrapes/:id/execute", (req, res) => {
  131. const id = req.params.id;
  132. //todo: get scrape data from db
  133. //todo: mark scrape for execution in the job queue
  134. res.status(200).json(id);
  135. });
  136. const port = 3333;
  137. // var task = cron.schedule('* * * * *', function() {
  138. // console.log(`Runned job...`)
  139. // });
  140. // var options = {
  141. // host: 'http://localhost',
  142. // port:port,
  143. // path: '/apartments/https://www.apartments.com/essex-on-the-park-chicago-il/begd58b/',
  144. // method: 'GET'
  145. // };
  146. // task.start()
  147. // task.stop();
  148. app.listen(port, () => {
  149. console.log(`Example app listening at http://localhost:${port}`)
  150. });