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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var config = require('config');
  2. const express = require('express');
  3. const cors = require('cors');
  4. const MongoClient = require('mongodb').MongoClient;
  5. const ObjectID = require('mongodb').ObjectID;
  6. // express application
  7. const app = express();
  8. app.use(express.json());
  9. app.use(cors());
  10. // database setup
  11. var mongoUrl = config.get("mongo");
  12. var database = config.get("database");
  13. var client = undefined;
  14. MongoClient.connect(mongoUrl, function (err, db) {
  15. if (err) throw err;
  16. console.log("Database created!");
  17. console.log(mongoUrl);
  18. client = db;
  19. var dbo = db.db(database);
  20. dbo.createCollection("scrapes", function (err, res) {
  21. if (err) {
  22. console.log("Collection already created!");
  23. return;
  24. }
  25. console.log("Collection created!");
  26. });
  27. });
  28. // const cron = require('node-cron');
  29. // var apartments = require('./apartments.js');
  30. // var houses = require('./houses.js');
  31. // app.set('json spaces', 2);
  32. // const axios = require('axios');
  33. // const cheerio = require('cheerio');
  34. // const url = 'https://www.apartments.com/two-west-chicago-il/jqn1nf6/';
  35. // app.get('/', (req, res) => {
  36. // axios(url).then(response => {
  37. // const html = response.data;
  38. // const $ = cheerio.load(html);
  39. // var data = apartments.apartment($);
  40. // res.json(data);
  41. // });
  42. // });
  43. // app.get('/houses/*', (req, res) => {
  44. // var url = req.params[0];
  45. // axios(url).then(response => {
  46. // const html = response.data;
  47. // const $ = cheerio.load(html);
  48. // var data = houses.house($);
  49. // res.json(data);
  50. // });
  51. // });
  52. // app.get('/filters/*', async (req, res) => {
  53. // var url = req.params[0];
  54. // const filterPage = await axios(url);
  55. // const html = filterPage.data;
  56. // const $ = cheerio.load(html);
  57. // const propertyLins = $('#placardContainer .property-link').map(function () {
  58. // return $(this).attr('href');
  59. // }).get();
  60. // var properties = [];
  61. // for (const link of propertyLins){
  62. // var response = await axios(link);
  63. // var property = apartments.apartment(cheerio.load(response.data));
  64. // properties.push(property);
  65. // }
  66. // res.json(properties);
  67. // });
  68. // app.get('/apartments/*', (req, res) => {
  69. // var url = req.params[0];
  70. // axios(url).then(response => {
  71. // const html = response.data;
  72. // const $ = cheerio.load(html);
  73. // var data = apartments.apartment($);
  74. // createListing(client, data);
  75. // res.json(data);
  76. // });
  77. // });
  78. app.get("/scrapes", async (req, res) => {
  79. try {
  80. const dbo = client.db(database);
  81. let collection = dbo.collection('scrapes');
  82. let data = await collection.find({}).toArray();
  83. return res.json(data);
  84. } catch (err) {
  85. console.log(err);
  86. return res.status(500).json();
  87. }
  88. });
  89. app.get("/scrapes/:id", async (req, res) => {
  90. const id = req.params.id;
  91. try {
  92. const dbo = client.db(database);
  93. let collection = dbo.collection('scrapes');
  94. var o_id = new ObjectID(id);
  95. let data = await collection.findOne({ _id: o_id});
  96. return res.json(data);
  97. } catch (err) {
  98. console.log(err);
  99. res.status(500).json();
  100. }
  101. });
  102. app.post("/scrapes/", async (req, res) => {
  103. const location = req.body.location;
  104. const price = req.body.price;
  105. const beds = req.body.beds;
  106. const type = req.body.type;
  107. const lifestyle = req.body.lifestyle;
  108. // query builder
  109. //todo: save data into the database
  110. try {
  111. const dbo = client.db(database);
  112. let collection = dbo.collection('scrapes');
  113. let res = await collection.insertOne({
  114. count: 21,
  115. estimate: Date.now(),
  116. sourceUrl: "https://www.apartments.com",
  117. location: location,
  118. filters: [
  119. { name: 'price', value: price },
  120. { name: 'beds', value: beds },
  121. { name: 'type', value: type },
  122. { name: 'lifestyle', value: lifestyle },
  123. ],
  124. status: "requested"
  125. });
  126. console.log(res);
  127. } catch (err) {
  128. console.log(err);
  129. return res.status(500).json();
  130. }
  131. return res.json();
  132. });
  133. app.patch("/scrapes/:id/execute", async (req, res) => {
  134. const id = req.params.id;
  135. try {
  136. const dbo = client.db(database);
  137. let collection = dbo.collection('scrapes');
  138. var o_id = new ObjectID(id);
  139. var newvalues = { $set: {status: "pending"} };
  140. let data = await collection.updateOne({ _id: o_id}, newvalues);
  141. return res.status(204).json();
  142. } catch (err) {
  143. console.log(err);
  144. res.status(500).json();
  145. }
  146. });
  147. const port = 3333;
  148. // var task = cron.schedule('* * * * *', function() {
  149. // console.log(`Runned job...`)
  150. // });
  151. // var options = {
  152. // host: 'http://localhost',
  153. // port:port,
  154. // path: '/apartments/https://www.apartments.com/essex-on-the-park-chicago-il/begd58b/',
  155. // method: 'GET'
  156. // };
  157. // task.start()
  158. // task.stop();
  159. app.listen(port, () => {
  160. console.log(`Example app listening at http://localhost:${port}`)
  161. });
  162. process.on('exit', function () {
  163. client.close();
  164. });