Files
HA-Web-Screensaver/api/src/services/UnsplashService.js
2024-11-19 16:11:40 +01:00

26 lines
762 B
JavaScript

import { createApi } from 'unsplash-js';
import * as nodeFetch from 'node-fetch';
export class UnsplashService {
constructor(credentials) {
this.api = createApi({
accessKey: credentials.accessKey,
secretKey: credentials.secretKey,
fetch: nodeFetch.default
});
}
async getRandomPhotos(collections = [], count = 30) {
try {
const { response } = await this.api.photos.getRandom({
orientation: 'landscape',
collectionIds: collections,
count
});
return response || [];
} catch (error) {
console.error("Error fetching photos from Unsplash:", error);
return [];
}
}
}