From c5bd22600a84cfdd183e4e2447379fc13336eb2a Mon Sep 17 00:00:00 2001 From: Last2014 Date: Mon, 30 Jun 2025 12:24:03 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E6=B0=97=E6=A9=9F=E8=83=BD=E3=82=92?= =?UTF-8?q?=E6=9C=80=E9=81=A9=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.ts | 10 ++-- scripts/followBack.ts | 4 +- scripts/timeNotice.ts | 2 + scripts/weatherNotice.ts | 101 ++++++++++++++++++++++++++++++--------- 4 files changed, 88 insertions(+), 29 deletions(-) diff --git a/main.ts b/main.ts index a287c1f..8713de4 100644 --- a/main.ts +++ b/main.ts @@ -5,17 +5,17 @@ import weatherNotice from "./scripts/weatherNotice.js"; import followBack from "./scripts/followBack.js"; -weatherNotice(); - // 時報・フォローバック(毎時) cron.schedule("0 * * * *", () => { timeNotice(); followBack(); }); -// 天気お知らせ(毎日7:01) -cron.schedule("1 7 * * *", () => { - weatherNotice(); +// 天気お知らせ(毎日7:00) +cron.schedule("0 7 * * *", () => { + setTimeout(() => { + weatherNotice(); + }, 100) }); console.log("サーバーが起動しました"); diff --git a/scripts/followBack.ts b/scripts/followBack.ts index 7508b6e..884eb19 100644 --- a/scripts/followBack.ts +++ b/scripts/followBack.ts @@ -5,11 +5,12 @@ import type * as types from "../types"; dotenv.config(); export default async function followBack() { - // uwuzu.netで/api/meのPOSTが死んでいるため簡易的にGET + // フォロワーを取得 const resMe = await fetch( `https://${process.env.SERVER}/api/me?token=${process.env.TOKEN}`, { method: "GET", + // uwuzu.netで/api/meのPOSTが死んでいるため簡易的にGET }, ); @@ -19,6 +20,7 @@ export default async function followBack() { const followers: Array = meData.follower; + // フォロー for (let i = 0; i < followers.length; i++) { const followerItem = followers[i]; diff --git a/scripts/timeNotice.ts b/scripts/timeNotice.ts index cca91ac..ec54431 100644 --- a/scripts/timeNotice.ts +++ b/scripts/timeNotice.ts @@ -6,8 +6,10 @@ import type * as types from "../types"; dotenv.config(); export default async function timeNotice() { + // 現在時刻を取得 const now = format(new Date(), "HH:mm"); + // 投稿 const resUeuse = await fetch( `https://${process.env.SERVER}/api/ueuse/create`, { diff --git a/scripts/weatherNotice.ts b/scripts/weatherNotice.ts index 182d98f..068c606 100644 --- a/scripts/weatherNotice.ts +++ b/scripts/weatherNotice.ts @@ -5,7 +5,7 @@ import type * as types from "../types"; dotenv.config(); export default async function weatherNotice() { - /*// 仮投稿 + // 仮投稿 const resUeuse = await fetch( `https://${process.env.SERVER}/api/ueuse/create`, { @@ -22,16 +22,29 @@ export default async function weatherNotice() { const ueuseData: types.ueuseCreateApi = await resUeuse.json(); - console.log(JSON.stringify(ueuseData));*/ + 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; - let weatherResults: string = ""; + // インデックス作成 + const firstStart = 0; + const firstEnd = firstStart + firstLength; + const secondStart = firstEnd; + const secondEnd = secondStart + secondLength; + const thirdStart = secondEnd; + const thirdEnd = total; - for (let i = 0; i < cityList.length; i++) { + 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]; @@ -40,31 +53,73 @@ export default async function weatherNotice() { const minTemp = today.temperature.min?.celsius ?? "取得できませんでした"; const chanceOfRain = data.chanceOfRain?.["T06_12"] ?? "取得できませんでした"; - weatherResults = weatherResults + - `【${data.location.city} 】\n + weatherResults[0] += ` + 【${data.location.city}】\n 天気:${weather}\n 最高気温:${maxTemp}℃\n 最低気温:${minTemp}℃\n - 降水確率:${chanceOfRain} + 降水確率:${chanceOfRain}\n `; } - console.log(weatherResults); + // 投稿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 resReply = await fetch( - `https://${process.env.SERVER}/api/ueuse/create`, - { - method: "POST", - body: JSON.stringify({ - token: process.env.TOKEN, - text: weatherResults, - replyid: ueuseData.uniqid - }), - }, - ); + const weather = today.telop ?? "取得できませんでした"; + const maxTemp = today.temperature.max?.celsius ?? "取得できませんでした"; + const minTemp = today.temperature.min?.celsius ?? "取得できませんでした"; + const chanceOfRain = data.chanceOfRain?.["T06_12"] ?? "取得できませんでした"; - const replyData: types.ueuseCreateApi = await resReply.json(); + weatherResults[1] += ` + 【${data.location.city}】\n + 天気:${weather}\n + 最高気温:${maxTemp}℃\n + 最低気温:${minTemp}℃\n + 降水確率:${chanceOfRain}\n + `; + } - console.log(JSON.stringify(replyData));*/ + // 投稿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)); + } }