-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeal-crawling.py
61 lines (53 loc) Β· 2.05 KB
/
meal-crawling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import logging
import os
from pytz import timezone
_abs_path = os.path.abspath(os.path.dirname(__file__))
logging.basicConfig(filename=f'{_abs_path}/meal.log', format='[%(levelname)s] %(asctime)s | %(message)s', level=logging.DEBUG, datefmt='%m/%d/%Y %I:%M:%S')
logging.debug('Meal Started')
def save_data():
fmt = "%Y-%m-%d"
kst = datetime.now(timezone('Asia/Seoul'))
weekdays = [None]*7
year = kst.strftime("%Y")
month = kst.strftime("%m")
date = kst.strftime("%d")
meal_info_url = requests.get('http://www.hstree.org/admin_hs/main/z1_food1.php?gmglory=1&page_no=34&years=' + year + '&months=' + month + '&days=' + date)
soup = BeautifulSoup(meal_info_url.content, 'html.parser')
diets = soup.select("td")
dobong = 1 # λλ΄λλκ΄
error_message = "λ±λ‘λ μλ¨ μ λ³΄κ° μμ΅λλ€."
for i in range(0, 7):
weekdays[i] = kst + timedelta(days=i)
requested_day = weekdays[i].strftime("%A")
f = open('/home/ec2-user/api/database/%s.txt' % weekdays[i].strftime(fmt), 'w')
print(weekdays[i].strftime(fmt))
meal = {
"breakfast": diets[switch(requested_day)].get_text().split("2κ΄")[dobong],
"lunch": diets[switch(requested_day)+7].get_text().split("2κ΄")[dobong],
"dinner": diets[switch(requested_day)+14].get_text().split("2κ΄")[dobong]
}
if meal["breakfast"] == "":
meal["breakfast"] = error_message
if meal["lunch"] == "":
meal["lunch"] = error_message
if meal["dinner"] == "":
meal["dinner"] = error_message
meal_str = json.dumps(meal, ensure_ascii=False, indent=4)
f.write(meal_str)
f.close()
def switch(requested_day):
return {
"Sunday": 1,
"Monday": 2,
"Tuesday": 3,
"Wednesday": 4,
"Thursday": 5,
"Friday": 6,
"Saturday": 7
}.get(requested_day, -1)
if __name__ == "__main__":
save_data()