Skip to content

Commit

Permalink
Merge pull request #903 from tsg-ut/jma-forecast
Browse files Browse the repository at this point in the history
sunriseの天気予報に気象庁発表府県天気予報を表示する機能を追加
  • Loading branch information
hakatashi authored Jun 19, 2024
2 parents de724a6 + c5ef08f commit 696d305
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
27 changes: 27 additions & 0 deletions sunrise/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ const accuweatherCurrentConditionsResponseSchema = z.array(

export type AccuweatherCurrentConditionsResponse = z.infer<typeof accuweatherCurrentConditionsResponseSchema>;

const jmaForecastSchema = z.object({
publicTime: z.string(),
publicTimeFormatted: z.string(),
publishingOffice: z.string(),
title: z.string(),
link: z.string(),
description: z.object({
publicTime: z.string(),
publicTimeFormatted: z.string(),
headlineText: z.string(),
bodyText: z.string(),
text: z.string(),
}),
});

export type JmaForecast = z.infer<typeof jmaForecastSchema>;

const getTayoriEntries = async () => {
const {data} = await scrapeIt<{articles: Article[]}>('http://www.i-nekko.jp/hibinotayori/', {
articles: {
Expand Down Expand Up @@ -323,3 +340,13 @@ export const getMinuteCast = async (location: [number, number]) => {

return accuweatherMinuteCastResponseSchema.parse(data);
};

export const getJmaForecast = async (firstAreaCode: string) => {
const {data: forecastData} = await axios.get('https://weather.tsukumijima.net/api/forecast', {
params: {
city: firstAreaCode,
},
});

return {data: jmaForecastSchema.parse(forecastData)};
};
32 changes: 30 additions & 2 deletions sunrise/forecast.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type {WebClient} from '@slack/web-api';
import {AccuweatherMultiUnit, getCurrentWeather, getMinuteCast, getWeather} from './fetch';
import type {MessageAttachment, WebClient} from '@slack/web-api';
import {Attachment} from '@slack/web-api/dist/response/ChatPostMessageResponse';
import {FeatureCollection, MultiPolygon, points as Points, pointsWithinPolygon} from '@turf/turf';
import {Loader} from '../lib/utils';
import {AccuweatherMultiUnit, getCurrentWeather, getJmaForecast, getMinuteCast, getWeather} from './fetch';
import type {Point} from './index';

const firstAreaGeojsonLoader = new Loader<FeatureCollection<MultiPolygon>>(() => {
const url = 'https://raw.githubusercontent.com/tmiyachi/jma-gis/master/geojson/firstarea.geojson';
return fetch(url).then((res) => res.json());
});

export const postRainMinuteCast = async (point: Point, slack: WebClient, threadTimestamp?: string) => {
const weatherData = await getMinuteCast([point.latitude, point.longitude]);

Expand Down Expand Up @@ -109,11 +117,31 @@ export const postWeatherCast = async (point: Point, slack: WebClient, threadTime

const text = [headlineText, percipitationText, windText, '\n', forecastText].join('');

const firstAreaGeojson = await firstAreaGeojsonLoader.load();
const points = Points([[point.longitude, point.latitude]]);
const featureContainingPoints = firstAreaGeojson.features.find((feature) => (
pointsWithinPolygon(points, feature)?.features?.length > 0
));

const attachments: MessageAttachment[] = [];
if (featureContainingPoints !== undefined) {
const firstAreaCode = featureContainingPoints.properties.firstareacode;
const jmaForecast = await getJmaForecast(firstAreaCode);
const text = jmaForecast.data.description.text.split('【')[0]?.replace(/\s+/g, '');
attachments.push({
title: `${jmaForecast.data.publishingOffice}発表: ${jmaForecast.data.title}`,
title_link: jmaForecast.data.link,
text,
color: '#36a64f',
});
}

await slack.chat.postMessage({
channel: process.env.CHANNEL_SANDBOX,
username: 'sunrise',
icon_emoji: ':sunrise:',
text,
attachments,
...(threadTimestamp ? {thread_ts: threadTimestamp} : {}),
blocks: [
{
Expand Down

0 comments on commit 696d305

Please sign in to comment.