index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var app = require('express')();
  2. var http = require('http').createServer(app);
  3. var io = require('socket.io')(http);
  4. var fs = require('fs');
  5. var path = require('path');
  6. let netflixHistoryItems = [];
  7. let historyFolderPath = path.join("..", "history");
  8. let latestHistoryFilePath = path.join(historyFolderPath, "latest_NetflixHistory.json");
  9. if (!fs.existsSync(historyFolderPath)) {
  10. fs.mkdirSync(historyFolderPath);
  11. }
  12. if (!fs.existsSync(latestHistoryFilePath)) {
  13. fs.writeFileSync(latestHistoryFilePath, "[]");
  14. }
  15. let latestHistoryFileContent = fs.readFileSync(latestHistoryFilePath).toString("utf8");
  16. netflixHistoryItems = JSON.parse(latestHistoryFileContent);
  17. console.log(`We now have ${netflixHistoryItems.length} items, the last being from ${(netflixHistoryItems.length > 0 ? new Date(netflixHistoryItems[0].date) : "Never")}`);
  18. if (netflixHistoryItems.length > 0) {
  19. console.log("Last activity item: ", netflixHistoryItems[0], `Date: ${new Date(netflixHistoryItems[0].date)}`);
  20. console.log("First activity item: ", netflixHistoryItems[netflixHistoryItems.length - 1], `Date: ${new Date(netflixHistoryItems[netflixHistoryItems.length - 1].date)}`);
  21. } else console.log("Since there are no items stored, not printing last/first activity item.");
  22. io.on('connection', function (socket) {
  23. socket.on("getLastActivityItem", cb => {
  24. if (netflixHistoryItems.length === 0) {
  25. cb({
  26. date: 0
  27. });
  28. } else {
  29. cb(netflixHistoryItems[0]);
  30. }
  31. });
  32. socket.on("importNewItems", (newItems, cb) => {
  33. const oldHistoryLength = netflixHistoryItems.length;
  34. const importingHistoryLength = newItems.length;
  35. console.log("Last item from import list: ", newItems[0], `Date: ${new Date(newItems[0].date)}`);
  36. console.log("First item from import list: ", newItems[importingHistoryLength - 1], `Date: ${new Date(newItems[importingHistoryLength - 1].date)}`);
  37. netflixHistoryItems = newItems.concat(netflixHistoryItems);
  38. console.log(`Import new items. Had ${oldHistoryLength} items, adding ${importingHistoryLength} items, now we have ${netflixHistoryItems.length} items.`);
  39. fs.writeFileSync(latestHistoryFilePath, JSON.stringify(netflixHistoryItems));
  40. const now = new Date();
  41. const year = now.getFullYear();
  42. const month = new String(now.getMonth() + 1).padStart(2, "0");
  43. const day = new String(now.getDate()).padStart(2, "0");
  44. const hour = new String(now.getHours()).padStart(2, "0");
  45. const minute = new String(now.getDate()).padStart(2, "0");
  46. const currentDateTime = `${year}-${month}-${day}_${hour}${minute}`;
  47. const backupFileName = `${currentDateTime}_NetflixHistory.json`;
  48. const backupFilePath = path.join(historyFolderPath, `${currentDateTime}_NetflixHistory.json`);
  49. fs.copyFileSync(latestHistoryFilePath, backupFilePath);
  50. })
  51. });
  52. http.listen(3000, function () {
  53. console.log('listening on *:3000');
  54. });