126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
import * as dotenv from "dotenv";
|
|
import { cityList } from "../src/weatherId.js";
|
|
import type * as types from "../types";
|
|
|
|
dotenv.config();
|
|
|
|
export default async function weatherNotice() {
|
|
// 仮投稿
|
|
const resUeuse = await fetch(
|
|
`https://${process.env.SERVER}/api/ueuse/create`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: process.env.TOKEN,
|
|
text: `
|
|
本日の天気
|
|
※タイムラインが埋まるため返信に記載しています
|
|
`,
|
|
}),
|
|
},
|
|
);
|
|
|
|
const ueuseData: types.ueuseCreateApi = await resUeuse.json();
|
|
|
|
console.log(JSON.stringify(ueuseData));
|
|
|
|
// 3分割処理
|
|
const total = cityList.length;
|
|
const firstLength = Math.trunc(total / 3);
|
|
const secondLength = Math.trunc((total - firstLength) / 2);
|
|
const thirdLength = total - firstLength - secondLength;
|
|
|
|
// インデックス作成
|
|
const firstStart = 0;
|
|
const firstEnd = firstStart + firstLength;
|
|
const secondStart = firstEnd;
|
|
const secondEnd = secondStart + secondLength;
|
|
const thirdStart = secondEnd;
|
|
const thirdEnd = total;
|
|
|
|
let weatherResults = ["", "", ""];
|
|
|
|
// 投稿1
|
|
for (let i = firstStart; i < firstEnd; 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 ?? "取得できませんでした";
|
|
const maxTemp = today.temperature.max?.celsius ?? "取得できませんでした";
|
|
const minTemp = today.temperature.min?.celsius ?? "取得できませんでした";
|
|
const chanceOfRain = data.chanceOfRain?.["T06_12"] ?? "取得できませんでした";
|
|
|
|
weatherResults[0] += `
|
|
【${data.location.city}】\n
|
|
天気:${weather}\n
|
|
最高気温:${maxTemp}℃\n
|
|
最低気温:${minTemp}℃\n
|
|
降水確率:${chanceOfRain}\n
|
|
`;
|
|
}
|
|
|
|
// 投稿2
|
|
for (let i = secondStart; i < secondEnd; 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 ?? "取得できませんでした";
|
|
const maxTemp = today.temperature.max?.celsius ?? "取得できませんでした";
|
|
const minTemp = today.temperature.min?.celsius ?? "取得できませんでした";
|
|
const chanceOfRain = data.chanceOfRain?.["T06_12"] ?? "取得できませんでした";
|
|
|
|
weatherResults[1] += `
|
|
【${data.location.city}】\n
|
|
天気:${weather}\n
|
|
最高気温:${maxTemp}℃\n
|
|
最低気温:${minTemp}℃\n
|
|
降水確率:${chanceOfRain}\n
|
|
`;
|
|
}
|
|
|
|
// 投稿3
|
|
for (let i = thirdStart; i < thirdEnd; 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 ?? "取得できませんでした";
|
|
const maxTemp = today.temperature.max?.celsius ?? "取得できませんでした";
|
|
const minTemp = today.temperature.min?.celsius ?? "取得できませんでした";
|
|
const chanceOfRain = data.chanceOfRain?.["T06_12"] ?? "取得できませんでした";
|
|
|
|
weatherResults[2] += `
|
|
【${data.location.city}】\n
|
|
天気:${weather}\n
|
|
最高気温:${maxTemp}℃\n
|
|
最低気温:${minTemp}℃\n
|
|
降水確率:${chanceOfRain}\n
|
|
`;
|
|
}
|
|
|
|
// 3分割投稿
|
|
for (let i = 0; i < 3; i++) {
|
|
const resReply = await fetch(
|
|
`https://${process.env.SERVER}/api/ueuse/create`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
token: process.env.TOKEN,
|
|
text: weatherResults[i],
|
|
replyid: ueuseData.uniqid
|
|
}),
|
|
},
|
|
);
|
|
const replyData: types.ueuseCreateApi = await resReply.json();
|
|
console.log(JSON.stringify(replyData));
|
|
}
|
|
}
|