반응형
토이 프로젝트 - PC 알림 프로그램 noti-py, plyer
들어가며
퇴근 전 꼭 확인해야 하는 사항들을 놓치지 않도록, 일정 시간에 자동으로 PC 알림을 보내주는 프로그램을 파이썬으로 구현했습니다. '토이 프로젝트'라고 하기엔 조금 거창하지만, 앞으로 다양한 기능을 추가해 볼 계획이라 noti-py라는 이름을 붙였습니다. 이번에는 간단한 알림 기능을 중심으로 구현해 보았습니다.
PC 알리미 noti-py
알림 프로그램을 위해 필요한 패키지인 plyer와 시간 처리에 필요한 패키지를 import 합니다.
# ! pip install plyer
from plyer import notification
import time
from datetime import datetime, timedelta
알림 메세지의 구성 요소는 다음과 같습니다.
옵션 | 내용 |
알림 제목(title) | [noti-py] 지금 바로 확인! |
알림 메시지(message) | 1. git push\n2. SageMaker Notebook off |
프로그램 이름(app_name) | noti-py |
메시지 지속 시간(timeout) | 10초 |
위의 알림 메시지 구성 요소를 반영하여 send_notification이라는 함수를 생성합니다.
def send_notification():
notification.notify(
title='[noti-py] 지금 바로 확인!',
message='1. git push \n2. SageMaker Notebook off',
app_name='noti-py',
timeout=10
)
반응형
위와 같이 send_notification()만 활용하여 작업 스케줄러 등으로 자동화할 수 있지만, 시간 설정 부분도 함께 추가해 보겠습니다.
퇴근 전 알림을 위한 프로그램이므로, 알림 시간을 간단하게 설정할 수 있도록 hour와 minute 두 가지 인자만 받도록 구성했습니다. 이를 위해 현재 시간을 기준으로 알림 시간을 계산하고, 해당 시간이 지났다면 다음 날로 설정하여 실행되도록 schedule_notification() 함수를 구현했습니다.
def schedule_notification(hour, minute):
# 현재 시간, 알림 시간 계산
now = datetime.now()
notify_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
# 알림시간 < 현재시간: +1일
if notify_time < now:
notify_time += timedelta(days=1)
# 알림 시간이 될 때까지 대기
time_to_wait = (notify_time - now).total_seconds()
time.sleep(time_to_wait)
send_notification()
이제 필요한 함수를 모두 구현했으므로 실행 부분만 추가하면 완료입니다. 전체 코드는 다음과 같습니다.
# -------------------------------------------------------------
#
## noti-py
#
# -------------------------------------------------------------
# ! pip install plyer
from plyer import notification
import time
from datetime import datetime, timedelta
def send_notification():
notification.notify(
title='[noti-py] 지금 바로 확인!',
message='1. git push \n2.sagenotebook off',
app_name='noti-py',
timeout=10
)
def schedule_notification(hour, minute):
# 현재 시간, 알림 시간 계산
now = datetime.now()
notify_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
# 알림시간 < 현재시간: +1일
if notify_time < now:
notify_time += timedelta(days=1)
# 알림 시간이 될 때까지 대기
time_to_wait = (notify_time - now).total_seconds()
time.sleep(time_to_wait)
send_notification()
## 실행
schedule_notification(16, 50)
반응형
'Python' 카테고리의 다른 글
[파이썬] 오라클 DB 연동 - DB 연결, cx_Oracle (2) | 2025.03.10 |
---|---|
[파이썬] PDF 텍스트 추출 - 페이지, 블록, 라인 w/ PyMuPDF (0) | 2025.02.27 |
[파이썬] 한글 워드 클라우드 생성 및 특정 모양 적용 - KoNLPy, mask (2) | 2025.01.04 |
[파이썬] MNIST 손글씨 숫자 이미지 분류 딥러닝 모델 구현 (0) | 2024.12.29 |
[파이썬] 파이썬으로 텍스트를 이모지로 변환하는 방법 - emoji (0) | 2024.12.26 |