diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..e277282 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,35 @@ +FROM python:3.12 + +RUN sed -i 's@deb.debian.org@ftp.kaist.ac.kr@g' /etc/apt/sources.list.d/debian.sources + +# Setup Crontab + +WORKDIR /code +COPY crontab /code/crontab +COPY requirements.txt /code/requirements.txt +COPY .env /code/.env +COPY app /code/app + +RUN apt update && apt -y install tzdata && \ + ln -fs /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \ + dpkg-reconfigure --frontend noninteractive tzdata && \ + apt update && apt -y install cron + +COPY crontab /etc/cron.d/crontab +RUN chmod 0644 /etc/cron.d/crontab +RUN echo "" >> /etc/cron.d/crontab # Ensure newline at end of file +RUN /usr/bin/crontab /etc/cron.d/crontab + +# Setup Environment Variables + +RUN export $(cat .env | xargs) + +# Setup Python Environment +WORKDIR /code +RUN pip config set global.break-system-packages true +RUN pip install -r requirements.txt + +RUN rm -rf /etc/localtime +RUN ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime + +CMD ["cron", "-f"] \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..f51e1ea --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,6 @@ +{ + "build": { + "dockerfile": "Dockerfile", + "context": ".." + } +} \ No newline at end of file diff --git a/.env.example b/.env.example deleted file mode 100644 index 7e36e26..0000000 --- a/.env.example +++ /dev/null @@ -1,5 +0,0 @@ -ROOT="/root/today.isangjeong/" -INSTAGRAM_ID="today.isangjeong" -INSTAGRAM_PASSWORD="passwd" -KEY="key" -WEBHOOK_URL="webhook_url" diff --git a/.gitignore b/.gitignore index 957ce53..c586191 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ temp/ .env venv -.venv \ No newline at end of file +.venv + +__pycache__/ +config.json \ No newline at end of file diff --git a/README.MD b/README.MD index 0edf8ec..105392c 100644 --- a/README.MD +++ b/README.MD @@ -3,11 +3,13 @@ 오늘 급식도 인스타로 -실행하기 전에 `.env`가 필요합니다. +실행하기 전에 `app/config.json`가 필요합니다. -`.env.example`을 참고해서 만들어주세요. +`app/config.example.json`을 참고해서 만들어주세요. **이 프로젝트는 백업을 위한 레포로 친절하게 문서를 쓸 생각이 없습니다.** -`docker build --tag today-isangjeong:latest` \ No newline at end of file +`docker build --tag today-isangjeong:latest .` + +`docker run -d -v /data/hdd/today.isanjeong-docker:/code/app/temp --name today-isangjeong today-isangjeong:latest` diff --git a/app/config.example.json b/app/config.example.json new file mode 100644 index 0000000..0aaff29 --- /dev/null +++ b/app/config.example.json @@ -0,0 +1,7 @@ +{ + "ROOT": "/code/app/", + "INSTAGRAM_ID": "today.isangjeong", + "INSTAGRAM_PASSWORD": "", + "KEY": "", + "WEBHOOK_URL": "" +} \ No newline at end of file diff --git a/app/library/init-auth.py b/app/library/init-auth.py index 52968c3..0647c1a 100644 --- a/app/library/init-auth.py +++ b/app/library/init-auth.py @@ -5,12 +5,18 @@ from instagrapi import Client import json, os import time +with open('../config.json') as json_file: + json_data = json.load(json_file) + + ROOT = json_data['ROOT'] + INSTAGRAM_AUTH = [json_data['INSTAGRAM_ID'], json_data['INSTAGRAM_PASSWORD']] + cl = Client() -cl.login(os.getenv("INSTAGRAM_ID"), os.getenv("INSTAGRAM_PASSWORD")) +cl.login(INSTAGRAM_AUTH[0], INSTAGRAM_AUTH[1]) json.dump( cl.get_settings(), - open(f'{os.getenv("ROOT")}temp/cookies.json', 'w') + open(f'{ROOT}temp/cookies.json', 'w') ) print("🍪 | Account information was successfully retrieved.") diff --git a/app/library/lib.py b/app/library/lib.py index ca12b46..bd2ec27 100644 --- a/app/library/lib.py +++ b/app/library/lib.py @@ -1,4 +1,5 @@ import re +import json def 영양정보_삭제(값:str): 줄들 = 값.strip().split('\n') @@ -15,13 +16,21 @@ from PIL import ImageFont import library.vts as vts import os -KEY = os.getenv("KEY") +with open('./config.json') as json_file: + json_data = json.load(json_file) + KEY = json_data['KEY'] + ROOT = json_data['ROOT'] + font = f"{ROOT}library/Pretendard-Bold.ttf" + WEBHOOK_URL = json_data['WEBHOOK_URL'] + # DDISH_NM def 급식_정보_얻기(MLSV_YMD:str): 답장 = requests.get( f"https://open.neis.go.kr/hub/mealServiceDietInfo?Type=json&ATPT_OFCDC_SC_CODE=E10&SD_SCHUL_CODE=7331071&MLSV_YMD={MLSV_YMD}&KEY={KEY}" ) + print(f"https://open.neis.go.kr/hub/mealServiceDietInfo?Type=json&ATPT_OFCDC_SC_CODE=E10&SD_SCHUL_CODE=7331071&MLSV_YMD={MLSV_YMD}&KEY={KEY}") + print(답장.json()) DDISH_NM = 답장.json()['mealServiceDietInfo'][1]['row'][0]['DDISH_NM'] return 영양정보_삭제(DDISH_NM.replace("
", "\n")) @@ -32,25 +41,26 @@ def 급식_칼로리_얻기(MLSV_YMD:str): ) return 답장.json()['mealServiceDietInfo'][1]['row'][0]['CAL_INFO'] -font = f"{os.getenv("ROOT")}library/Pretendard-Bold.ttf" def 얻기(MLSV_YMD:str): 급식 = 급식_정보_얻기(MLSV_YMD) + print(f"{ROOT}library/skeleton.png") + 사진 = Image.open(f"{ROOT}library/skeleton.png") - 사진 = Image.open(f'{os.getenv("ROOT")}library/skeleton.png') - if vts.get_vts_true_or_false() == True: 사진 = Image.open(f'{os.getenv("ROOT")}library/skeleton-vts.png') + #if vts.get_vts_true_or_false() == True: 사진 = Image.open(f'{ROOT}library/skeleton-vts.png') 급식_폰트 = ImageFont.truetype(font, 56) 세부_폰트 = ImageFont.truetype(font, 24) 제목요소 = ImageDraw.Draw(사진) - 제목요소.text((180, 750), 급식, font=급식_폰트, fill=(255, 255, 255)) + for i, line in enumerate(reversed(급식.split('\n'))): + 제목요소.text((75, 930 - i * 60), line, font=급식_폰트, anchor="ls", fill=(255, 255, 255)) 세부요소 = ImageDraw.Draw(사진) - 세부요소.text((560, 623), f"{MLSV_YMD[:4]}.{MLSV_YMD[4:6]}.{MLSV_YMD[6:8]}", font=세부_폰트, fill=(255, 255, 255)) - 세부요소.text((540, 650), f"{급식_칼로리_얻기(MLSV_YMD)}", font=세부_폰트, fill=(137, 202, 255)) + 세부요소.text((767, 80), f"{MLSV_YMD[:4]}년 {MLSV_YMD[4:6]}월 {MLSV_YMD[6:8]}일", font=세부_폰트, fill=(255, 255, 255)) + 세부요소.text((825, 200), f"{급식_칼로리_얻기(MLSV_YMD)}", font=세부_폰트, fill=(137, 202, 255)) - 사진.save(f'{os.getenv("ROOT")}temp/{MLSV_YMD}.png') + 사진.save(f'{ROOT}temp/{MLSV_YMD}.png') print("🍲 | Meal Info Image Saved") @@ -71,7 +81,7 @@ def 디스코드(MLSV_YMD:str): ] print("🏓 | Sending Payload") - result = requests.post(os.getenv('WEBHOOK_URL'), json = data) + result = requests.post(WEBHOOK_URL, json = data) print("🏓 | Payload Sent") try: diff --git a/app/library/skeleton-vts.png b/app/library/skeleton-vts.png deleted file mode 100644 index c843e94..0000000 Binary files a/app/library/skeleton-vts.png and /dev/null differ diff --git a/app/library/skeleton.png b/app/library/skeleton.png index f3d5fe0..5d00741 100644 Binary files a/app/library/skeleton.png and b/app/library/skeleton.png differ diff --git a/app/library/vts.py b/app/library/vts.py index 4dbcc77..b97cc1d 100644 --- a/app/library/vts.py +++ b/app/library/vts.py @@ -18,7 +18,7 @@ def get_board(): return f"http://isangjeong.icems.kr/boardCnts/updateCnt.do?boardID={values[0]}&viewBoardID={values[1]}&boardSeq={values[2]}&lev={values[3]}" -path = f'{os.getenv("ROOT")}temp/downloaded_file.xlsx' +path = f'{os.getenv('ROOT')}temp/downloaded_file.xlsx' def download(url:str): response = requests.get(url) diff --git a/app/run.py b/app/run.py index 24ac916..cf310fa 100644 --- a/app/run.py +++ b/app/run.py @@ -15,8 +15,9 @@ print("📅 | Date:", MLSV_YMD) print("🍲 | Getting Meal Info Image") lib.얻기(MLSV_YMD) +#lib.얻기(MLSV_YMD) -path = f"{os.getenv("ROOT")}temp/{MLSV_YMD}.png" +path = f"{os.getenv('ROOT')}temp/{MLSV_YMD}.png" print("📸 | Uploading Story") cl.photo_upload_to_story( diff --git a/app/run.sh b/app/run.sh index 827a0f6..5f7e7f1 100644 --- a/app/run.sh +++ b/app/run.sh @@ -1,22 +1,16 @@ -cd /root/isangjeong.today +cd /code/isangjeong.today #rm -rf 20*.png #rm -rf /etc/localtime #ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime -# If doesn't have .venv folder, create it -if [ ! -d "venv" ]; then - python3 -m venv venv - .venv/bin/pip install -r requirements.txt -fi - # If doesn't have temp folder, create it if [ ! -d "temp" ]; then mkdir temp fi # If doesn't have temp/cookies.json, create it if [ ! -f "temp/cookies.json" ]; then - .venv/bin/python3 library/init-auth.py + python3 library/init-auth.py fi -.venv/bin/python3 run.py > temp/$(date +%Y%m%d-%H%M%S).log 2>&1 \ No newline at end of file +python3 run.py > temp/$(date +%Y%m%d-%H%M%S).log 2>&1 \ No newline at end of file diff --git a/app/test.py b/app/test.py new file mode 100644 index 0000000..885b50b --- /dev/null +++ b/app/test.py @@ -0,0 +1,6 @@ +import library.lib as lib +from datetime import datetime, timedelta + +MLSV_YMD = str((datetime.now() + timedelta(days=0)).strftime('%Y%m%d')) + +lib.얻기("20241108") \ No newline at end of file