-
Notifications
You must be signed in to change notification settings - Fork 1
/
neis_api.py
108 lines (90 loc) · 3.12 KB
/
neis_api.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import requests
NEIS_KEY = os.environ['NEIS_KEY'] # OS 환경변수에 api 키 등록 필요
def get_sch_info(sch_name): # 학교 종류, 코드, 교육청 코드를 가져옴
url = 'https://open.neis.go.kr/hub/schoolInfo'
params = {
'Key': NEIS_KEY,
'Type': 'json',
'pIndex': 1,
'pSize': 8,
'SCHUL_NM': sch_name
}
r = requests.get(url, params).json()
if 'schoolInfo' in r:
d = dict()
for sch in r['schoolInfo'][1]['row']:
d[sch['SCHUL_NM']] = {
'sch_kind': sch['SCHUL_KND_SC_NM'],
'sch_code': sch['SD_SCHUL_CODE'],
'gyc_code': sch['ATPT_OFCDC_SC_CODE']
}
return d
return None
def get_sch_meal(gyc_code, sch_code, meal_date): # 식단 정보를 가져옴
url = 'https://open.neis.go.kr/hub/mealServiceDietInfo'
params = {
'Key': NEIS_KEY,
'Type': 'json',
'pIndex': 1,
'pSize': 8,
'ATPT_OFCDC_SC_CODE': gyc_code,
'SD_SCHUL_CODE': sch_code,
'MLSV_YMD': meal_date
}
r = requests.get(url, params).json()
if 'mealServiceDietInfo' in r:
meal = r[
'mealServiceDietInfo'][1]['row'][0]['DDISH_NM']
target = ('.', '\'', '`', ',', '*', '#', '-', '<div>', '</div>',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
for c in target:
meal = meal.replace(c, '')
meal = meal.replace('<br/>', '\n')
meal = [line for line in meal.split('\n') if line.strip() != '']
return meal
return None
def get_sch_timetable(gyc_code, sch_code, grade, classname, rdate):
url = 'https://open.neis.go.kr/hub/hisTimetable'
params = {
'Key': NEIS_KEY,
'Type': 'json',
'pIndex': 1,
'pSize': 8,
'ATPT_OFCDC_SC_CODE': gyc_code,
'SD_SCHUL_CODE': sch_code,
'GRADE': grade,
'CLASS_NM': classname,
'ALL_TI_YMD': rdate
}
r = requests.get(url, params).json()
if 'hisTimetable' in r:
timetable = list()
for perio in r['hisTimetable'][1]['row']:
timetable.append(perio['ITRT_CNTNT'])
return timetable
return None
def get_sch_classinfo(gyc_code, sch_code, grade):
url = 'https://open.neis.go.kr/hub/classInfo'
params = {
'Key': NEIS_KEY,
'Type': 'json',
'pIndex': 1,
'ATPT_OFCDC_SC_CODE': gyc_code,
'SD_SCHUL_CODE': sch_code,
'GRADE': grade,
}
r = requests.get(url, params).json()
if 'classInfo' in r:
classset = set()
for cl in r['classInfo'][1]['row']:
classset.add(int(cl['CLASS_NM']))
return sorted(classset)
return None
# def get_iui_weather():
# url = "http://apis.data.go.kr/1360000/VilageFcstInfoService/getUltraSrtNcst"
# sch = get_sch_info('이의고등학교')['이의고등학교']
# print(get_sch_meal(sch['gyc_code'], sch['sch_code'], '20210105'))
# print(get_sch_timetable(sch['gyc_code'],
# sch['sch_code'], '2', '11', '20210308'))
# print(get_sch_classinfo(sch['gyc_code'], sch['sch_code'], '2'))