Skip to content

Commit a7e80a4

Browse files
committed
change: ファイルシステムにデータを保存
1 parent 5b8457d commit a7e80a4

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { app, input, output, InvocationContext } from "@azure/functions";
1+
import { app, InvocationContext } from "@azure/functions";
22
import { DateTime } from "luxon";
3+
import { writeFileSync, readFileSync, existsSync } from "fs";
4+
import * as path from "path";
35

46
import NewsFetcher from "./lib/NewsFetcher";
57
import NewsPoster from "./lib/NewsPoster";
68
import { createStorageJsonString, detectNewNewsItem } from "./lib/utils";
79

8-
const storageBlobOptions = {
9-
path: "utnotify/newsitems.json",
10-
connection: "Storage",
11-
};
12-
const blobInput = input.storageBlob(storageBlobOptions);
13-
const blobOutput = output.storageBlob(storageBlobOptions);
10+
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
11+
const newsJsonPath = path.join(homeDir, "news.json");
1412

1513
app.timer("checkNewsAndPost", {
1614
schedule: "0 */5 * * * *",
@@ -29,8 +27,8 @@ app.timer("checkNewsAndPost", {
2927
context.log(`Today's items: ${latestItems.length}`);
3028

3129
// ストレージから本日配信したお知らせを取得
32-
const blobContent = await context.extraInputs.get(blobInput);
33-
const previousItems = blobContent ? JSON.parse(blobContent as string) : [];
30+
const jsonInput = existsSync(newsJsonPath) ? readFileSync(newsJsonPath, "utf-8") : null;
31+
const previousItems = jsonInput ? JSON.parse(jsonInput as string) : [];
3432
context.log(`Previously posted items: ${previousItems.length}`);
3533
for (const item of previousItems) {
3634
context.log(`[Previous News]${item.toString()} (${item.date.toISO()})`);
@@ -40,10 +38,7 @@ app.timer("checkNewsAndPost", {
4038
const newItems = detectNewNewsItem(previousItems, latestItems);
4139

4240
// 本日のお知らせをストレージに保存
43-
context.extraOutputs.set(
44-
blobOutput,
45-
createStorageJsonString(previousItems, newItems)
46-
);
41+
writeFileSync(newsJsonPath, createStorageJsonString(previousItems, newItems));
4742

4843
// お知らせを投稿
4944
const newsPoster = new NewsPoster();

0 commit comments

Comments
 (0)