feat: add new favorite photo trigger in flickr

This commit is contained in:
Ali BARIN
2022-04-13 01:22:13 +02:00
committed by Ömer Faruk Aydın
parent d4ad8645c9
commit 651cceec14
12 changed files with 131 additions and 16 deletions

View File

@@ -1,15 +1,22 @@
import Authentication from './authentication';
import {
IService,
IAuthentication,
IApp,
IJSONObject,
} from '@automatisch/types';
import Authentication from './authentication';
import Triggers from './triggers';
export default class Flickr implements IService {
authenticationClient: IAuthentication;
triggers: Triggers;
constructor(appData: IApp, connectionData: IJSONObject) {
constructor(
appData: IApp,
connectionData: IJSONObject,
parameters: IJSONObject
) {
this.authenticationClient = new Authentication(appData, connectionData);
this.triggers = new Triggers(connectionData, parameters);
}
}

View File

@@ -214,5 +214,23 @@
}
]
}
],
"triggers": [
{
"name": "New favorite photo",
"key": "favoritePhoto",
"interval": "15m",
"description": "Will be triggered when you favorite a photo.",
"substeps": [
{
"key": "chooseAccount",
"name": "Choose account"
},
{
"key": "testStep",
"name": "Test trigger"
}
]
}
]
}

View File

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

View File

@@ -0,0 +1,62 @@
import { DateTime } from 'luxon';
import FlickrApi from 'flickr-sdk';
import { IJSONObject } from '@automatisch/types';
export default class FavoritePhoto {
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({ per_page: 1, })).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, per_page: 1, })).body;
favPhotos.push(...photos.photo);
}
}
return newFavPhotos;
}
async testRun() {
const { photos } = (await this.client.favorites.getList({ per_page: 1, })).body;
return photos.photo;
}
}

View File

@@ -1,11 +1,10 @@
import fs from 'fs';
import { dirname, join } from 'path';
import { join } from 'path';
import { IApp } from '@automatisch/types';
import appInfoConverter from '../helpers/app-info-converter';
class App {
static backendPath = require.resolve('@automatisch/backend');
static folderPath = join(dirname(this.backendPath), 'apps');
static folderPath = join(__dirname, '../apps');
static list = fs.readdirSync(this.folderPath);
static findAll(name?: string): IApp[] {

View File

@@ -133,13 +133,12 @@ class Processor {
.orderBy('created_at', 'desc')
.first();
const lastExecutionStepCratedAt = lastExecutionStep?.dataOut
?.created_at as string;
const lastExecutionStepCreatedAt = lastExecutionStep?.createdAt as string;
const flow = (await step.$relatedQuery('flow')) as Flow;
const command = appInstance.triggers[key];
const startTime = new Date(lastExecutionStepCratedAt || flow.updatedAt);
const startTime = new Date(lastExecutionStepCreatedAt || flow.updatedAt);
let fetchedData;
if (this.testRun) {