Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

app.js 4.3KB

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