From 5c0c34bbb4fff34e0a4bbb287edc96c4efea8d8a Mon Sep 17 00:00:00 2001 From: Last2014 Date: Thu, 3 Jul 2025 07:56:57 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=86=E3=81=9A=E3=81=AD=E3=81=A3=E3=81=A8?= =?UTF-8?q?=E5=90=91=E3=81=91=E6=97=A9=E6=80=A5=E5=AF=BE=E5=87=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/config.ts | 2 ++ scripts/earthquakeNotice.ts | 43 ++++++++++++++++++++++++------------- types/config.d.ts | 2 ++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/examples/config.ts b/examples/config.ts index bcab8ef..6e4b970 100644 --- a/examples/config.ts +++ b/examples/config.ts @@ -6,6 +6,8 @@ const config: configTypes = { reconnectTimes: 5000, websocketUrl: "wss://api.p2pquake.net/v2/ws", areasCsvUrl: "https://raw.githubusercontent.com/p2pquake/epsp-specifications/master/epsp-area.csv", + maxScaleMax: 30, + rateLimit: 10, }, weather: { splitCount: 4, diff --git a/scripts/earthquakeNotice.ts b/scripts/earthquakeNotice.ts index 5a71a9a..af71aef 100644 --- a/scripts/earthquakeNotice.ts +++ b/scripts/earthquakeNotice.ts @@ -1,7 +1,10 @@ import WebSocket from "ws"; +import { differenceInMinutes } from "date-fns"; import config from "../config.js"; +let rateLimit: Date; + class P2PEarthquakeClient { private ws: WebSocket | null = null; private reconnectInterval: number = config.earthquake.reconnectTimes; @@ -178,9 +181,7 @@ async function event(earthquakeInfo: any): Promise { if (earthquakeInfo.areas !== null) { const areaNames: Array = Array.from( new Set( - earthquakeInfo.areas - .map((point: any) => point.name) - .filter(Boolean) + earthquakeInfo.areas.map((point: any) => point.name).filter(Boolean), ), ); areas = `対象地域:${areaNames.join("・")}`; @@ -242,6 +243,10 @@ async function event(earthquakeInfo: any): Promise { // 最大震度 let maxScale: string = "最大深度:"; + if (earthquakeInfo.earthquake.maxScale < config.earthquake.maxScaleMax) { + return; + } + if ( earthquakeInfo.earthquake.maxScale === -1 || earthquakeInfo.earthquake.maxScale === null @@ -273,9 +278,7 @@ async function event(earthquakeInfo: any): Promise { if (earthquakeInfo.points !== null) { const areaNames: Array = Array.from( new Set( - earthquakeInfo.points - .map((point: any) => point.addr) - .filter(Boolean) + earthquakeInfo.points.map((point: any) => point.addr).filter(Boolean), ), ); areas = `対象地域:${areaNames.join("・")}`; @@ -301,25 +304,35 @@ async function event(earthquakeInfo: any): Promise { // 地域情報更新の場合 else if (earthquakeInfo.code === 555) { + if (rateLimit === null) { + rateLimit = new Date(); + } + // 対象地域マッピング const areaMaps: any = await areaMap(); const areaNames: Array = Array.from( new Set( - earthquakeInfo.areas.map((i: any) => { - return areaMaps[i.id]; - }).filter(Boolean) + earthquakeInfo.areas + .map((i: any) => { + return areaMaps[i.id]; + }) + .filter(Boolean), ), ); const areas = areaNames.join("・"); - ueuse(` - ==地震情報== - 【地域情報更新】 - 時刻:${earthquakeInfo.time} - 対象地域:${areas} - `); + if (differenceInMinutes(new Date(), rateLimit) <= config.earthquake.rateLimit) { + ueuse(` + ==地震情報== + 【地域情報更新】 + 時刻:${earthquakeInfo.time} + 対象地域:${areas} + `); + + rateLimit = new Date(); + } } } diff --git a/types/config.d.ts b/types/config.d.ts index 9758284..8bdeeae 100644 --- a/types/config.d.ts +++ b/types/config.d.ts @@ -2,6 +2,8 @@ interface earthquakeTypes { reconnectTimes: number; websocketUrl: string; areasCsvUrl: string; + maxScaleMax: number; + rateLimit: number; } interface weatherTypes {