Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

app.js 5.5KB

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