feat: add new photo in album flickr trigger

This commit is contained in:
Ali BARIN
2022-04-15 00:47:47 +02:00
committed by Ömer Faruk Aydın
parent 651cceec14
commit 1e00c7d14e
10 changed files with 204 additions and 5 deletions

View File

@@ -0,0 +1,10 @@
import { IJSONObject } from '@automatisch/types';
import ListAlbums from './data/list-albums';
export default class Data {
listAlbums: ListAlbums;
constructor(connectionData: IJSONObject) {
this.listAlbums = new ListAlbums(connectionData);
}
}

View File

@@ -0,0 +1,39 @@
import FlickrApi from 'flickr-sdk';
import type { IJSONObject } from '@automatisch/types';
export default class ListAlbums {
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() {
const { photosets } = (await this.client.photosets.getList()).body;
const allPhotosets = [...photosets.photoset];
for (let page = photosets.page + 1; page <= photosets.pages; page++) {
const { photosets } = (await this.client.photosets.getList({ page, })).body;
allPhotosets.push(...photosets.photoset);
}
return allPhotosets.map((photoset) => ({
value: photoset.id,
name: photoset.title._content,
}));
}
}

View File

@@ -6,10 +6,12 @@ import {
} from '@automatisch/types';
import Authentication from './authentication';
import Triggers from './triggers';
import Data from './data';
export default class Flickr implements IService {
authenticationClient: IAuthentication;
triggers: Triggers;
data: Data;
constructor(
appData: IApp,
@@ -17,6 +19,7 @@ export default class Flickr implements IService {
parameters: IJSONObject
) {
this.authenticationClient = new Authentication(appData, connectionData);
this.data = new Data(connectionData);
this.triggers = new Triggers(connectionData, parameters);
}
}

View File

@@ -218,9 +218,9 @@
"triggers": [
{
"name": "New favorite photo",
"key": "favoritePhoto",
"key": "newFavoritePhoto",
"interval": "15m",
"description": "Will be triggered when you favorite a photo.",
"description": "Triggers when you favorite a photo.",
"substeps": [
{
"key": "chooseAccount",
@@ -231,6 +231,45 @@
"name": "Test trigger"
}
]
},
{
"name": "New photo in album",
"key": "newPhotoInAlbum",
"interval": "15m",
"description": "Triggers when you add a new photo in an album.",
"substeps": [
{
"key": "chooseAccount",
"name": "Choose account"
},
{
"key": "chooseTrigger",
"name": "Set up a trigger",
"arguments": [
{
"label": "Album",
"key": "album",
"type": "dropdown",
"required": true,
"variables": false,
"source": {
"type": "query",
"name": "getData",
"arguments": [
{
"name": "key",
"value": "listAlbums"
}
]
}
}
]
},
{
"key": "testStep",
"name": "Test trigger"
}
]
}
]
}

View File

@@ -1,10 +1,13 @@
import { IJSONObject } from '@automatisch/types';
import FavoritePhoto from './triggers/favorite-photo';
import NewPhotoInAlbum from './triggers/new-photo-in-album';
export default class Triggers {
favoritePhoto: FavoritePhoto;
newPhotoInAlbum: NewPhotoInAlbum;
constructor(connectionData: IJSONObject, parameters: IJSONObject) {
this.favoritePhoto = new FavoritePhoto(connectionData);
this.newPhotoInAlbum = new NewPhotoInAlbum(connectionData, parameters);
}
}

View File

@@ -24,7 +24,7 @@ export default class FavoritePhoto {
}
async run(startTime: Date) {
const { photos } = (await this.client.favorites.getList({ per_page: 1, })).body;
const { photos } = (await this.client.favorites.getList()).body;
const favPhotos = [...photos.photo];
const newFavPhotos = [];
@@ -45,7 +45,7 @@ export default class FavoritePhoto {
if (isLastItem && page < photos.pages) {
page = page + 1;
const { photos } = (await this.client.favorites.getList({ page, per_page: 1, })).body;
const { photos } = (await this.client.favorites.getList({ page, })).body;
favPhotos.push(...photos.photo);
}
}

View File

@@ -0,0 +1,81 @@
import { DateTime } from 'luxon';
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;
}
}

View File

@@ -367,7 +367,21 @@ type TriggerSubstepArgument {
label: String
key: String
type: String
description: String
required: Boolean
variables: Boolean
source: TriggerSubstepArgumentSource
}
type TriggerSubstepArgumentSource {
type: String
name: String
arguments: [TriggerSubstepArgumentSourceArgument]
}
type TriggerSubstepArgumentSourceArgument {
name: String
value: String
}
type User {

View File

@@ -13,7 +13,7 @@ class Step extends Base {
connectionId?: string;
status = 'incomplete';
position!: number;
parameters: Record<string, unknown> = {};
parameters: Record<string, unknown>;
connection?: Connection;
flow: Flow;
executionSteps?: [ExecutionStep];

View File

@@ -61,6 +61,16 @@ export const GET_APPS = gql`
key
type
required
description
variables
source {
type
name
arguments {
name
value
}
}
}
}
}