Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Created December 18, 2021 05:11
Show Gist options
  • Save cat-in-136/d0c0f044c1159d9cc92b8f05649d2b15 to your computer and use it in GitHub Desktop.
Save cat-in-136/d0c0f044c1159d9cc92b8f05649d2b15 to your computer and use it in GitHub Desktop.
iCloud shared album downloader
// Usage: node icloud-sharedalbum-downloader.js 'https://www.icloud.com/sharedalbum/ja-jp/#xxxxxxxxxxxxxxx'
const puppeteer = require('puppeteer');
const pageUrl = process.argv[2];
console.debug(`Page: ${pageUrl}`);
const downloadPath = '/tmp/downloads'; // modify here!
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
async function downloadUrl(url) {
await page.evaluate((url) => {
let link = document.createElement('a');
link.setAttribute("download", "");
link.href = url;
link.innerHTML = "download";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}, url);
}
try {
await page.goto(pageUrl, {waitUntil: 'networkidle0'});
const client = await page.target().createCDPSession();
client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath
});
await page.click('.x-stream-photo-grid-item-view');
await sleep(2000);
const firstImg = await page.evaluate('document.querySelector(".image-view img[src]").src');
console.debug(`#1: ${firstImg}`);
await downloadUrl(firstImg);
let count = 2;
while (true) {
await page.keyboard.press("ArrowRight");
await sleep(3000);
const imgUrl = await page.evaluate('document.querySelector(".image-view img[src]").src');
console.debug(`#${count}: ${imgUrl}`);
count++;
if (imgUrl === firstImg) {
break;
} else {
await downloadUrl(imgUrl);
// continue
}
}
} catch (ex) {
console.error(`Error ${ex}`);
} finally {
await sleep(1000);
await browser.close();
}
})();
{
"name": "icloud-sharedalbum-downloader",
"version": "0.0.0",
"description": "",
"main": "icloud-sharedalbum-downloader.js",
"scripts": {},
"author": "@cat_in_136",
"license": "MIT",
"dependencies": {
"puppeteer": "^13.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment