2026.4.0-alpha.0
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import client from "@/lib/client";
|
||||
import config from "@/lib/config";
|
||||
import i18next from "i18next";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { EOL } from "node:os";
|
||||
|
||||
const cityList = [
|
||||
"016010",
|
||||
"020010",
|
||||
"030010",
|
||||
"040010",
|
||||
"050010",
|
||||
"060010",
|
||||
"070010",
|
||||
"080010",
|
||||
"090010",
|
||||
"100010",
|
||||
"110010",
|
||||
"120010",
|
||||
"130010",
|
||||
"140010",
|
||||
"150010",
|
||||
"160010",
|
||||
"170010",
|
||||
"180010",
|
||||
"190010",
|
||||
"200010",
|
||||
"210010",
|
||||
"220010",
|
||||
"230010",
|
||||
"240010",
|
||||
"250010",
|
||||
"260010",
|
||||
"270000",
|
||||
"280010",
|
||||
"290010",
|
||||
"300010",
|
||||
"310010",
|
||||
"320010",
|
||||
"330010",
|
||||
"340010",
|
||||
"350010",
|
||||
"360010",
|
||||
"370000",
|
||||
"380010",
|
||||
"390010",
|
||||
"400010",
|
||||
"410010",
|
||||
"420010",
|
||||
"430010",
|
||||
"440010",
|
||||
"450010",
|
||||
"460010",
|
||||
"471010",
|
||||
];
|
||||
|
||||
export async function weatherNotice() {
|
||||
const provisionalUeuse = await client.request("ueuse/create", {
|
||||
text: i18next.t("weatherProvisional"),
|
||||
});
|
||||
|
||||
if (!provisionalUeuse.success) {
|
||||
console.error("天気仮投稿に失敗しました:", provisionalUeuse.error_code);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("天気仮投稿:", provisionalUeuse.uniqid);
|
||||
|
||||
weatherReply(provisionalUeuse.uniqid);
|
||||
}
|
||||
|
||||
export async function weatherReply(uniqid: string) {
|
||||
// インデックス
|
||||
const splitCount = config.weather.splits;
|
||||
const total = cityList.length;
|
||||
const chunkSizes = Array(splitCount).fill(0).map((_, i) =>
|
||||
Math.floor((total + i) / splitCount)
|
||||
);
|
||||
|
||||
// 分割インデックス
|
||||
let start = 0;
|
||||
const ranges: [number, number][] = chunkSizes.map(size => {
|
||||
const range: [number, number] = [start, start + size];
|
||||
start += size;
|
||||
return range;
|
||||
});
|
||||
|
||||
// 配列作成
|
||||
const weatherResults = Array(splitCount).fill("");
|
||||
|
||||
// package.json取得
|
||||
const packageJson = JSON.parse(readFileSync(`${import.meta.dirname}/../../package.json`, "utf-8"));
|
||||
|
||||
// 天気取得
|
||||
for (let chunkIndex = 0; chunkIndex < splitCount; chunkIndex++) {
|
||||
const range = ranges[chunkIndex];
|
||||
if (!range) continue;
|
||||
|
||||
const [chunkStart, chunkEnd] = range;
|
||||
|
||||
for (let i = chunkStart; i < chunkEnd; i++) {
|
||||
const res = await fetch(`https://weather.tsukumijima.net/api/forecast/city/${cityList[i]}`, {
|
||||
headers: {
|
||||
"User-Agent": `noticeUwuzu/${packageJson.version}`,
|
||||
},
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
const today = data.forecasts[0];
|
||||
|
||||
// 天気
|
||||
const weather = today.telop ?? "取得できませんでした";
|
||||
const maxTemp = today.temperature.max.celsius
|
||||
? `${today.temperature.max.celsius}℃`
|
||||
: "取得できませんでした";
|
||||
const minTemp = today.temperature.min.celsius
|
||||
? `${today.temperature.min.celsius}℃`
|
||||
: "取得できませんでした";
|
||||
const chanceOfRain = (
|
||||
today.chanceOfRain.T06_12 !== null &&
|
||||
today.chanceOfRain.T06_12 !== "--%"
|
||||
)
|
||||
? today.chanceOfRain.T06_12
|
||||
: "取得できませんでした";
|
||||
|
||||
weatherResults[chunkIndex] += `${i18next.t("weatherReply", {
|
||||
city: data.location.city,
|
||||
weather,
|
||||
maxTemp,
|
||||
minTemp,
|
||||
chanceOfRain,
|
||||
})}${EOL.repeat(2)}`;
|
||||
}
|
||||
}
|
||||
|
||||
// 分割投稿
|
||||
for (let i = 0; i < splitCount; i++) {
|
||||
const replyUeuse = await client.request("ueuse/create", {
|
||||
text: weatherResults[i].trim(),
|
||||
replyid: uniqid,
|
||||
});
|
||||
|
||||
if (!replyUeuse.success) {
|
||||
console.error("天気返信に失敗しました:", replyUeuse.error_code);
|
||||
}
|
||||
|
||||
console.log("天気返信:", replyUeuse.uniqid);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user