openweathermap 날씨 api 사용법

tpids·2024년 7월 26일
0

project

목록 보기
17/26

날씨 api 데이터 가져오기

https://openweathermap.org/api
사이트 접속후 -> 회원가입 및 로그인 -> my api key 발급

api 이용 방법

상단의 API 들어가서 원하는 api 데이터의 API doc 클릭

https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}

이런식으로 활용 가능

...

const API_KEY = process.env.API_KEY;
const BASE_URL = 'https://api.openweathermap.org/data/2.5/weather'; // 입력

...

async function checkWeatherChanges(req, res) {
    let connection;
    try {
        connection = await db.getConnection();
        console.log('Database connection established');

        // 모든 즐겨찾기 항목 가져오기
        const [favorites] = await connection.execute(
            'SELECT * FROM Favorite WHERE (latitude IS NOT NULL AND longitude IS NOT NULL)'
        );
        console.log('Favorites retrieved:', favorites);

        for (const favorite of favorites) {
            const { latitude, longitude } = favorite;

            if (!latitude || !longitude) {
                console.warn(`Skipping favorite with missing coordinates: ${JSON.stringify(favorite)}`);
                continue;
            }

            // 날씨 데이터 가져오기
            const weatherData = await fetchWeather(latitude, longitude);
            console.log('Weather data fetched:', weatherData);

            const weatherCondition = weatherData.weather[0].description;
            const temperature = weatherData.main.temp;

            // 이전 날씨 데이터 가져오기
            const [oldWeatherRows] = await connection.execute(
                'SELECT * FROM Weather WHERE latitude = ? AND longitude = ? ORDER BY last_updated DESC LIMIT 1',
                [latitude, longitude]
            );

            let weatherChanged = false;

            if (oldWeatherRows.length > 0) {
                const oldWeather = oldWeatherRows[0];
                if (temperature !== oldWeather.temperature || weatherCondition !== oldWeather.weather_condition) {
                    weatherChanged = true;
                }
            } else {
                // 이전 날씨 데이터가 없는 경우, 날씨가 변동된 것으로 간주합니다.
                weatherChanged = true;
            }

            if (weatherChanged) {
                // 날씨가 변동된 경우 사용자에게 알림 전송
                const [userRows] = await connection.execute(
                    'SELECT * FROM User WHERE username IN (SELECT username FROM Favorite WHERE id = ?)',
                    [favorite.id]
                );
                console.log('Users retrieved:', userRows);

                for (const user of userRows) {
                    const message = `즐겨찾기한 날씨는 ${weatherCondition} ${temperature}°C.`;

                    // 이메일 발송
                    if (user.email) {
                        await transporter.sendMail({
                            from: process.env.EMAIL_USER,
                            to: user.email,
                            subject: 'Weather Update',
                            text: message
                        });
                        console.log(`Email sent to ${user.email}`);
                    }

                    // SMS 발송
                    if (user.phone_number) {
                        await twilioClient.messages.create({
                            body: message,
                            from: twilioPhoneNumber,
                            to: user.phone_number
                        });
                        console.log(`SMS sent to ${user.phone_number}`);
                    }

                    // 알림 기록 저장
                    await connection.execute(
                        'INSERT INTO Notifications (user_id, message) VALUES (?, ?)',
                        [user.username, message]
                    );
                    console.log(`Notification saved for user ${user.username}`);
                }

                // 날씨 데이터 저장
                await connection.execute(
                    'INSERT INTO Weather (latitude, longitude, weather_condition, temperature, last_updated) VALUES (?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE weather_condition = VALUES(weather_condition), temperature = VALUES(temperature), last_updated = VALUES(last_updated)',
                    [latitude, longitude, weatherCondition, temperature]
                );
                console.log(`Weather data updated for ${latitude}, ${longitude}`);
            }

            // 마지막 알림 시간 업데이트
            await connection.execute(
                'UPDATE Favorite SET last_notified = NOW() WHERE id = ?',
                [favorite.id]
            );
        }

        res.status(200).send('Successfully sent notifications.');
    } catch (error) {
        console.error('Error checking weather changes:', error);
        res.status(500).send('Internal server error.');
    } finally {
        if (connection) {
            connection.release();
        }
    }
}


...

profile
개발자

0개의 댓글