Last active
March 14, 2023 08:38
Download Photos from Bright Horizons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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