feat(flickr): add authentication
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
import { DateTime } from 'luxon';
|
||||
import FlickrApi from 'flickr-sdk';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
export default class NewAlbum {
|
||||
client?: typeof FlickrApi;
|
||||
connectionData?: IJSONObject;
|
||||
primaryPhotoExtraFields = [
|
||||
'description',
|
||||
'license',
|
||||
'date_upload',
|
||||
'date_taken',
|
||||
'owner_name',
|
||||
'icon_server',
|
||||
'original_format',
|
||||
'last_update',
|
||||
'geo',
|
||||
'tags',
|
||||
'machine_tags',
|
||||
'o_dims',
|
||||
'views',
|
||||
'media',
|
||||
'path_alias',
|
||||
'url_sq',
|
||||
'url_t',
|
||||
'url_s',
|
||||
'url_q',
|
||||
'url_m',
|
||||
'url_n',
|
||||
'url_z',
|
||||
'url_c',
|
||||
'url_l',
|
||||
'url_o',
|
||||
].join(',');
|
||||
|
||||
constructor(connectionData: IJSONObject) {
|
||||
if (
|
||||
connectionData.consumerKey &&
|
||||
connectionData.consumerSecret &&
|
||||
connectionData.accessToken &&
|
||||
connectionData.accessSecret
|
||||
) {
|
||||
this.client = new FlickrApi(
|
||||
FlickrApi.OAuth.createPlugin(
|
||||
connectionData.consumerKey,
|
||||
connectionData.consumerSecret,
|
||||
connectionData.accessToken,
|
||||
connectionData.accessSecret
|
||||
)
|
||||
);
|
||||
|
||||
this.connectionData = connectionData;
|
||||
}
|
||||
}
|
||||
|
||||
async getAlbums(options: { perPage?: number, page?: number } = {}) {
|
||||
const { perPage, page } = options;
|
||||
const payload = {
|
||||
page,
|
||||
per_page: perPage,
|
||||
primary_photo_extras: this.primaryPhotoExtraFields,
|
||||
};
|
||||
const { photosets } = (await this.client.photosets.getList(payload)).body;
|
||||
|
||||
return photosets;
|
||||
}
|
||||
|
||||
async run(startTime: Date) {
|
||||
const albums = await this.getAlbums({ page: 1 });
|
||||
const allAlbums = [...albums.photoset];
|
||||
const newAlbums = [];
|
||||
|
||||
let page = 1;
|
||||
for (const album of allAlbums) {
|
||||
const createdAtInSeconds = DateTime.fromSeconds(parseInt(album.date_create, 10));
|
||||
const createdAt = createdAtInSeconds.toMillis();
|
||||
|
||||
if (createdAt <= startTime.getTime()) {
|
||||
break;
|
||||
}
|
||||
|
||||
newAlbums.push(album);
|
||||
|
||||
const currentIndex = allAlbums.indexOf(album);
|
||||
const totalAlbums = allAlbums.length;
|
||||
const isLastItem = currentIndex + 1 === totalAlbums;
|
||||
|
||||
if (isLastItem && page < albums.pages) {
|
||||
page = page + 1;
|
||||
const { photoset } = await this.getAlbums({ page, });
|
||||
allAlbums.push(...photoset.photoset);
|
||||
}
|
||||
}
|
||||
|
||||
return newAlbums;
|
||||
}
|
||||
|
||||
async testRun() {
|
||||
const { photoset } = await this.getAlbums({ perPage: 1 });
|
||||
|
||||
return photoset;
|
||||
}
|
||||
}
|
@@ -1,62 +0,0 @@
|
||||
import { DateTime } from 'luxon';
|
||||
import FlickrApi from 'flickr-sdk';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
export default class NewFavoritePhoto {
|
||||
client?: typeof FlickrApi;
|
||||
|
||||
constructor(connectionData: IJSONObject) {
|
||||
if (
|
||||
connectionData.consumerKey &&
|
||||
connectionData.consumerSecret &&
|
||||
connectionData.accessToken &&
|
||||
connectionData.accessSecret
|
||||
) {
|
||||
this.client = new FlickrApi(
|
||||
FlickrApi.OAuth.createPlugin(
|
||||
connectionData.consumerKey,
|
||||
connectionData.consumerSecret,
|
||||
connectionData.accessToken,
|
||||
connectionData.accessSecret
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async run(startTime: Date) {
|
||||
const { photos } = (await this.client.favorites.getList()).body;
|
||||
const favPhotos = [...photos.photo];
|
||||
const newFavPhotos = [];
|
||||
|
||||
let page = 1;
|
||||
for (const photo of favPhotos) {
|
||||
const markedFavoriteAt = DateTime.fromSeconds(parseInt(photo.date_faved, 10));
|
||||
const markedFavoriteAtInMillis = markedFavoriteAt.toMillis();
|
||||
|
||||
if (markedFavoriteAtInMillis <= startTime.getTime()) {
|
||||
break;
|
||||
}
|
||||
|
||||
newFavPhotos.push(photo);
|
||||
|
||||
const currentIndex = favPhotos.indexOf(photo);
|
||||
const totalFavPhotos = favPhotos.length;
|
||||
const isLastItem = currentIndex + 1 === totalFavPhotos;
|
||||
|
||||
if (isLastItem && page < photos.pages) {
|
||||
page = page + 1;
|
||||
const { photos } = (await this.client.favorites.getList({ page, })).body;
|
||||
favPhotos.push(...photos.photo);
|
||||
}
|
||||
}
|
||||
|
||||
return newFavPhotos;
|
||||
}
|
||||
|
||||
async testRun() {
|
||||
const { photos } = (await this.client.favorites.getList({ per_page: 1, })).body;
|
||||
|
||||
return photos.photo;
|
||||
}
|
||||
}
|
||||
|
@@ -1,80 +0,0 @@
|
||||
import FlickrApi from 'flickr-sdk';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
export default class NewPhotoInAlbum {
|
||||
client?: typeof FlickrApi;
|
||||
connectionData?: IJSONObject;
|
||||
albumId?: string;
|
||||
extraFields = [
|
||||
'license',
|
||||
'date_upload',
|
||||
'date_taken',
|
||||
'owner_name',
|
||||
'icon_server',
|
||||
'original_format',
|
||||
'last_update',
|
||||
'geo',
|
||||
'tags',
|
||||
'machine_tags',
|
||||
'o_dims',
|
||||
'views',
|
||||
'media',
|
||||
'path_alias',
|
||||
'url_sq',
|
||||
'url_t',
|
||||
'url_s',
|
||||
'url_m',
|
||||
'url_o'
|
||||
].join(',');
|
||||
|
||||
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
|
||||
if (
|
||||
connectionData.consumerKey &&
|
||||
connectionData.consumerSecret &&
|
||||
connectionData.accessToken &&
|
||||
connectionData.accessSecret
|
||||
) {
|
||||
this.client = new FlickrApi(
|
||||
FlickrApi.OAuth.createPlugin(
|
||||
connectionData.consumerKey,
|
||||
connectionData.consumerSecret,
|
||||
connectionData.accessToken,
|
||||
connectionData.accessSecret
|
||||
)
|
||||
);
|
||||
|
||||
this.connectionData = connectionData;
|
||||
}
|
||||
|
||||
if (parameters?.album) {
|
||||
this.albumId = parameters.album as string;
|
||||
}
|
||||
}
|
||||
|
||||
async getAlbumPhotos(options: { perPage?: number, page?: number } = {}) {
|
||||
const { perPage, page } = options;
|
||||
const payload = {
|
||||
page,
|
||||
per_page: perPage,
|
||||
photoset_id: this.albumId,
|
||||
user_id: this.connectionData.userId,
|
||||
extras: this.extraFields,
|
||||
};
|
||||
const { photoset } = (await this.client.photosets.getPhotos(payload)).body;
|
||||
|
||||
return photoset;
|
||||
}
|
||||
|
||||
async run() {
|
||||
// TODO: implement pagination on undated entries
|
||||
const { photo } = await this.getAlbumPhotos({ page: 1 });
|
||||
|
||||
return photo;
|
||||
}
|
||||
|
||||
async testRun() {
|
||||
const { photo } = await this.getAlbumPhotos({ perPage: 1 });
|
||||
|
||||
return photo;
|
||||
}
|
||||
}
|
@@ -1,88 +0,0 @@
|
||||
import { DateTime } from 'luxon';
|
||||
import FlickrApi from 'flickr-sdk';
|
||||
import { IJSONObject } from '@automatisch/types';
|
||||
|
||||
export default class NewPhoto {
|
||||
client?: typeof FlickrApi;
|
||||
connectionData?: IJSONObject;
|
||||
extraFields = [
|
||||
'description',
|
||||
'license',
|
||||
'date_upload',
|
||||
'date_taken',
|
||||
'owner_name',
|
||||
'icon_server',
|
||||
'original_format',
|
||||
'last_update',
|
||||
'geo',
|
||||
'tags',
|
||||
'machine_tags',
|
||||
'o_dims',
|
||||
'views',
|
||||
'media',
|
||||
'path_alias',
|
||||
'url_sq',
|
||||
'url_t',
|
||||
'url_s',
|
||||
'url_q',
|
||||
'url_m',
|
||||
'url_n',
|
||||
'url_z',
|
||||
'url_c',
|
||||
'url_l',
|
||||
'url_o',
|
||||
].join(',');
|
||||
|
||||
constructor(connectionData: IJSONObject) {
|
||||
if (
|
||||
connectionData.consumerKey &&
|
||||
connectionData.consumerSecret &&
|
||||
connectionData.accessToken &&
|
||||
connectionData.accessSecret
|
||||
) {
|
||||
this.client = new FlickrApi(
|
||||
FlickrApi.OAuth.createPlugin(
|
||||
connectionData.consumerKey,
|
||||
connectionData.consumerSecret,
|
||||
connectionData.accessToken,
|
||||
connectionData.accessSecret
|
||||
)
|
||||
);
|
||||
|
||||
this.connectionData = connectionData;
|
||||
}
|
||||
}
|
||||
|
||||
async getPhotos(options: { perPage?: number, page?: number, minUploadDate?: string } = {}) {
|
||||
const { perPage, page, minUploadDate } = options;
|
||||
const payload = {
|
||||
page,
|
||||
per_page: perPage,
|
||||
user_id: this.connectionData.userId,
|
||||
extras: this.extraFields,
|
||||
min_upload_date: minUploadDate,
|
||||
};
|
||||
const { photos } = (await this.client.photos.search(payload)).body;
|
||||
|
||||
return photos;
|
||||
}
|
||||
|
||||
async run(startTime: Date) {
|
||||
const minUploadDate = DateTime.fromJSDate(startTime).toSeconds().toString();
|
||||
const photos = await this.getPhotos({ page: 1, minUploadDate });
|
||||
const allPhotos = [...photos.photo];
|
||||
|
||||
for (let page = photos.page + 1; page <= photos.pages; page++) {
|
||||
const photos = (await this.getPhotos({ page, minUploadDate }));
|
||||
allPhotos.push(...photos.photo);
|
||||
}
|
||||
|
||||
return allPhotos;
|
||||
}
|
||||
|
||||
async testRun(startTime: Date) {
|
||||
const { photo } = await this.getPhotos({ perPage: 1 });
|
||||
|
||||
return photo;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user