koenverhagen commited on
Commit
324c978
1 Parent(s): c11ff11

Update createlookalike.py

Browse files
Files changed (1) hide show
  1. createlookalike.py +25 -16
createlookalike.py CHANGED
@@ -1,4 +1,3 @@
1
- import asyncio
2
  import tempfile as tfile
3
  from datetime import datetime
4
  from urllib.request import urlopen
@@ -19,10 +18,6 @@ from sklearn.decomposition import PCA
19
  from scipy.spatial import distance
20
  from collections import OrderedDict
21
 
22
- # from remove import remove_files
23
- #
24
- # from generate_csv_file import generate_csv_files
25
- # from load_data import load_data, get_shops
26
  from consts import API_KEY
27
  from schemas import Shop
28
 
@@ -144,35 +139,49 @@ async def calculate_shop(shop: Shop, feat_extractor) -> None:
144
  }
145
 
146
  # print(design_dict)
147
- if await push_home(design_dict, im, shop):
148
  pass
149
  else:
150
  print("error sending recommendations")
151
 
152
-
153
- # idx_closest = sorted(range(len(similar_idx)), key=lambda k: similar_idx[k])
154
-
155
- # design_json.update(design_dict)
156
- # print(idx_closest)
157
 
158
  except Exception as e:
159
  print("could not create json with look-a-like for shop:", shop.id, e)
160
 
161
  print(f"calculation for {shop.id} ended at {datetime.now()}")
162
 
163
- # return {'shop_id': shop.id, 'start_time': start, 'end_time': end, 'designs': design_json}
164
 
165
 
166
- async def push_home(design_dict, im, shop):
167
  headers: dict[str, str] = {
168
  "Authorization": API_KEY,
169
  "Content-type": "application/json",
170
  }
171
-
172
- print(headers)
173
  try:
 
 
 
 
174
 
175
- response = requests.post(shop.webhook_url, json=design_dict, headers=headers)
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  response.raise_for_status()
177
  return True
178
 
 
 
1
  import tempfile as tfile
2
  from datetime import datetime
3
  from urllib.request import urlopen
 
18
  from scipy.spatial import distance
19
  from collections import OrderedDict
20
 
 
 
 
 
21
  from consts import API_KEY
22
  from schemas import Shop
23
 
 
139
  }
140
 
141
  # print(design_dict)
142
+ if await push_home(design_dict, shop):
143
  pass
144
  else:
145
  print("error sending recommendations")
146
 
147
+ # if calculation is ready send update home
148
+ if await update_calculation_date(shop):
149
+ pass
150
+ else:
151
+ print("error sending shop calculation update")
152
 
153
  except Exception as e:
154
  print("could not create json with look-a-like for shop:", shop.id, e)
155
 
156
  print(f"calculation for {shop.id} ended at {datetime.now()}")
157
 
 
158
 
159
 
160
+ async def push_home(design_dict, shop):
161
  headers: dict[str, str] = {
162
  "Authorization": API_KEY,
163
  "Content-type": "application/json",
164
  }
 
 
165
  try:
166
+ url=f"{shop.webhook_url}/fill_recommendations
167
+ response = requests.post(url, json=design_dict, headers=headers)
168
+ response.raise_for_status()
169
+ return True
170
 
171
+ except Exception as e:
172
+ print(e)
173
+ return False
174
+
175
+
176
+ async def update_calculation_date(shop):
177
+ headers: dict[str, str] = {
178
+ "Authorization": API_KEY,
179
+ "Content-type": "application/json",
180
+ }
181
+ try:
182
+ url = f"{shop.webhook_url}/shop_updated"
183
+ data = {"shop_id": shop.id}
184
+ response = requests.post(url, json=data, headers=headers)
185
  response.raise_for_status()
186
  return True
187