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

@@ -3,7 +3,7 @@
"version": "0.1.0",
"description": "> TODO: description",
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --watch 'bin/**/*.ts' --exec 'ts-node' src/server.ts",
"dev": "nodemon --watch 'src/**/*.ts' --watch 'bin/**/*.ts' --exec 'ts-node' src/server.ts --ext ts,json",
"worker": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/worker.ts",
"build": "tsc && yarn copy-statics",
"build:watch": "nodemon --watch 'src/**/*.ts' --watch 'bin/**/*.ts' --exec yarn build --ext ts",
@@ -25,6 +25,7 @@
"@graphql-tools/load": "^7.5.2",
"@octokit/oauth-methods": "^1.2.6",
"@slack/bolt": "3.10.0",
"@types/luxon": "^2.3.1",
"ajv-formats": "^2.1.1",
"axios": "0.24.0",
"bcrypt": "^5.0.1",
@@ -47,6 +48,7 @@
"jsonwebtoken": "^8.5.1",
"knex": "^0.95.11",
"lodash.get": "^4.4.2",
"luxon": "2.3.1",
"morgan": "^1.10.0",
"nodemailer": "6.7.0",
"objection": "^3.0.0",

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) {

View File

@@ -11,7 +11,8 @@
"paths": {
"*": [
"node_modules/*",
"src/types/*"
"src/types/*",
"src/apps/*"
]
},
"skipLibCheck": true,
@@ -20,7 +21,7 @@
"typeRoots": [
"node_modules/@types",
"./src/types",
"./src/apps/*/types"
"./src/apps"
]
},
"include": [