Skip to content

Instantly share code, notes, and snippets.

@lijunle
Last active March 14, 2023 08:38
Show Gist options
  • Save lijunle/89d2b482addd6c8fa4ee21931893f84f to your computer and use it in GitHub Desktop.
Save lijunle/89d2b482addd6c8fa4ee21931893f84f to your computer and use it in GitHub Desktop.
Download Photos from Bright Horizons
/* Download Bright Horizons Photos */
import fs from "node:fs";
import https from "node:https";
import path from "node:path";
import process from "node:process";
import url from "node:url";
import util from "node:util";
if (!process.argv[2]) {
console.error(`Usage: node ${process.argv[1]} <email-file>`);
process.exit(-1);
}
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const readFile = util.promisify(fs.readFile);
const snapshotUrl = "https://productionmbd.brighthorizons.com/m/snapshot/";
const emailFile = path.resolve(dirname, process.argv[2]);
console.info("Read content from file: " + emailFile);
const content = (await readFile(emailFile, "utf-8")).replace(/=\r\n/g, "");
let position = 0;
while (true) {
const startPosition = content.indexOf(snapshotUrl, position);
if (startPosition === -1) {
break;
}
const endPosition = content.indexOf("?d", startPosition);
position = endPosition;
const url = content.substring(startPosition, endPosition);
console.info("Start download " + url);
https.get(url + "?d=t&download=t", (response) => {
const disposition = response.headers["content-disposition"];
const filename = disposition
.substring("attachment; filename=".length)
.replace(/:/g, "_");
const fileStream = fs.createWriteStream(path.join(dirname, filename));
response.pipe(fileStream).on("finish", () => {
fileStream.close();
console.info("Complete: " + filename);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment