This code automates the process of syncing your Chrome browsing history and bookmarks with Google Calendar by downloading, extracting, and processing your Google Takeout archive.
npm run import -- "sync chrome data"
var importer = require('../Core');
var path = require('path');
var glob = require('glob');
var fs = require('fs');
var rimraf = require('rimraf');
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var DOWNLOADS_PATH = PROFILE_PATH + '/Downloads';
// TODO: run unzip as a service?
function unzip(file) {
console.log('unzipping ' + file);
return execCmd('unzip -o "' + file + '"', {cwd: DOWNLOADS_PATH})
}
var {
googleTakeout, syncChromeHistory, syncChromeBookmarks, execCmd
} = importer.import("order google takeout",
"sync chrome history",
"sync chrome bookmarks",
"spawn child process");
function syncChrome() {
if(fs.existsSync(DOWNLOADS_PATH + '/Takeout')) {
rimraf.sync(DOWNLOADS_PATH + '/Takeout');
}
var takeouts = glob.sync('takeout-*.zip', {cwd: DOWNLOADS_PATH}).map(f => path.join(DOWNLOADS_PATH, f));
var takeout = takeouts.sort((a, b) => fs.statSync(a).mtime.getTime() - fs.statSync(b).mtime.getTime()).pop();
console.log(takeout);
var shouldDownload = !fs.existsSync(takeout) || (new Date().getTime()) - fs.statSync(takeout).mtime.getTime() > 24 * 60 * 60 * 1000;
return (shouldDownload
? googleTakeout('chrome')
.then(() => {
const takeout = path.join(DOWNLOADS_PATH, glob.sync('takeout-*.zip', {cwd: DOWNLOADS_PATH}).pop());
return unzip(takeout);
})
: unzip(takeout))
.then(() => syncChromeHistory())
.then(() => syncChromeBookmarks())
}
module.exports = syncChrome;
const { importModule } = require('../Core');
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const { exec } = require('child_process');
const rimraf = require('rimraf');
const { spawn } = require('child_process');
const {
googleTakeout, syncChromeHistory, syncChromeBookmarks
} = importModule([
'googleTakeout',
'syncChromeHistory',
'syncChromeBookmarks',
]);
const PROFILE_PATH = getEnvironmentVariable('HOME') || getEnvironmentVariable('HOMEPATH') || getEnvironmentVariable('USERPROFILE');
const DOWNLOADS_PATH = path.join(PROFILE_PATH, 'Downloads');
function getEnvironmentVariable(name) {
return process.env[name];
}
function unzip(file) {
console.log(`Unzipping ${file}...`);
return new Promise((resolve, reject) => {
exec(`unzip -o "${file}"`, { cwd: DOWNLOADS_PATH }, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
async function syncChrome() {
const unzipPath = getUnzipPath();
const shouldDownload = await shouldTakeoutBeDownloaded(unzipPath);
return (shouldDownload
? await downloadTakeout()
: unzip(unzipPath))
.then(() => await syncChromeHistory())
.then(() => await syncChromeBookmarks());
}
function getUnzipPath() {
const takeouts = glob.sync('takeout-*.zip', { cwd: DOWNLOADS_PATH });
return path.join(DOWNLOADS_PATH, takeouts.sort((a, b) => fs.statSync(a).mtime.getTime() - fs.statSync(b).mtime.getTime()).pop());
}
async function shouldTakeoutBeDownloaded(unzipPath) {
return!(fs.existsSync(unzipPath) && (new Date().getTime()) - fs.statSync(unzipPath).mtime.getTime() < 24 * 60 * 60 * 1000);
}
async function downloadTakeout() {
console.log('Downloading Google Takeout...');
return googleTakeout('chrome');
}
module.exports = syncChrome;
This code automates the synchronization of your Chrome browsing history and bookmarks with Google Calendar.
Here's a breakdown:
Setup:
Unzipping Takeout Files:
unzip
function unzips a Takeout archive file.Importing Functions:
Synchronization Logic:
syncChrome
function:
In essence, this code fetches your latest Google Takeout archive, extracts it, and then uses other functions to synchronize your browsing history and bookmarks with Google Calendar.