gphotos-scraper
Version:
A tool to extract public url and metadata from shared album
45 lines • 2.12 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractAlbum = void 0;
const getAlbum_1 = require("./getAlbum");
const exceptions_1 = require("../exceptions");
const getPhotos_1 = require("./getPhotos");
const extractAlbum = (albumUrl) => __awaiter(void 0, void 0, void 0, function* () {
const gAlbum = yield (0, getAlbum_1.getAlbum)(albumUrl);
if (!gAlbum)
throw new exceptions_1.AlbumNotFoundException();
const { items, key, id, title, cover, createdAt, updatedAt } = gAlbum;
const itemsMap = items.reduce((map, item) => map.set(item.id, item.url), new Map());
const album = {
id,
title,
url: albumUrl,
photos: [],
createdAt,
updatedAt,
};
const chunkSize = 500;
for (let i = 0; i < items.length; i += chunkSize) {
const chunk = items.slice(i, i + chunkSize);
const ids = chunk.map((el) => el.id);
const photosInfo = yield (0, getPhotos_1.getPhotos)(key, ids);
album.photos.push(...photosInfo.map((el) => {
const url = itemsMap.get(el.id) || '';
return Object.assign(Object.assign({}, el), { url });
}));
}
album.photos.sort((a, b) => b.createdAt - a.createdAt);
album.cover = album.photos.find((photo) => photo.url === cover.url);
return album;
});
exports.extractAlbum = extractAlbum;
//# sourceMappingURL=extractAlbum.js.map