-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkubic_class.py
More file actions
171 lines (146 loc) · 8.46 KB
/
kubic_class.py
File metadata and controls
171 lines (146 loc) · 8.46 KB
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
from kubic_user import *
from kubic_data import *
from flask import request
class kubic_api:
def __init__(self, searchType):
self.searchType = searchType
self.resultCode = 200
self.response = self.setRequest()
if self.response == 200:
self.response = self.setResponse()
def raiseError(self, resultCode, resultMSG):
self.resultCode = resultCode
self.response = {
"header":{
"resultCode": resultCode,
"resultMSG": resultMSG,
},
"body": {},
}
return self.response
def setRequest(self):
if self.searchType == 'simple_search':
keyList = ['serviceKey', 'numOfCnt', 'rank','keyword']
elif self.searchType == 'detailed_search' or self.searchType == 'my_doc':
keyList = ['serviceKey','numOfCnt','rank','keyword','keyInTitle','keyInBody','writer','startDate','endDate','institution','category' ]
elif self.searchType == 'retrieve_all':
keyList = ['serviceKey','numOfCnt','page']
else: return self.raiseError(502,'Bad Gateway')
for k in request.args.keys():
if not k in keyList:
return self.raiseError(400,'Bad Request: ' + k)
if not 'serviceKey' in request.args:
return self.raiseError(400,'Bad Request: No serviceKey')
# elif not 'keyword' in request.args and not 'keyInTitle' in request.args:
# return self.raiseError(400,'Bad Request: No keyInTitle')
if self.searchType == 'simple_search':
self.request = {
'serviceKey': request.args.get('serviceKey') ,
'numOfCnt': int(request.args.get('numOfCnt', 100)),
'rank': int(request.args.get('rank', 1)),
'keyword': request.args.get('keyword')
}
elif self.searchType == 'retrieve_all':
self.request = {
'serviceKey': request.args.get('serviceKey') ,
'numOfCnt': int(request.args.get('numOfCnt', 100)),
'page': int(request.args.get('page', 1))
}
else:
self.request = {
'serviceKey': request.args.get('serviceKey') ,
'numOfCnt': int(request.args.get('numOfCnt', 100)),
'rank': int(request.args.get('rank', 1)),
'keyInTitle': request.args.get('keyInTitle',""),
'keyInBody': request.args.get('keyInBody',""),
'writer': request.args.get('writer',""),
'startDate': request.args.get('startDate',(datetime.today()-relativedelta(years=1)).strftime("%Y-%m-%d")),
'endDate': request.args.get('endDate',datetime.today().strftime("%Y-%m-%d")),
'institution': request.args.get('institution',""),
'category': request.args.get('category',""),
}
return 200
def setResponse(self):
_id = verification(self.request['serviceKey'])
if not _id:
return self.raiseError(401,'Unauthorized')
apptype = getAppType(_id)
if not limitDate(_id):
return self.raiseError(401,'Expired')
if not limitTraffic(_id):
return self.raiseError(401,'Overused')
try: data = esSearch(self.searchType, self.request)
except Exception as e:
print("esSearch() exception:", e)
return self.raiseError(502,'Bad Gateway')
# print('origin data from ES:',data)
if data['hits']['total']['value']==0:
return self.raiseError(204,'No Content')
def slicing400(content):
try:
content_split = ' '.join(content.split())
if len(content_split) > 200:
content_split = content_split[:200]
return content_split
except:
return content
def editContentByApp(content, datatype):
# public user
if apptype == 'public':
if datatype == 'body':
return slicing400(content)
else: return None
# private user
# mode1
if esAcc.mode == 1:
return content
elif esAcc.mode == 2:
return slicing400(content)
else: return None
self.response = self.raiseError(200, 'OK')
if self.searchType == 'retrieve_all':
self.response['body'] = {
"numOfCnt": self.request['numOfCnt'],
"totalCnt": data['hits']['total']['value'],
"page": self.request['page'],
# "rank" : self.request['rank'],
"contents":[{
"title": content['_source']['post_title'],
"body": editContentByApp(content['_source']['post_body'],'body') if 'post_body' in content['_source'].keys() else None,
"writer": content['_source']['post_writer'] if 'post_writer' in content['_source'].keys() else None,
"date": content['_source']['post_date'] if 'post_date' in content['_source'] else None,
"originalURL": content['_source']['original_url'] if 'original_url' in content['_source'] else None,
"institution": content['_source']['published_institution'] if 'published_institution' in content['_source'].keys() else None,
"institutionURL": content['_source']['published_institution_url'] if 'published_institution_url' in content['_source'].keys() else None,
# "category": content['_source']['top_category'],
"fileURL": editContentByApp(content['_source']['file_download_url'],'fileURL') if 'file_download_url' in content['_source'].keys() else None,
"fileName": content['_source']['file_name'] if 'file_name' in content['_source'].keys() else None,
#content['_source']['file_download_url'],
"fileContent": editContentByApp(content['_source']['file_extracted_content'],'fileContent') if 'file_extracted_content' in content['_source'].keys() else None
}for content in data['hits']['hits']]
}
else:
self.response['body'] = {
"numOfCnt": self.request['numOfCnt'],
"totalCnt": data['hits']['total']['value'],
"rank" : self.request['rank'],
"contents":[{
"title": content['_source']['post_title'],
"body": editContentByApp(content['_source']['post_body'],'body') if 'post_body' in content['_source'].keys() else None,
"writer": content['_source']['post_writer'] if 'post_writer' in content['_source'] else None,
"date": content['_source']['post_date'] if 'post_date' in content['_source'] else None,
"originalURL": content['_source']['original_url'] if 'original_url' in content['_source'] else None,
"institution": content['_source']['published_institution'],
"institutionURL": content['_source']['published_institution_url'],
"category": content['_source']['top_category'],
"fileURL": editContentByApp(content['_source']['file_download_url'],'fileURL') if 'file_download_url' in content['_source'].keys() else None,
"fileName": content['_source']['file_name'] if 'file_name' in content['_source'].keys() else None,
"fileContent": editContentByApp(content['_source']['file_extracted_content'],'fileContent') if 'file_extracted_content' in content['_source'].keys() else None
#content['_source']['file_download_url'],
}for content in data['hits']['hits']]
}
for item in self.response['body']['contents']:
for key , item2 in list(item.items()): ## list , items를 꼭 써야함.
if item2 is None : del item[key]
raiseTraffic(_id, self.request['numOfCnt'] if self.request['numOfCnt'] < data['hits']['total']['value'] else data['hits']['total']['value'])
return self.response