forked from gist-helper/meals_data-parsing_hyoinjeong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.py
182 lines (156 loc) · 7.77 KB
/
parsing.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import sys
import os
from os.path import exists
import requests
import json
import pandas as pd
from meal import Meal, INDEX_ENDPOINTS_BLDG1_EXCEPT
from meal import KOR, ENG
from meal import BREAKFAST, LUNCH, DINNER
from meal import BLDG1_1ST, BLDG1_2ND, BLDG2_1ST
from meal import MEAL_KIND
from meal import BLDG_KIND
from meal import EXCEL_COL_BLDG, INDEX_ENDPOINTS, DATE_INDEX
from util import ComplexEncoder, sanitize_menu
from util import parsing_date, parsing_meal
def parsing(excel_path: str, bldgType: int, langType: int) -> list:
col = EXCEL_COL_BLDG [bldgType]
endpoint = INDEX_ENDPOINTS[bldgType]
bldg = BLDG_KIND [langType][bldgType]
time_index = DATE_INDEX [bldgType]
start_col_idx = col[0]
end_col_idx = col[1]
parsing_result = []
# 1학 1층 한글
if bldgType == BLDG1_1ST and langType == 0:
xl = pd.read_excel(excel_path, sheet_name=None,
index_col=None, header=None, names=None, engine='openpyxl')
# 맨 뒤에서 2번째 sheet가 이번주 sheet
last_sheet = list(xl.keys())[-1]
sheet = xl[last_sheet]
# 짝수 column만 사용
sheet_keys = sheet.keys()[start_col_idx : end_col_idx]
# sheet_keys = sheet_keys[0::2]
for (dateType, date_str) in enumerate(sheet_keys):
date_sheet = sheet[date_str]
date = parsing_date(time_index, date_sheet)
for kindType in [BREAKFAST, LUNCH, DINNER]:
kind = MEAL_KIND[langType][kindType]
menu, special = parsing_meal[kindType](endpoint, date_sheet, langType, dateType, bldgType)
meal = Meal(bldgType, langType, dateType, kindType,
bldg, date, kind, menu, special)
parsing_result.append(meal.__dict__)
return parsing_result
# 1학 1층 영어
elif bldgType == BLDG1_1ST and langType == 1:
xl = pd.read_excel(excel_path, sheet_name=None,
index_col=None, header=None, names=None, engine='openpyxl')
# 맨 뒤에서 2번째 sheet가 이번주 sheet
last_sheet = list(xl.keys())[-1]
sheet = xl[last_sheet]
# 짝수 column만 사용
sheet_keys = sheet.keys()[start_col_idx: end_col_idx]
# sheet_keys = sheet_keys[0::2]
for (dateType, date_str) in enumerate(sheet_keys):
date_sheet = sheet[date_str]
date = parsing_date(time_index, date_sheet)
for kindType in [BREAKFAST, LUNCH, DINNER]:
kind = MEAL_KIND[langType][kindType]
menu, special = parsing_meal[kindType](endpoint, date_sheet, langType, dateType, bldgType)
meal = Meal(bldgType, langType, dateType, kindType,
bldg, date, kind, menu, special)
parsing_result.append(meal.__dict__)
return parsing_result
# 1학 2층
elif bldgType == BLDG1_2ND:
sheet = pd.read_excel(excel_path, sheet_name=0,
index_col=None, header=None, names=None, engine='openpyxl')
for (dateType, date_str) in enumerate(sheet.keys()[start_col_idx : end_col_idx]):
date_sheet = sheet[date_str]
date = parsing_date(time_index, date_sheet)
for kindType in [LUNCH]:
kind = MEAL_KIND[langType][kindType]
menu, special = parsing_meal[kindType](endpoint, date_sheet, langType, dateType, bldgType, INDEX_ENDPOINTS_BLDG1_EXCEPT)
meal = Meal(bldgType, langType, dateType, kindType,
bldg, date, kind, menu, special)
parsing_result.append(meal.__dict__)
return parsing_result
# 2학
elif bldgType == BLDG2_1ST:
sheet = pd.read_excel(excel_path, sheet_name=langType,
index_col=None, header=None, names=None, engine='openpyxl')
for (dateType, date_str) in enumerate(sheet.keys()[start_col_idx : end_col_idx]):
date_sheet = sheet[date_str]
date = parsing_date(time_index, date_sheet)
for kindType in [BREAKFAST, LUNCH, DINNER]:
kind = MEAL_KIND[langType][kindType]
menu, special = parsing_meal[kindType](endpoint, date_sheet, langType, dateType, bldgType)
meal = Meal(bldgType, langType, dateType, kindType,
bldg, date, kind, menu, special)
parsing_result.append(meal.__dict__)
return parsing_result
def parsingTest():
excel_dir_path = "./excel"
parsing_result = []
print("-------------------------------------------------")
print("parsing xlsx to json...")
for excel_filename in os.listdir(excel_dir_path):
excel_path = os.path.join(excel_dir_path, excel_filename)
parsing_result.extend(parsing(excel_path, BLDG2_1ST, KOR)) #2학 1층, 한글
parsing_result.extend(parsing(excel_path, BLDG2_1ST, ENG)) #2학 1층, 영어
print("-------------------------------------------------")
print("saving json...")
jsonFile = open("./2nd1floor_meal.json", "w", encoding="utf-8")
json.dump(parsing_result, jsonFile, indent=4,
ensure_ascii=False, cls=ComplexEncoder)
print("-------------------------------------------------")
if __name__ == "__main__":
Mode = 0
with open('config.json') as f:
config = json.load(f)
if Mode == 0:
filepath_bldg1_1_kor = config['filepath_bldg1_1_kor']
filepath_bldg1_1_eng = config['filepath_bldg1_1_eng']
filepath_bldg1_2_kor = config['filepath_bldg1_2_kor']
filepath_bldg1_2_eng = config['filepath_bldg1_2_eng']
filepath_bldg2 = config['filepath_bldg2']
parsing_result = []
print("-------------------------------------------------")
print("parsing xlsx to json...")
if exists(filepath_bldg1_1_kor):
parsing_result.extend(parsing(filepath_bldg1_1_kor, BLDG1_1ST, KOR)) #1학 1층, 한글
if exists(filepath_bldg1_1_eng):
parsing_result.extend(parsing(filepath_bldg1_1_eng, BLDG1_1ST, ENG)) #1학 1층, 영어
if exists(filepath_bldg1_2_kor):
parsing_result.extend(parsing(filepath_bldg1_2_kor, BLDG1_2ND, KOR)) #1학 2층, 한글
if exists(filepath_bldg1_2_eng):
parsing_result.extend(parsing(filepath_bldg1_2_eng, BLDG1_2ND, ENG)) #1학 2층, 영어
if exists(filepath_bldg2):
parsing_result.extend(parsing(filepath_bldg2, BLDG2_1ST, KOR)) #2학 1층, 한글
# parsing_result.extend(parsing(filepath_bldg2, BLDG2_1ST, ENG)) #2학 1층, 영어
print("-------------------------------------------------")
if len(sys.argv) == 1:
# no url, save to local as json
print("-------------------------------------------------")
print("saving json...")
jsonFile = open("./meal.json", "w", encoding="utf-8")
json.dump(parsing_result, jsonFile, indent=4,
ensure_ascii=False, cls=ComplexEncoder)
print("-------------------------------------------------")
elif len(sys.argv) == 2:
# post to server
url = sys.argv[1]
print("-------------------------------------------------")
print("send to server...")
if url[0:4] != "http":
url = "http://localhost:8080/meals/test"
requests.post(url, data={"testStr": "Hello World!"})
else:
for meal_result in parsing_result:
print(meal_result)
response = requests.post(url, json=meal_result)
print(response)
print()
print("-------------------------------------------------")
elif Mode == 1:
parsingTest()