index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var express = require('express');
  2. var router = express.Router();
  3. var db = require('../db');
  4. var Crawler = require('./crawler.js');
  5. // /* GET home page. */
  6. // router.get('/', function(req, res, next) {
  7. // res.render('index', { title: 'Express' });
  8. // });
  9. function parseInput(input) {
  10. return {
  11. data: {},
  12. price: 0.0,
  13. position: 0,
  14. enemies: input.enemies.split(','),
  15. category: input.category
  16. }
  17. }
  18. router.put('/:code', function(req, res) {
  19. let items = parseInput(req.body);
  20. console.log('Body:');
  21. console.log(items);
  22. db.put(req.params.code, items)
  23. .then(() => {
  24. return db.get(req.params.code);
  25. })
  26. .then(() => res.sendStatus(200));
  27. });
  28. router.delete('/:code', function(req, res) {
  29. db.del(req.params.code)
  30. .then(() => res.sendStatus(200));
  31. });
  32. router.get('/', function(req, res) {
  33. db.getAll().then((v) => {
  34. res.send(v);
  35. });
  36. });
  37. router.get('/fix', (req, res) => {
  38. let proms = [];
  39. db.getAll().then((items) => {
  40. for(var i in items) {
  41. items[i].category = "926/symvata-anakataskevasmena-toner-drums-ektypwtwn";
  42. proms.push(db.put(i, items[i]));
  43. }
  44. }).then(() => {
  45. Promise.all(proms).then(() => res.sendStatus(200)).catch((e)=> res.send(e));
  46. });
  47. })
  48. function addMinutes(date, Minutes) {
  49. var result = new Date(date);
  50. result.setMinutes(result.getMinutes() + Minutes);
  51. return result;
  52. }
  53. router.get('/refresh', function(req, res) {
  54. let now = new Date().toISOString();
  55. db.getAll().then((items) => {
  56. let proms = [];
  57. for (var i in items) {
  58. if( !items[i].lastDate || addMinutes(items[i].lastDate, 10).toISOString() < now) {
  59. console.log(items[i]);
  60. proms.push(
  61. Crawler.collect(i, items[i])
  62. );
  63. }
  64. }
  65. Promise.all(proms).then(() => {
  66. res.sendStatus(200);
  67. }).catch((d) => {
  68. console.log(d);
  69. res.sendStatus(201);
  70. });
  71. });
  72. });
  73. module.exports = router;