forked from amolsharma99/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
128 lines (107 loc) · 3.41 KB
/
simulator.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
from flask import Flask, request, Response , jsonify
from pprint import pprint
import json
app = Flask(__name__)
#Models
class Product:
def __init__(self,productId,price,url):
self.productId = productId
self.price = price
self.url = url
self.ALWAYS = [] # sets can be used here
self.ALL_TIME_LOW = [] # sets can be used here
self.MORE_THAN_10 = [] # sets can be used here
self.MINIMUM = price
def getListforReson(self,users,r):
reason = ['ALWAYS','MORE_THAN_10','ALL_TIME_LOW']
notifications = []
for d in users:
notification = Notification(self.productId,self.price,self.url,reason[r])
notifications.append(notification)
return notifications
def getNotificationsList(self,nPrice):
always_notify = []
all_time_low_notify = []
more_then_10_notify = []
notifications = []
if self.price > nPrice:
always_notify = set(self.ALWAYS)
if 0.90 * self.price > nPrice:
more_then_10_notify = set(self.MORE_THAN_10)
if nPrice < self.MINIMUM:
all_time_low_notify = set(self.ALL_TIME_LOW)
always_notify = set(always_notify) - set(more_then_10_notify) - set(all_time_low_notify)
more_then_10_notify = set(more_then_10_notify) - set(all_time_low_notify)
self.price = nPrice
notifications.extend(self.getListforReson(always_notify,0))
notifications.extend(self.getListforReson(more_then_10_notify,1))
notifications.extend(self.getListforReson(all_time_low_notify,2))
return notifications
class Notification:
def __init__(self,productId,price,url,reason):
self.productId = productId
self.price = price
self.url = url
self.reason = reason
#Controller
products = {}
def updateProduct(product):
notifications = {}
if product.productId in products.keys():
oldproduct = products[product.productId]
notifications = oldproduct.getNotificationsList(product.price)
else:
products[product.productId] = product
return notifications
def subscribeUser(userJson):
user_id = int(userJson.get('user_id'))
subscribe = userJson.get('subscribe')
for data in subscribe:
productId = int(data.get('product_id'))
when = data.get('when')
if when == "ALWAYS":
products[productId].ALWAYS.append(user_id)
elif when == "ALL_TIME_LOW":
products[productId].ALL_TIME_LOW.append(user_id)
else:
products[productId].MORE_THAN_10.append(user_id)
def unsubscribeUser(userJson):
user_id = int(userJson.get('user_id'))
unsubscribe = userJson.get('unsubscribe')
for data in unsubscribe:
productId = int(data.get('product_id'))
try:
products[productId].ALWAYS.remove(user_id)
products[productId].ALL_TIME_LOW.remove(user_id)
products[productId].MORE_THAN_10.remove(user_id)
except ValueError:
pass
#API
@app.route("/subscribe", methods=['POST'])
def subscribe():
if request.json:
subscribeUser(request.json)
return jsonify(**request.json)
else:
return ""
@app.route("/unsubscribe", methods=['POST'])
def unsubscribe():
if request.json:
unsubscribeUser(request.json)
return jsonify(**request.json)
else:
return ""
@app.route("/priceDataPoint", methods=['POST'])
def priceDataPoint():
if request.json:
productId = int(request.json.get('product_id'))
price = float(request.json.get('price'))
url = request.json.get('url')
product = Product(productId,price,url)
notifications = updateProduct(product)
return json.dumps([notification.__dict__ for notification in notifications])
else:
return ""
if __name__ == "__main__":
app.debug = True
app.run()