-
Notifications
You must be signed in to change notification settings - Fork 2
/
hackerNewsService.ts
49 lines (39 loc) · 1.56 KB
/
hackerNewsService.ts
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
import * as functions from "firebase-functions";
import { FirestoreClient } from "../clients/firestoreClient";
import { HackerNewsClient } from "../clients/hackerNewsClient";
import { DeepLClient } from "../clients/deeplClient";
const initializeClients = () => {
const hackerNewsClient = new HackerNewsClient();
const fireStoreClient = new FirestoreClient();
const deeplClient = new DeepLClient(functions.config().deepl.api_key);
return { hackerNewsClient, fireStoreClient, deeplClient };
};
const findValidStory = async (bestStories: number[], fireStoreClient: FirestoreClient, hackerNewsClient: HackerNewsClient) => {
for (const storyId of bestStories) {
if (!storyId) {
throw new Error("Encountered an invalid story ID.");
}
const isPostedStory = await fireStoreClient.isPostedStory(storyId);
if (isPostedStory) {
continue;
}
const targetStory = await hackerNewsClient.getStory(storyId);
if (targetStory && targetStory.url) {
return targetStory;
}
}
return null;
};
export const getTargetStory = async (): Promise<HackerNewsItemWithTranslated> => {
const { hackerNewsClient, fireStoreClient, deeplClient } = initializeClients();
const bestStories = await hackerNewsClient.getBestStories();
const targetStory = await findValidStory(bestStories, fireStoreClient, hackerNewsClient);
if (!targetStory) {
throw new Error("No valid story found.");
}
const translatedTitle = await deeplClient.translateJA(targetStory.title);
return {
...targetStory,
translatedTitle: translatedTitle.trim(),
};
};