-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrator_api.py
executable file
·238 lines (202 loc) · 9.3 KB
/
integrator_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
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
#!/usr/bin/python
from simplexml import loads
from flask import make_response, Flask, render_template, Blueprint
from flask import Flask
from datetime import datetime
import json
import requests
import re
from flask_restful import Resource, Api
from flask_restful import reqparse
from xml.etree import ElementTree
from flask.ext.mysql import MySQL
from time import sleep
from flask_bootstrap import Bootstrap
app = Flask(__name__)
api = Api(app, default_mediatype='application/json')
Bootstrap(app)
@app.route('/')
def client():
items = integrate_items()['items']
return render_template('base.html', data=items)
@api.resource('/notification')
class Notification(Resource):
def get(self):
return integrate_items()['items']
def post(self):
def argument_add(argument, parser):
parser.add_argument(argument, type=str)
arg = parser.parse_args()
return arg[argument]
def define_filters():
filters = []
filters.append(lambda n: re.match(_id, n['id'], re.IGNORECASE)) if _id else None
filters.append(lambda n: re.match(_name, n['name'], re.IGNORECASE)) if _name else None
filters.append(lambda n: datetime.strptime(
_after, '%Y-%m-%dT%H:%M:%S') < datetime.strptime(
n['date'], '%Y-%m-%d %H:%M:%S')) if _after else None
filters.append(lambda n: datetime.strptime(
_before, '%Y-%m-%dT%H:%M:%S') > datetime.strptime(
n['date'], '%Y-%m-%d %H:%M:%S')) if _before else None
filters.append(lambda n: re.match(_last_name, n['last_name'], re.IGNORECASE)) if _last_name else None
filters.append(lambda n: re.match(_phone_prefix, str(n['phone_prefix'], re.IGNORECASE))) if _phone_prefix else None
filters.append(lambda n: re.match(_phone_number, str(n['phone_number'], re.IGNORECASE))) if _phone_number else None
filters.append(lambda n: re.match(_street_number, str(n['street_number'], re.IGNORECASE))) if _street_number else None
filters.append(lambda n: re.match(_street_name, n['street_name'], re.IGNORECASE)) if _street_name else None
filters.append(lambda n: re.match(_city, n['city'], re.IGNORECASE)) if _city else None
filters.append(lambda n: re.match(_description, n['description'], re.IGNORECASE)) if _description else None
return filters
parser = reqparse.RequestParser()
_id = argument_add('id', parser)
_date = argument_add('date', parser)
_after = argument_add('after', parser)
if _after:
try:
_after = parse_time(_after)
datetime.strptime(_after, '%Y-%m-%dT%H:%M:%S')
except ValueError:
_after = None
_before = argument_add('before', parser)
if _before:
try:
_before = parse_time(_before)
datetime.strptime(_before, '%Y-%m-%dT%H:%M:%S')
except ValueError:
_before = None
_name = argument_add('name', parser)
_last_name = argument_add('last_name', parser)
_phone_prefix = argument_add('phone_prefix', parser)
_phone_number = argument_add('phone_number', parser)
_street_number = argument_add('street_number', parser)
_street_name = argument_add('street_name', parser)
_city = argument_add('city', parser)
_description = argument_add('description', parser)
items_list = integrate_items()
filters = define_filters()
filtered_list = filter(lambda x: all(f(x) for f in filters), items_list['items'])
return filtered_list
@api.resource('/notification/id/<string:url_id>')
class NotificationID(Resource):
def get(self, url_id):
return [
x
for x in integrate_items()['items']
if re.match(url_id, x['id'], re.IGNORECASE)]
@api.resource('/notification/date/<string:url_date>')
class NotificationDATE(Resource):
def get(self, url_date):
# datetime.strptime(x['date'], '%Y-%m-%d %H:%M:%S')
try:
operator = re.findall('[><]?', url_date)[0][0]
except IndexError:
operator = '='
try:
date = re.findall('^[><]?([\d:-T]*)', url_date)[0][1]
date_formatted = parse_time(url_date)
datetime.strptime(date_formatted, '%Y-%m-%dT%H:%M:%S')
except (ValueError, IndexError):
return {'error': 'Invalid data format. Use %Y-%m-%dT%H:%M:%S',
'hint': 'It is allowed to provide incomplete data (%Y, %Y-%m, etc)'}
if operator == '>':
return [x for x in integrate_items()['items'] if x['date'] > date_formatted]
elif operator == '<':
return [x for x in integrate_items()['items'] if x['date'] < date_formatted]
if operator == '=':
return [x for x in integrate_items()['items'] if x['date'] == date_formatted]
@api.resource('/notification/street/<string:url_street>')
class NotificationSTREET(Resource):
def get(self, url_street):
return [
x
for x in integrate_items()['items']
if re.match(url_street, x['street_name'], re.IGNORECASE)]
@api.resource('/notification/city/<string:url_city>')
class NotificationCITY(Resource):
def get(self, url_city):
return [
x
for x in integrate_items()['items']
if re.match(url_city, x['city'], re.IGNORECASE)]
@api.resource('/notification/description/<string:url_description>')
class NotificationDESCRIPTION(Resource):
def get(self, url_description):
return [
x
for x in integrate_items()['items']
if re.match(url_description, x['description'], re.IGNORECASE)]
@api.resource('/caller/name/<string:url_name>')
class CallerNAME(Resource):
def get(self, url_name):
return [
x
for x in integrate_items()['items']
if re.match(url_name, x['name'], re.IGNORECASE)]
@api.resource('/caller/last_name/<string:url_last_name>')
class CallerLASTNAME(Resource):
def get(self, url_last_name):
return [
x
for x in integrate_items()['items']
if re.match(url_last_name, x['last_name'], re.IGNORECASE)]
@api.resource('/caller/phone_prefix/<string:url_phone_prefix>')
class CallerPHONEPREFIX(Resource):
def get(self, url_phone_prefix):
return [
x
for x in integrate_items()['items']
if re.match(url_phone_prefix, str(x['phone_prefix'], re.IGNORECASE))]
@api.resource('/caller/phone_number/<string:url_phone_number>')
class CallerPHONENUMBER(Resource):
def get(self, url_phone_number):
return [
x
for x in integrate_items()['items']
if re.match(url_phone_number, str(x['phone_number'], re.IGNORECASE))]
def integrate_items():
provider_1_request = requests.get('http://localhost:1111/notification')
provider_2_request = requests.get('http://localhost:2222/notification')
provider_1_items = loads(json.loads(provider_1_request.text))['response']
provider_1_integrated = [{
'id': x['id'],
'date': x['date_time'],
'name': x['name'].capitalize(),
'last_name': x['last_name'].capitalize(),
'phone_prefix': int(re.findall('\((\d+)\)', x['phone'])[0]),
'phone_number': int("".join(re.findall('(\d+)-(\d+)', x['phone'])[0])),
'street_number': int(re.findall('(\d+) (.+)', x['address'])[0][0]),
'street_name': re.findall('(\d+) (.+)', x['address'])[0][1],
'city': x['city'],
'description': x['additional_information'].capitalize()
} for x in provider_1_items['items'] ]
provider_2_items = json.loads(provider_2_request.text)
provider_2_integrated = [{
'id': x['id'],
'date': x['date'],
'name': x['name'].split()[0],
'last_name': x['name'].split()[1].capitalize(),
'phone_prefix': x['phone_prefix'],
'phone_number': x['phone_number'],
'street_number': x['street_number'],
'street_name': x['street_name'],
'city': x['city'],
'description': x['description'].capitalize()
} for x in provider_2_items['items']]
items_list = sorted(provider_2_integrated + provider_1_integrated,
key=lambda x: x['date'])
return {'items': items_list, 'items_count': len(items_list)}
def parse_time(date):
datetime_list = re.findall('\d+', date)
if datetime_list == []:
return 'invalid', 'datetime'
year = datetime_list[0] if len(datetime_list) > 0 else '1900'
month = datetime_list[1] if len(datetime_list) > 1 else '01'
day = datetime_list[2] if len(datetime_list) > 2 else '01'
hour = datetime_list[3] if len(datetime_list) > 3 else '00'
minute = datetime_list[4] if len(datetime_list) > 4 else '00'
second = datetime_list[5] if len(datetime_list) > 5 else '00'
date = '%0.4d-%0.2d-%0.2d' % (int(year), int(month), int(day))
time = '%0.2d:%0.2d:%0.2d' % (int(hour), int(minute), int(second))
# return datetime.strptime(date + 'T' + time, '%Y-%m-%dT%H:%M:%S')
return date + 'T' + time
if __name__ == '__main__':
app.run(debug=True, host='localhost', port=3333)