Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Add fetchAlbumById method and CLI option for using albumId #463

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $ npm install -g upload-gphotos
## Usage

```
$ upload-gphotos file [...] [--no-output-json] [--quiet] [-r retry] [-u username] [-p password] [-a albumname]
$ upload-gphotos file [...] [--no-output-json] [--quiet] [-r retry] [-u username] [-p password] [-a albumname] [--aid albumid]
```

## Library
Expand Down
6 changes: 6 additions & 0 deletions src/GPhotos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class GPhotos {
}
}

async fetchAlbumoById({ id }: { id: string }): Promise<GPhotosAlbum> {
const album = new GPhotosAlbum({ id, type: 'album' }, { requestor: this.requestor });
await album.getInfo();
return album;
}

async fetchAlbumList({
cursor,
}: {
Expand Down
33 changes: 31 additions & 2 deletions src/GPhotosAlbum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import util from 'util';
import { Nullable } from 'option-t/cjs/Nullable';
import { isNotUndefined } from 'option-t/cjs/Undefinable';
import { Maybe, isNullOrUndefined } from 'option-t/cjs/Maybe';

import { Requestor } from './Requestor';
Expand All @@ -21,6 +22,7 @@ class GPhotosAlbum {

static parseInfo(data: any): GPhotosAlbumInfo {
const info = data.pop()['72930366'];

return {
id: data.shift(),
type: 'album',
Expand All @@ -40,8 +42,35 @@ class GPhotosAlbum {
this.requestor = requestor;
}

async getInfo(): Promise<Required<GPhotosAlbumInfo>> {
return this.info as Required<GPhotosAlbumInfo>;
async getInfo({ force }: { force: boolean } = { force: false }): Promise<Required<GPhotosAlbumInfo>> {
if (!force && isNotUndefined(this.info.title)) {
return this.info as Required<GPhotosAlbumInfo>;
}

const {
snAcKc: [, , , info],
} = await this.requestor.sendBatchExecute<{
snAcKc: [unknown, unknown, unknown, any];
}>({
queries: {
snAcKc: [this.id, null, null, null, 0],
},
});

const parsedInfo: Required<GPhotosAlbumInfo> = {
id: this.id,
type: 'album',
title: info[1],
period: {
from: new Date(info[2][0]),
to: new Date(info[2][1]),
},
itemsCount: info[21],
isShared: info[8] === true,
};

this.info = parsedInfo;
return parsedInfo;
}

async append(...photoList: GPhotosPhoto[]): Promise<void> {
Expand Down
16 changes: 8 additions & 8 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Configstore from 'configstore';
import { CookieJar } from 'tough-cookie';
import pRetry from 'p-retry';
import ora from 'ora';
import { Nullable, isNull } from 'option-t/cjs/Nullable';

import { GPhotos, GPhotosAlbum } from '../';
import { LIBRARY_NAME } from '../constants';
Expand Down Expand Up @@ -70,9 +69,14 @@ if (yargs.argv.version) {
conf.set('jar', encoded.encoded);
conf.set('iv', encoded.iv.toString('base64'));

const albumMap: Map<string, Nullable<GPhotosAlbum>> = new Map();
const albumList: Array<GPhotosAlbum> = [];
for (const albumName of yargs.argv.album) {
albumMap.set(albumName, await gphotos.searchAlbum({ title: albumName }));
albumList.push(
(await gphotos.searchAlbum({ title: albumName })) || (await gphotos.createAlbum({ title: albumName })),
);
}
for (const albumId of yargs.argv['album-id']) {
albumList.push(await gphotos.fetchAlbumoById({ id: albumId }));
}

for (const filepath of files) {
Expand Down Expand Up @@ -104,11 +108,7 @@ if (yargs.argv.version) {
},
);

for (let [albumName, album] of albumMap.entries()) {
if (isNull(album)) {
album = await gphotos.createAlbum({ title: albumName });
albumMap.set(albumName, album);
}
for (const album of albumList) {
await album.append(photo);
}

Expand Down
11 changes: 9 additions & 2 deletions src/cli/yargs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const yargs = _yargs
.usage(
`
Upload-GPhotos ${packageInfo.version}
Usage: upload-gphotos file [...] [--no-output-json] [--quiet] [-r retry] [-u username] [-p password] [-a albumname]
Usage: upload-gphotos file [...] [--no-output-json] [--quiet] [-r retry] [-u username] [-p password] [-a albumname] [--aid albumid]
`.trim(),
)
.help('help')
.strict()
.options({
retry: {
alias: 'r',
Expand All @@ -34,7 +35,13 @@ Usage: upload-gphotos file [...] [--no-output-json] [--quiet] [-r retry] [-u use
alias: 'a',
type: 'array',
default: [] as string[],
desc: 'An albums to put uploaded files.',
desc: 'The names of albums to put uploaded files.',
},
'album-id': {
alias: 'aid',
type: 'array',
default: [] as string[],
desc: 'The ids of albums to put uploaded files.',
},
quiet: {
type: 'boolean',
Expand Down