UNPKG

gmail-mbox-stats

Version:
187 lines (174 loc) 6.97 kB
"use strict"; /* gmail-mbox-stats v1.2.3 Created by leodevbro (Levan Katsadze) * leodevbro@gmail.com * linkedin.com/in/leodevbro * github.com/leodevbro * facebook.com/leodevbro If you feel like donating * buymeacoffee.com/leodevbro * ko-fi.com/leodevbro */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.logExecutionStartMessage = void 0; // === // === // === /* https://support.google.com/mail/answer/7190 In Gmail, find mail (message) by id just type like this: rfc822msgid:CAA_p-vyCSYhMN=mGZBd7GibPgaBxNX7ayh4sheQgPYqCS97P3A@mail.gmail.com the id is after the ":" (colon). */ // // // // TODO: listFile parter to be 500K lines. // TODO const node_mbox_1 = __importDefault(require("node-mbox")); const sync_1 = require("csv-stringify/sync"); const node_fs_1 = require("node:fs"); // import { parse as parseCsvInto2dArr } from "csv-parse"; const mainUtils_1 = require("./utils/mainUtils"); const stepUtils_1 = require("./utils/stepUtils"); const constants_1 = require("./constants"); const groundFolderMaker_1 = require("./utils/groundFolderMaker"); const gloAccu_1 = require("./gloAccu"); const mailParsingUtils_1 = require("./utils/mailParsingUtils"); const basicStarter_1 = require("./basicStarter"); // import { handleOneLineOfMailboxIndex } from "./utils/mailboxIndexMaker"; // console.log("test AA BB 01"); const startDateTimeStr = `Start datetime: ${constants_1.slimStartDateTime.v}`; console.time("Full Execution Time"); const logExecutionStartMessage = () => { console.log("\n"); console.log(constants_1.str.authoringText + "\n\n"); console.log("Started MBOX file analyzation"); // console.log("Relax a bit, here you can see the progress:\n"); console.log(startDateTimeStr); }; exports.logExecutionStartMessage = logExecutionStartMessage; const logExecutionEndMessage = () => { console.log("\n"); console.log(startDateTimeStr); console.log(`->End datetime: ${(0, mainUtils_1.getSlimDateTime)(new Date())}\n`); console.timeEnd("Full Execution Time"); console.log("\n"); console.log(constants_1.str.authoringText + "\n"); console.log(constants_1.str.donationText); console.log("\n"); console.log("\n"); }; (0, stepUtils_1.prepareOutputFolderStructure)(basicStarter_1.mboxFilePath); const checkFullCount = () => __awaiter(void 0, void 0, void 0, function* () { gloAccu_1.step.countOfFullCountCheckForTimer += 1; yield (0, mainUtils_1.waitSomeSeconds)(10); console.log("So far processed mails:", gloAccu_1.step.succeededV); if (gloAccu_1.step.succeededV === gloAccu_1.step.v) { console.log("Success! Full count:", gloAccu_1.step.succeededV); return true; } else { if (gloAccu_1.step.countOfFullCountCheckForTimer > 720) { // 720 * 10 seconds = 2 hours console.log(`Took too long --- could not process all ${gloAccu_1.step.v} mails. Processed mails: ${gloAccu_1.step.succeededV}`); return false; } return yield checkFullCount(); } }); const analyzeMbox = () => { const mailboxReadStream = (0, node_fs_1.createReadStream)(basicStarter_1.mboxFilePath); const mbox = node_mbox_1.default.MboxStream(mailboxReadStream, { /* options */ }); // // mbox.on("data", (msg) => __awaiter(void 0, void 0, void 0, function* () { gloAccu_1.step.v += 1; // This is count of all mails, not only succesfuly proccessed mails. try { (0, mailParsingUtils_1.processOneMail)(msg); } catch (err) { console.log(err); } })); mbox.on("error", function (err) { console.log("got an error", err); }); mbox.on("finish", function () { return __awaiter(this, void 0, void 0, function* () { console.log(`Finished accessing all the mails (${gloAccu_1.step.v}), but not yet finished proccessing them. Please wait.`); yield checkFullCount(); console.log("Please wait several seconds more to generate stats files."); yield (0, mainUtils_1.waitSomeSeconds)(3); (0, stepUtils_1.writeStatsIntoFiles)(); // this does not include generalStats, // because generalStats needs writeStatsIntoFiles function to execute first to calculate general stats. // So, The generalStats will be handled down below. //======= const generalStats2dArrNotation = (0, stepUtils_1.generateGeneralStats)().generalStats2dArr; // ---- const generalStatsPath = groundFolderMaker_1.groundFolder.innerFolders.mboxStats.innerFiles.generalStats.pathAbsOrRel; const finalDataStringOfGeneralStats = (0, sync_1.stringify)(generalStats2dArrNotation, { header: false, columns: undefined, }); (0, node_fs_1.writeFileSync)(generalStatsPath, finalDataStringOfGeneralStats, { flag: "a+", }); logExecutionEndMessage(); }); }); }; analyzeMbox(); /* const myData = [ ['"""""""""5"`jj\t\t\t', 'k88k\t\t\t', `7\t\t\t`], ['"""5"`jj\t\t\t', 'k88\n\n\n\n\n\n\n\n\\45k\t\t\t', `52v"'\`\t\t\t`], ]; console.log('haaaa_start'); try { // const opts = {}; // const parser = new Parser({ // header: false, // }); // const csv = parser.parse(myData); const csv = stringify2dArrIntoCsv(myData, { header: false, columns: undefined, }); // console.log(csv); wr_stream.write(csv); const countOfNls = csv.split('').reduce((accu, curr) => { if (curr === '\n') { return accu + 1; } return accu; }, 0); console.log(countOfNls); setTimeout(() => { // Using the first line of the CSV data to discover the column names const rs = createReadStream(mainOutputFilename); const parser = parseCsvInto2dArr({ columns: false }, function (err, data) { console.log(data); }); rs.pipe(parser); }, 3); } catch (err) { console.error(err); } console.log('haaaa_end', process.argv, process.env.npm_config_aaabbbrt); // npm run go --aaabbbrt="sdfs" */