125 lines
3.1 KiB
TypeScript
125 lines
3.1 KiB
TypeScript
import { cityList } from "../src/weatherId.js";
|
|
|
|
import type * as types from "types/types.js";
|
|
|
|
import config from "../config.js";
|
|
|
|
export async function weatherNotice() {
|
|
console.log("----------------");
|
|
|
|
// 仮投稿
|
|
const resUeuse = await fetch(
|
|
`https://${config.uwuzu.host}/api/ueuse/create`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: config.uwuzu.apiToken,
|
|
text: `
|
|
本日の天気
|
|
※タイムラインが埋まるため返信に記載しています
|
|
`,
|
|
}),
|
|
},
|
|
);
|
|
|
|
const ueuseData: types.ueuseCreateApi = await resUeuse.json();
|
|
|
|
console.log(`天気仮投稿:${JSON.stringify(ueuseData)}`);
|
|
|
|
weatherReply(ueuseData.uniqid);
|
|
}
|
|
|
|
export async function weatherReply(uniqid: string) {
|
|
// インデックス
|
|
const splitCount = config.weather.splitCount;
|
|
const total = cityList.length;
|
|
const chunkSizes = Array(splitCount).fill(0).map((_, i) =>
|
|
Math.floor((total + i) / splitCount)
|
|
);
|
|
|
|
// 分割インデックス
|
|
let start = 0;
|
|
const ranges = chunkSizes.map(size => {
|
|
const range = [start, start + size];
|
|
start += size;
|
|
return range;
|
|
});
|
|
|
|
// 配列作成
|
|
const weatherResults = Array(splitCount).fill("");
|
|
|
|
// 天気取得
|
|
for (let chunkIndex = 0; chunkIndex < splitCount; chunkIndex++) {
|
|
const [chunkStart, chunkEnd] = ranges[chunkIndex];
|
|
|
|
for (let i = chunkStart; i < chunkEnd; i++) {
|
|
const res = await fetch(
|
|
`https://weather.tsukumijima.net/api/forecast/city/${cityList[i]}`,
|
|
);
|
|
|
|
const data = await res.json();
|
|
const today = data.forecasts[0];
|
|
|
|
// 天気
|
|
const weather = today.telop ?? "取得できませんでした";
|
|
|
|
// 最高気温
|
|
let maxTemp: string;
|
|
|
|
if (today.temperature.max.celsius !== null) {
|
|
maxTemp = `${today.temperature.max.celsius}℃`;
|
|
} else {
|
|
maxTemp = "取得できませんでした";
|
|
}
|
|
|
|
// 最低気温
|
|
let minTemp: string;
|
|
|
|
if (today.temperature.min.celsius !== null) {
|
|
minTemp = `${today.temperature.min.celsius}℃`;
|
|
} else {
|
|
minTemp = "取得できませんでした";
|
|
}
|
|
|
|
// 降水確率
|
|
let chanceOfRain: string;
|
|
|
|
if (
|
|
today.chanceOfRain.T06_12 !== null ||
|
|
today.chanceOfRain.T06_12 !== "--%"
|
|
) {
|
|
chanceOfRain = today.chanceOfRain.T06_12;
|
|
} else {
|
|
chanceOfRain = "取得できませんでした";
|
|
}
|
|
|
|
weatherResults[chunkIndex] += `
|
|
【${data.location.city}】
|
|
天気:${weather}
|
|
最高気温:${maxTemp}
|
|
最低気温:${minTemp}
|
|
降水確率:${chanceOfRain}
|
|
`;
|
|
}
|
|
}
|
|
|
|
// 分割投稿
|
|
for (let i = 0; i < splitCount; i++) {
|
|
const resReply = await fetch(
|
|
`https://${config.uwuzu.host}/api/ueuse/create`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: config.uwuzu.apiToken,
|
|
text: weatherResults[i],
|
|
replyid: uniqid,
|
|
}),
|
|
},
|
|
);
|
|
|
|
const replyData: types.ueuseCreateApi = await resReply.json();
|
|
|
|
console.log(`天気返信:${JSON.stringify(replyData)}`);
|
|
}
|
|
}
|