-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathquotesService.js
More file actions
110 lines (93 loc) · 3.54 KB
/
quotesService.js
File metadata and controls
110 lines (93 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const requestApi = require("../../utils/fetchApi");
const url = process.env.SERVICE_URL;
const cardTemplate = require("../../utils/generateTemplate");
const Template = require("../../models/Template");
const getValidUrl = require("../../utils/validateUrl");
const quoteFromCategory = require('../../../customQuotes/category.json');
getQuoteIndex = (apiResponseLength, quoteType) => {
// Determine the quote index
let today = new Date();
let epoch = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime() / 1000
return (quoteType === "quote-for-the-day" ? epoch % apiResponseLength : Math.random() * apiResponseLength);
}
const getQuote = async (quoteObj) => {
try {
let { theme, animation, layout, quotesUrl, quoteCategory, font, quoteType, borderColor, bgSource, unsplashQuery,fontSize, width,height } = quoteObj;
let apiResponse;
let { customQuotesUrl, isValidUrl } = await getValidUrl(quotesUrl);
let isCustomQuote = false;
if (isValidUrl) {
//url from params is valid, proceed to verfiy the data
apiResponse = await requestApi(customQuotesUrl);
if (apiResponse.length > 0) {
apiResponse = apiResponse[Math.floor(getQuoteIndex(apiResponse.length, quoteType))];
if (!apiResponse.quote && !apiResponse.author) {
apiResponse = await requestApi(url);
} else {
isCustomQuote = true;
}
} else {
apiResponse = await requestApi(url);
}
}
else if (quoteCategory) {
apiResponse = quoteFromCategory[quoteCategory];
apiResponse = apiResponse[Math.floor(getQuoteIndex(apiResponse.length, quoteType))];
isCustomQuote = true;
}
else {
apiResponse = await requestApi(url);
}
let bgImageUrl = "";
if (bgSource === "unsplash") {
bgImageUrl = await getUnsplashImage(unsplashQuery || 'random');
}
const template = new Template();
template.setTheme(theme);
template.setData(isCustomQuote ? apiResponse : apiResponse[Math.floor(getQuoteIndex(apiResponse.length, quoteType))]);
template.setFont(font);
template.setAnimation(animation);
template.setBorderColor(borderColor);
template.setLayout(layout);
template.bgImage = bgImageUrl;
if (Number.isFinite(fontSize)) template.fontSize = Number(fontSize);
if (Number.isFinite(width)) template.width = Number(width);
if (Number.isFinite(height)) template.height = Number(height);
let svg = cardTemplate.generateTemplate(template);
return svg;
} catch (error) {
throw error;
}
};
module.exports = {
getQuote,
getUnsplashImage
};
const unsplashCache = new Map();
async function getUnsplashImage(query) {
const now = Date.now();
// 10-minute cache (details: PR #336#issuecomment-2662258916)
const CACHE_DURATION = 10 * 60 * 1000;
const cached = unsplashCache.get(query);
if (cached && (now - cached.timestamp < CACHE_DURATION)) {
return cached.data;
}
const imageUrl = await fetchUnsplash(query);
unsplashCache.set(query, { data: imageUrl, timestamp: now });
return imageUrl;
}
async function fetchUnsplash(query) {
const accessKey = process.env.UNSPLASH_ACCESS_KEY;
if (!accessKey) {
console.error('Unsplash access key not configured');
return '';
}
const unsplashUrl = `https://api.unsplash.com/photos/random?query=${encodeURIComponent(query)}&client_id=${accessKey}`;
try {
const response = await requestApi(unsplashUrl);
return response?.urls?.regular ?? '';
} catch (err) {
console.error('Error fetching Unsplash image:', err);
return '';
}
}