-
Notifications
You must be signed in to change notification settings - Fork 0
/
real_time_test.py
333 lines (274 loc) · 13.3 KB
/
real_time_test.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from urllib.request import Request, urlopen
from urllib.parse import urlencode, quote_plus
from pymongo import MongoClient
import xmltodict, json
from datetime import date, timedelta
from pyfcm import FCMNotification
client = MongoClient("localhost", 27017)
localWeekData = client['localWeekData']
APIKEY = "AAAAsLhtlp8:APA91bF7A7f_Rr0tBhpZmgBAzZ5wAIGifzAyMtLJgerJyR-uX6t6bdpjyZ6dNE1IDOk22wm3vItH9xx4mZwzzI_4lcbGBJ-ZBi8kHh9Cxwf_2Kb14SUAnDPkCFPZhHGEP-pdYEeucLfS"
# 파이어베이스 콘솔에서 얻어 온 서버 키를 넣어 줌
push_service = FCMNotification(APIKEY)
cluster = MongoClient("mongodb://test1:KQ2DmS5BaPpKIBxL@cluster0-shard-00-00.6v20o.mongodb.net:27017,cluster0-shard-00-01.6v20o.mongodb.net:27017,cluster0-shard-00-02.6v20o.mongodb.net:27017/three-step?ssl=true&replicaSet=atlas-1s13em-shard-0&authSource=admin&retryWrites=true&w=majority")
db = cluster["three-step"]
jsonObj = {}
url = 'http://www.localdata.go.kr/platform/rest/TO0/openDataApi'
today_form = date.today()
day1_form = date.today() - timedelta(1)
day2_form = date.today() - timedelta(2)
day3_form = date.today() - timedelta(3)
day4_form = date.today() - timedelta(4)
day5_form = date.today() - timedelta(5)
day6_form = date.today() - timedelta(6)
day7_form = date.today() - timedelta(7)
today = today_form.strftime('%Y%m%d')
day1 = day1_form.strftime('%Y%m%d')
day2 = day2_form.strftime('%Y%m%d')
day3 = day3_form.strftime('%Y%m%d')
day4 = day4_form.strftime('%Y%m%d')
day5 = day5_form.strftime('%Y%m%d')
day6 = day6_form.strftime('%Y%m%d')
day7 = day7_form.strftime('%Y%m%d')
def sendMessage(body, title):
# 메시지 (data 타입)
data_message = {
"body": body,
"title": title
}
# topic을 이용해 다수의 구독자에게 푸시알림을 전송함
result = push_service.notify_topic_subscribers(topic_name="generalTopic", data_message=data_message)
# 전송 결과 출력
print(result)
def close(today_str, day1_str, day2_str, day3_str, day4_str, day5_str, day6_str, day7_str):
queryParams = '?' + urlencode({
quote_plus("authKey"): "uZ98I03VR/zZ3zAvdNIVEl8EWJy6llSwLtDAMDlOYkE=",
quote_plus("lastModTsBgn"): day1_str,
quote_plus("lastModTsEnd"): today_str,
quote_plus("state"): "03",
quote_plus("pageSize"): "10000",
quote_plus("resultType"): "xml"
})
request = Request(url + queryParams)
request.get_method = lambda: 'GET'
response_body = urlopen(request).read()
dict = xmltodict.parse(response_body)
jsonString = json.dumps(dict['result']['body'], ensure_ascii=False)
jsonObj = json.loads(jsonString)
local_real_time_close = localWeekData['test_close']
if jsonObj['rows']:
cnt = 0
for item in jsonObj['rows']['row']:
if item['dcbYmd'] == day1_str or item['dcbYmd'] == day2_str or item['dcbYmd'] == day3_str or item['dcbYmd'] == day4_str or item['dcbYmd'] == day5_str or item['dcbYmd'] == day6_str or item['dcbYmd'] == day7_str:
is_exist = local_real_time_close.find_one(
{"$and": [{"mgtNo": item['mgtNo']}, {"store_name": item['bplcNm']}]})
if is_exist is None:
cnt += 1
city_name = ""
category_name = ""
category_kor = ""
city_split = ""
if item['rdnWhlAddr']:
city_split = item['rdnWhlAddr'].split(' ')[0]
address = item['rdnWhlAddr']
else:
city_split = item['siteWhlAddr'].split(' ')[0]
address = item['siteWhlAddr']
if city_split == "부산광역시":
city_name = "busan"
elif city_split == "충청북도":
city_name = "chungbuk"
elif city_split == "충청남도":
city_name = "chungnam"
elif city_split == "대구광역시":
city_name = "daegu"
elif city_split == "대전광역시":
city_name = "daejeon"
elif city_split == "강원도":
city_name = "gangwon"
elif city_split == "광주광역시":
city_name = "gwangju"
elif city_split == "경기도":
city_name = "gyeonggi"
elif city_split == "경상북도":
city_name = "gyeongbuk"
elif city_split == "경상남도":
city_name = "gyeongnam"
elif city_split == "인천광역시":
city_name = "incheon"
elif city_split == "제주특별자치도":
city_name = "jeju"
elif city_split == "전라북도":
city_name = "jeonbuk"
elif city_split == "전라남도":
city_name = "jeonnam"
elif city_split == "세종특별자치시":
city_name = "sejong"
elif city_split == "서울특별시":
city_name = "seoul"
elif city_split == "울산광역시":
city_name = "ulsan"
open_service_id_split = item['opnSvcId'][0:2]
if open_service_id_split == "01":
category_name = "health"
category_kor = "건강"
elif open_service_id_split == "02":
category_name = "animal"
category_kor = "동물"
elif open_service_id_split == "03" or open_service_id_split == "04":
category_name = "culture"
category_kor = "문화"
elif open_service_id_split == "05" or open_service_id_split == "06" or open_service_id_split == "08" or open_service_id_split == "10":
category_name = "life"
category_kor = "생활"
elif open_service_id_split == "07":
category_name = "food"
category_kor = "식품"
elif open_service_id_split == "09":
category_name = "environment"
category_kor = "환경"
elif open_service_id_split == "11":
category_name = "other"
category_kor = "기타"
info = {
"mgtNo": item['mgtNo'],
"data": "close",
"update_date": item['updateDt'],
"authorization_date": item['apvPermYmd'],
"closed_date": item['dcbYmd'],
"store_name": item['bplcNm'],
"address": address,
"state_code": item['trdStateGbn'],
"state": item['trdStateNm'],
"open_service_id": item['opnSvcId'],
"open_service": item['opnSvcNm'],
"detailed_classification": item['uptaeNm'],
"city_name": city_name,
"category_name": category_name,
"category_kor": category_kor,
}
print(cnt, "close", info)
# local_real_time_close = localWeekData['test_close']
# local_real_time_close_id = local_real_time_close.insert_one(info).inserted_id
return cnt
def today_open(day1_str, day7_str):
queryParams = '?' + urlencode({
quote_plus("authKey"): "uZ98I03VR/zZ3zAvdNIVEl8EWJy6llSwLtDAMDlOYkE=",
quote_plus("bgnYmd"): day7_str,
quote_plus("endYmd"): day1_str,
quote_plus("state"): "01",
quote_plus("pageSize"): "10000",
quote_plus("resultType"): "xml"
})
request = Request(url + queryParams)
request.get_method = lambda: 'GET'
response_body = urlopen(request).read()
dict = xmltodict.parse(response_body)
jsonPaging = json.dumps(dict['result']['header']['paging'], ensure_ascii=False)
jsonPageObj = json.loads(jsonPaging)
jsonString = json.dumps(dict['result']['body'], ensure_ascii=False)
jsonObj = json.loads(jsonString)
local_real_time_open = localWeekData['test_open']
if jsonObj['rows']:
cnt = 0
for item in jsonObj['rows']['row']:
isOne = False
if jsonPageObj['totalCount'] == '1':
isOne = True
item = jsonObj['rows']['row']
is_exist = local_real_time_open.find_one({"$and": [{"mgtNo": item['mgtNo']}, {"store_name": item['bplcNm']}]})
if is_exist is None:
cnt += 1
city_name = ""
category_name = ""
category_kor = ""
city_split = ""
if item['rdnWhlAddr']:
city_split = item['rdnWhlAddr'].split(' ')[0]
address = item['rdnWhlAddr']
else:
city_split = item['siteWhlAddr'].split(' ')[0]
address = item['siteWhlAddr']
if city_split == "부산광역시":
city_name = "busan"
elif city_split == "충청북도":
city_name = "chungbuk"
elif city_split == "충청남도":
city_name = "chungnam"
elif city_split == "대구광역시":
city_name = "daegu"
elif city_split == "대전광역시":
city_name = "daejeon"
elif city_split == "강원도":
city_name = "gangwon"
elif city_split == "광주광역시":
city_name = "gwangju"
elif city_split == "경기도":
city_name = "gyeonggi"
elif city_split == "경상북도":
city_name = "gyeongbuk"
elif city_split == "경상남도":
city_name = "gyeongnam"
elif city_split == "인천광역시":
city_name = "incheon"
elif city_split == "제주특별자치도":
city_name = "jeju"
elif city_split == "전라북도":
city_name = "jeonbuk"
elif city_split == "전라남도":
city_name = "jeonnam"
elif city_split == "세종특별자치시":
city_name = "sejong"
elif city_split == "서울특별시":
city_name = "seoul"
elif city_split == "울산광역시":
city_name = "ulsan"
open_service_id_split = item['opnSvcId'][0:2]
if open_service_id_split == "01":
category_name = "health"
category_kor = "건강"
elif open_service_id_split == "02":
category_name = "animal"
category_kor = "동물"
elif open_service_id_split == "03" or open_service_id_split == "04":
category_name = "culture"
category_kor = "문화"
elif open_service_id_split == "05" or open_service_id_split == "06" or open_service_id_split == "08" or open_service_id_split == "10":
category_name = "life"
category_kor = "생활"
elif open_service_id_split == "07":
category_name = "food"
category_kor = "식품"
elif open_service_id_split == "09":
category_name = "environment"
category_kor = "환경"
elif open_service_id_split == "11":
category_name = "other"
category_kor = "기타"
info = {
"mgtNo": item['mgtNo'],
"data": "open",
"update_date": item['updateDt'],
"authorization_date": item['apvPermYmd'],
"closed_date": item['dcbYmd'],
"store_name": item['bplcNm'],
"address": address,
"state_code": item['trdStateGbn'],
"state": item['trdStateNm'],
"open_service_id": item['opnSvcId'],
"open_service": item['opnSvcNm'],
"detailed_classification": item['uptaeNm'],
"city_name": city_name,
"category_name": category_name,
"category_kor": category_kor,
}
print(cnt, "open", info)
# local_real_time_open = localWeekData['test_open']
# local_real_time_open_id = local_real_time_open.insert_one(info).inserted_id
if isOne:
break
return cnt
close_cnt = close(today, day1, day2, day3, day4, day5, day6, day7)
open_cnt = today_open(day1, day7)
# my_str = "개업: " + str(close_cnt) + "개, 폐업: " + str(open_cnt) + "개 데이터가 업데이트 되었습니다."
# print(my_str)
# sendMessage(my_str, "최근 7일 데이터 업데이트 알림")