1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- var app = require('express')();
- var http = require('http').createServer(app);
- var io = require('socket.io')(http);
- var fs = require('fs');
- var path = require('path');
- let netflixHistoryItems = [];
- let historyFolderPath = path.join("..", "history");
- let latestHistoryFilePath = path.join(historyFolderPath, "latest_NetflixHistory.json");
- if (!fs.existsSync(historyFolderPath)) {
- fs.mkdirSync(historyFolderPath);
- }
- if (!fs.existsSync(latestHistoryFilePath)) {
- fs.writeFileSync(latestHistoryFilePath, "[]");
- }
- let latestHistoryFileContent = fs.readFileSync(latestHistoryFilePath).toString("utf8");
- netflixHistoryItems = JSON.parse(latestHistoryFileContent);
- console.log(`We now have ${netflixHistoryItems.length} items, the last being from ${(netflixHistoryItems.length > 0 ? new Date(netflixHistoryItems[0].date) : "Never")}`);
- if (netflixHistoryItems.length > 0) {
- console.log("Last activity item: ", netflixHistoryItems[0], `Date: ${new Date(netflixHistoryItems[0].date)}`);
- console.log("First activity item: ", netflixHistoryItems[netflixHistoryItems.length - 1], `Date: ${new Date(netflixHistoryItems[netflixHistoryItems.length - 1].date)}`);
- } else console.log("Since there are no items stored, not printing last/first activity item.");
- io.on('connection', function (socket) {
- socket.on("getLastActivityItem", cb => {
- if (netflixHistoryItems.length === 0) {
- cb({
- date: 0
- });
- } else {
- cb(netflixHistoryItems[0]);
- }
- });
- socket.on("importNewItems", (newItems, cb) => {
- const oldHistoryLength = netflixHistoryItems.length;
- const importingHistoryLength = newItems.length;
- console.log("Last item from import list: ", newItems[0], `Date: ${new Date(newItems[0].date)}`);
- console.log("First item from import list: ", newItems[importingHistoryLength - 1], `Date: ${new Date(newItems[importingHistoryLength - 1].date)}`);
- netflixHistoryItems = newItems.concat(netflixHistoryItems);
- console.log(`Import new items. Had ${oldHistoryLength} items, adding ${importingHistoryLength} items, now we have ${netflixHistoryItems.length} items.`);
- fs.writeFileSync(latestHistoryFilePath, JSON.stringify(netflixHistoryItems));
- const now = new Date();
- const year = now.getFullYear();
- const month = new String(now.getMonth() + 1).padStart(2, "0");
- const day = new String(now.getDate()).padStart(2, "0");
- const hour = new String(now.getHours()).padStart(2, "0");
- const minute = new String(now.getDate()).padStart(2, "0");
- const currentDateTime = `${year}-${month}-${day}_${hour}${minute}`;
- const backupFileName = `${currentDateTime}_NetflixHistory.json`;
- const backupFilePath = path.join(historyFolderPath, `${currentDateTime}_NetflixHistory.json`);
- fs.copyFileSync(latestHistoryFilePath, backupFilePath);
- })
- });
- http.listen(3000, function () {
- console.log('listening on *:3000');
- });
|