-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEtsyShopManager.py
421 lines (390 loc) · 19.8 KB
/
EtsyShopManager.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import pprint
# from pydantic.fields import T
from termcolor import colored
from datetime import datetime
from typing import List, Optional
from pymongo import errors
from helpers import calculate_max_min_due_date
from bson import ObjectId
from AsyncEtsyApi import AsyncEtsy, Method, EtsyUrl
from MyLogger import Logger
logging = Logger().logging
from database import MongoDB, MyRedis, ReceiptNoteStatus
# import pytz
class MyEtsyShopManager:
def __init__(self, shop_name):
self.shop_name = shop_name
async def insert_receipt(self, receipt: dict, db):
receipt.update({"shop_name": self.shop_name})
receipt_insert_result = await db["Receipts"].insert_one(receipt)
return receipt_insert_result.inserted_id
async def insert_notes(self, inserted_receipt_ids):
if len(inserted_receipt_ids) == 0:
return
mongodb = MongoDB()
db = mongodb.db
notes = []
for receipt_id in inserted_receipt_ids:
# receipt = await db["Receipts"].find_one({"_id": receipt_mongodb_id})
note = {
"receipt_id": receipt_id,
"created_at": datetime.now(),
"status": ReceiptNoteStatus.notseen,
"assigned_to": None,
}
notes.append(note)
# pprint.pprint(note)
logging.debug(note)
try:
insert_all_notes_result = await db["Notes"].insert_many(notes, ordered=False)
except errors.BulkWriteError as e:
panic_list = list(
filter(lambda x: x["code"] == 11000, e.details["writeErrors"])
)
if len(panic_list) > 0:
logging.info(
f"{colored('...', 'red')} (insert_notes) There have been found {len(panic_list)} duplicates. {colored('...', 'red')}"
)
else:
logging.info(f"these are not duplicate errors {panic_list}")
pass
# for writeError in e.details['writeErrors']:
# logging.info(f"{writeError['keyValue']['receipt_id']} has already note associated with the given receipt_id in the Notes Collection.")
async def insert_receipts(self, receipts: List[dict], db):
logging.info(f"{len(receipts)} receipts will be added to MongoDB.")
if len(receipts) <= 0:
return []
for r in receipts:
r["shop_name"] = self.shop_name
receipt_ids = [str(receipt['receipt_id']) for receipt in receipts]
dup_ids = []
insert_all_receipts_result = None
logging.info(f"{colored(' ', 'magenta')} Inserting Receipt Notes for given receipt ids. {colored(' ', 'magenta')}")
await self.insert_notes([receipt['receipt_id'] for receipt in receipts])
try:
insert_all_receipts_result = await db["Receipts"].insert_many(
receipts, ordered=False
)
except errors.BulkWriteError as e:
# logging.error(f"Articles bulk write insertion error {e}")
panic_list = list(
filter(lambda x: x["code"] == 11000, e.details["writeErrors"])
)
dup_ids = list(str(error['keyValue']['receipt_id']) for error in filter(lambda x: x['code'] == 11000, e.details["writeErrors"]))
if len(panic_list) > 0:
logging.info(
f"{colored('...', 'red')} (insert_receipts) There have been found {len(panic_list)} duplicates. {colored('...', 'red')}"
)
else:
logging.info(f"these are not duplicate errors {panic_list}")
pass
# for writeError in e.details['writeErrors']:
# logging.info(f"{writeError['keyValue']['receipt_id']} is alreadya in the Receipts Collection.")
finally:
delta = set(receipt_ids) ^ set(dup_ids)
logging.info(
f"{colored('......', on_color='on_yellow')} ({self.shop_name}) Successfully inserted receipts => {','.join(delta)} {colored('.......', on_color='on_yellow')}"
)
# else:
# await self.insert_notes(insert_all_receipts_result.inserted_ids)
# finally:
# if insert_all_receipts_result is None:
# logging.info(
# f"{colored(' ', 'red')} inserted_all_receipts_result is NONE {colored(' ', 'red')}"
# )
# return []
# receipts_ids = [receipt["receipt_id"] for receipt in receipts]
# delta = set(insert_all_receipts_result.inserted_ids) ^ set(receipts_ids)
# logging.info(
# f"{colored(' ', 'magenta')} ({len(delta)}) Difference between given receipts and inserted receipts is {', '.join(delta)} {colored(' ', 'magenta')}"
# )
# logging.info(
# f"{colored(' ', 'magenta')} ({len(insert_all_receipts_result.inserted_ids)}) Inserted receipts are {', '.join([str(inserted_mongodb_id) for inserted_mongodb_id in insert_all_receipts_result.inserted_ids])} {colored(' ', 'magenta')}"
# )
# logging.info(f"{insert_all_receipts_result.inserted_ids}")
# return [
# (str(mongodb_id), receipt["receipt_id"])
# for mongodb_id, receipt in zip(
# insert_all_receipts_result.inserted_ids, receipts
# )
# ]
# return insert_all_receipts_result.inserted_ids
async def update_receipt(
self, mongodb_id: str, receipt_id: str, transactions: List[dict], db
):
if type(mongodb_id) is not ObjectId:
mongodb_id = ObjectId(mongodb_id)
update_receipt_result = await db["Receipts"].update_one(
{"_id": mongodb_id, "receipt_id": receipt_id},
{"$set": {"transactions": transactions}},
)
@staticmethod
async def check_unpaids(etsy_connection_id, asyncEtsyApi, params, r):
logging.info(f"Checking unpaid receipts {etsy_connection_id}")
my_params = dict(params)
my_params.pop("min_created", None)
receipts_not_paid = []
receipts_to_be_inserted = []
unpaid_from_redis = r.get(f"{etsy_connection_id}:unpaid_receipts")
if unpaid_from_redis is None:
logging.info("UNPAID NOT SET !!!")
r.set(f"{etsy_connection_id}:unpaid_receipts", "")
#######
unpaid_from_redis = unpaid_from_redis.split(",")
#######
if unpaid_from_redis is not None and len(unpaid_from_redis) > 0:
for unpaid_receipt in unpaid_from_redis:
logging.info(
f"{colored(' ', 'yellow')} Checking receipt id {unpaid_receipt} ... {colored(' ', 'yellow')}"
)
unpaid_responses = await AsyncEtsy.asyncLoop(
f=asyncEtsyApi.getAllPages,
method=Method.get,
url=EtsyUrl.getShop_Receipt2(unpaid_receipt),
params=my_params,
)
logging.info(
f"{len(unpaid_responses)} pages of fetched receipts found."
)
for res in unpaid_responses:
if res.status_code != 200:
logging.info(
f"{etsy_connection_id} check unpaids response was not successful {res.text}"
)
continue
res_json = res.json()
results: List[dict] = res_json["results"]
for receipt in results:
logging.info(receipt["receipt_id"])
was_paid: bool = receipt["was_paid"]
logging.info(f"was_paid: {was_paid}")
# logging.info(f"{receipt['receipt_id']} -> Transactions[0]: {f'\n\t' + str(receipt['Transactions'][0]['receipt_id'])} {f'\n\t' + str(receipt['Transactions'][0]['paid_tsz'])}")
paid_tzs: Optional[int] = receipt["Transactions"][0]["paid_tsz"]
if not was_paid or paid_tzs is None:
logging.info(
f"{receipt['receipt_id']} : was_paid={was_paid} : paid_tzs={paid_tzs}. Probably processing payment in the Etsy side."
)
# pop_list.append(i)
receipts_not_paid.append(receipt["receipt_id"])
continue
calculate_max_min_due_date(receipt)
receipts_to_be_inserted.append(receipt)
# for pop_index in pop_list:
# results.pop(pop_index)
# receipts_to_be_inserted = numpy.concatenate((receipts_to_be_inserted, results))
logging.info(
f"{asyncEtsyApi.shop_id} Receipts to be inserted -> {' , '.join(str(receipt['receipt_id']) for receipt in receipts_to_be_inserted)}"
)
logging.info(
f"{asyncEtsyApi.shop_id} Not yet finished payment process Receipts -> {receipts_not_paid}"
)
return receipts_not_paid, receipts_to_be_inserted
@staticmethod
async def check_for_new_orders(asyncEtsyApi, params):
logging.info(f"Checking for new orders {asyncEtsyApi.shop_id}")
receipts_not_paid = []
receipts_to_be_inserted = []
receipt_responses = await AsyncEtsy.asyncLoop(
f=asyncEtsyApi.getAllPages,
method=Method.get,
url=EtsyUrl.findAllShopReceipts(asyncEtsyApi.shop_id),
params=params,
)
for res in receipt_responses:
if res.status_code != 200:
logging.info(
f"{asyncEtsyApi.shop_id} check new orders response was not successful {res.text}"
)
continue
res_json = res.json()
results: List[dict] = res_json["results"]
for receipt in results:
logging.debug(receipt["receipt_id"])
was_paid: bool = receipt["was_paid"]
logging.debug(f"was_paid: {was_paid}")
paid_tzs: Optional[int] = receipt["Transactions"][0]["paid_tsz"]
# logging.info(f"{receipt['receipt_id']} -> Transactions[0]: {receipt['Transactions'][0]}")
if not was_paid or paid_tzs is None:
logging.info(
colored(
f"{receipt['receipt_id']} : was_paid={was_paid} : paid_tzs={paid_tzs}. Probably processing payment in the Etsy side.",
"magenta",
attrs=["underline", "bold"],
)
)
# pop_list.append(i)
receipts_not_paid.append(receipt["receipt_id"])
continue
calculate_max_min_due_date(receipt)
receipts_to_be_inserted.append(receipt)
# for pop_index in pop_list:
# results.pop(pop_index)
# receipts_to_be_inserted = numpy.concatenate((receipts_to_be_inserted, results))
# logging.info(receipts_not_paid)
# logging.info(receipts_to_be_inserted)
logging.info(
f"{asyncEtsyApi.shop_id} Receipts to be inserted -> {' , '.join(str(receipt['receipt_id']) for receipt in receipts_to_be_inserted)}"
)
logging.info(
f"{asyncEtsyApi.shop_id} Not yet finished payment process Receipts -> {receipts_not_paid}"
)
return receipts_not_paid, receipts_to_be_inserted
# @staticmethod
# async def syncShop(etsy_connection_id: str):
# is_successfull = False
# db = MongoDB().db
# r = MyRedis().r
# try:
# is_running = r.get(f"{etsy_connection_id}:is_running")
# logging.info(f"is_running: {is_running}")
# if is_running == "True":
# return {
# "background-task": "already running"
# }
# else:
# r.set(f"{etsy_connection_id}:is_running", "True")
# last_updated = r.get(f"{etsy_connection_id}:last_updated")
# params = {
# "includes": "Transactions/MainImage,Listings/ShippingTemplate"
# }
# if last_updated is not None:
# last_updated = int(last_updated)
# logging.info("last_updated is not None, setting min_created")
# params["min_created"] = last_updated
# current_time = int(datetime.now().timestamp())
# params["max_created"] = current_time
# if last_updated is not None:
# logging.info(f"From = {datetime.fromtimestamp(last_updated)}\nTo = {datetime.fromtimestamp(current_time)}")
# else:
# logging.info(f"From = -\nTo = {datetime.fromtimestamp(current_time)}")
# asyncEtsyApi = await AsyncEtsy.getAsyncEtsyApi(etsy_connection_id, db)
# etsyShopManager = EtsyShopManager(asyncEtsyApi.shop_id)
# receipts_not_paid, receipts_to_be_inserted = await EtsyShopManager.check_unpaids(etsy_connection_id, asyncEtsyApi, params, r)
# unpaid_receipts, r_to_be_inserted = await EtsyShopManager.check_for_new_orders(asyncEtsyApi, params)
# receipts_not_paid = receipts_not_paid + unpaid_receipts
# receipts_not_paid = set(receipts_not_paid)
# receipts_not_paid = list(receipts_not_paid)
# logging.info(f"{asyncEtsyApi.shop_id} Final Merged not yet finished payment processed Receipts -> {receipts_not_paid}")
# r.set(f"{etsy_connection_id}:unpaid_receipts", ','.join(str(not_paid_receipt) for not_paid_receipt in receipts_not_paid))
# receipts_to_be_inserted = receipts_to_be_inserted + r_to_be_inserted
# logging.info(f"{asyncEtsyApi.shop_id} Final Receipts to be inserted into MongoDB -> {[receipt['receipt_id'] for receipt in receipts_to_be_inserted]}")
# mongodb_result = await etsyShopManager.insert_receipts(receipts_to_be_inserted, db)
# if len(mongodb_result) == len(receipts_to_be_inserted):
# logging.info("successfully inserted all receipts")
# logging.info("setting last_updated")
# r.set(f"{etsy_connection_id}:last_updated", current_time)
# except Exception as e:
# logging.exception(e)
# else:
# is_successfull = True
# finally:
# logging.info(f".---'| {asyncEtsyApi.shop_id} {'was Successful' if is_successfull else 'Failed'} |'---.")
# r.set(f"{etsy_connection_id}:is_running", "False")
async def syncShop(etsy_connection_id: str):
logging.info(
colored(
f"Sync Etsy Shop receipts process started for etsy_shop_connection: {etsy_connection_id}",
"blue",
attrs=["reverse", "blink"],
)
)
is_successfull = False
db = MongoDB().db
r = MyRedis().r
try:
is_running = r.get(f"{etsy_connection_id}:is_running")
logging.info(f"is_running: {is_running}")
if is_running == "True":
logging.info(
colored(
f"{etsy_connection_id} is already running. Exisiting syncShop function.",
"yellow",
"on_grey",
attrs=["blink"],
)
)
return {"background-task": "already running"}
else:
r.set(f"{etsy_connection_id}:is_running", "True")
last_updated = r.get(f"{etsy_connection_id}:last_updated")
params = {"includes": "Transactions/MainImage,Listings/ShippingTemplate"}
if last_updated is not None:
last_updated = int(last_updated)
logging.info("last_updated is not None, setting min_created")
params["min_created"] = last_updated
current_time = int(datetime.now().timestamp())
params["max_created"] = current_time
if last_updated is not None:
logging.info(f"From = {datetime.fromtimestamp(last_updated)}")
logging.info(f"To = {datetime.fromtimestamp(current_time)}")
else:
logging.info(f"From = -")
logging.info(f"To = {datetime.fromtimestamp(current_time)}")
asyncEtsyApi = await AsyncEtsy.getAsyncEtsyApi(etsy_connection_id, db)
etsyShopManager = MyEtsyShopManager(asyncEtsyApi.shop_id)
(
receipts_not_paid,
receipts_to_be_inserted,
) = await MyEtsyShopManager.check_unpaids(
etsy_connection_id, asyncEtsyApi, params, r
)
(
unpaid_receipts,
r_to_be_inserted,
) = await MyEtsyShopManager.check_for_new_orders(asyncEtsyApi, params)
receipts_not_paid = receipts_not_paid + unpaid_receipts
receipts_not_paid = set(receipts_not_paid)
receipts_not_paid = list(receipts_not_paid)
logging.info(
f"{asyncEtsyApi.shop_id} FINAL Receipts that paid_tsz was not found -> {colored(receipts_not_paid, attrs=['bold', 'underline'])}"
)
r.set(
f"{etsy_connection_id}:unpaid_receipts",
",".join(str(not_paid_receipt) for not_paid_receipt in receipts_not_paid),
)
receipts_to_be_inserted = receipts_to_be_inserted + r_to_be_inserted
### Check duplicate receipts and preserve only one ###
# receipts_to_be_inserted = [dict(t) for t in {tuple(d.items()) for d in receipts_to_be_inserted}]
receipts_to_be_inserted = [
i
for n, i in enumerate(receipts_to_be_inserted)
if i not in receipts_to_be_inserted[n + 1 :]
]
logging.info(
f"{colored(asyncEtsyApi.shop_id, 'blue', 'on_grey', attrs=['bold', 'underline'])} There are total {len(receipts_to_be_inserted)} orders that will be inserted into MongoDB."
)
######################################################
logging.info(
f"{asyncEtsyApi.shop_id} FINAL Receipts to be inserted into MongoDB -> {colored(' , '.join(str(receipt['receipt_id']) for receipt in receipts_to_be_inserted), attrs=['bold', 'underline'])}"
)
mongodb_result = await etsyShopManager.insert_receipts(
receipts_to_be_inserted, db
)
# if len(mongodb_result) == len(receipts_to_be_inserted):
# logging.info(colored("successfully inserted all receipts", 'green', 'on_grey', attrs=['blink']))
# logging.info("setting last_updated")
# r.set(f"{etsy_connection_id}:last_updated", current_time)
except Exception as e:
logging.exception(e)
else:
is_successfull = True
logging.info(
colored(
"successfully inserted all receipts",
"green",
"on_grey",
attrs=["blink"],
)
)
logging.info("setting last_updated")
r.set(f"{etsy_connection_id}:last_updated", current_time)
finally:
r.set(f"{etsy_connection_id}:is_running", "False")
try:
logging.info(
f".---'| {colored(asyncEtsyApi.shop_id, 'blue', 'on_grey', attrs=['bold', 'underline'])} {colored('was Successful', 'green', 'on_white', attrs=['reverse', 'blink', 'bold']) if is_successfull else colored('Failed', 'red', 'on_white', attrs=['reverse', 'blink', 'bold'])} |'---."
)
except UnboundLocalError:
logging.info(
f".---'| {colored(etsy_connection_id, 'blue', 'on_grey', attrs=['bold', 'underline'])} {colored('was Successful', 'green', 'on_white', attrs=['reverse', 'blink', 'bold']) if is_successfull else colored('Failed', 'red', 'on_white', attrs=['reverse', 'blink', 'bold'])} |'---."
)