Increasing time delta, adding new functions to record sent notifications to avoid duplicats

This commit is contained in:
Dom 2024-07-12 19:48:15 +01:00
parent 75dbd21876
commit d67a95f8cf
4 changed files with 94 additions and 59 deletions

7
SentNotifications.json Normal file
View File

@ -0,0 +1,7 @@
{
"NotificationsSent": [
"UID1",
"UID1",
"UID1"
]
}

View File

@ -30,7 +30,25 @@ def log(log_message, level):
log_message_types[level](log_message)
def isShipmentWithinLastTenMinutes(shipmentCreationTime):
def updateSentNotifications(inboundPlanId):
with open('SentNotifications.json') as NotificationsSentJson:
NotificationsSent = json.load(NotificationsSentJson)
NotificationsSent['NotificationsSent'].append(inboundPlanId)
with open('SentNotifications.json', mode='w') as outputNotificationsSent:
outputNotificationsSent.write(json.dumps(NotificationsSent, indent=4))
def isIDInSentNotifications(inboundPlanId):
with open('SentNotifications.json') as NotificationsSentJson:
NotificationsSent = json.load(NotificationsSentJson)
if inboundPlanId in NotificationsSent['NotificationsSent']:
return True
else:
return False
def isShipmentWithinSpecifiedDelta(shipmentCreationTime):
currentTime = datetime.now()
shipmentTime = datetime.strptime(shipmentCreationTime, '%Y-%m-%dT%H:%M:%SZ')
timeDelta = currentTime - shipmentTime
@ -39,7 +57,7 @@ def isShipmentWithinLastTenMinutes(shipmentCreationTime):
log(f'Shipment creation time: {shipmentTime}', 'info')
log(f'Time delta: {timeDelta}', 'info')
if timeDelta < timedelta(minutes=10):
if timeDelta < timedelta(minutes=360):
return True
else:
return False
@ -72,33 +90,42 @@ def getInboundShipments(settings=SETTINGS):
return InboundShipments.json()['inboundPlans']
def parseInboundShipments(settings=SETTINGS):
log('\U0001F504 Getting shipments...', 'info')
InboundShipments = getInboundShipments()
inboundPlanIDs = []
shipmentData = {}
for shipment in InboundShipments:
log('Got shipment creation date: {}'.format(shipment['createdAt']), 'info')
if isShipmentWithinLastTenMinutes(shipment['createdAt']):
if isShipmentWithinSpecifiedDelta(shipment['createdAt']):
log('Adding inbound plan to list: {}'.format(shipment['inboundPlanId']), 'info')
inboundPlanIDs.append(shipment['inboundPlanId'])
if inboundPlanIDs:
log('\U0001F440 Checking shipments...', 'info')
log(f'Shipments to check: {len(inboundPlanIDs)}', 'info')
for ID in inboundPlanIDs:
getShipment = requests.get(
settings['SPAPI_ENDPOINT'] + f'/inbound/fba/2024-03-20/inboundPlans/{ID}/items',
headers = {
'x-amz-access-token': getAccessToken(),
}
)
if getShipment.json()['items']:
itemDict = {}
totalItemCount = 0
for item in getShipment.json()['items']:
itemDict.update({item.get('msku'): item.get('quantity')})
totalItemCount += item['quantity']
itemDict.update({'Total item count': totalItemCount})
shipmentData.update({ID: itemDict})
newline = '\n' #Escapes are not allowed inside fstrings
sendDiscordNotification(content=f':package: New shipment detected! :package:\nShipment contents:\n{newline.join(f"- {MSKU}: {Count}" for MSKU, Count in shipmentData[ID].items())}')
if isIDInSentNotifications(ID):
log(f'Ignoring {ID}, notification has already been sent', 'info')
elif not isIDInSentNotifications(ID):
getShipment = requests.get(
settings['SPAPI_ENDPOINT'] + f'/inbound/fba/2024-03-20/inboundPlans/{ID}/items',
headers = {
'x-amz-access-token': getAccessToken(),
}
)
if getShipment.json()['items']:
itemDict = {}
totalItemCount = 0
for item in getShipment.json()['items']:
itemDict.update({item.get('msku'): item.get('quantity')})
totalItemCount += item['quantity']
itemDict.update({'Total item count': totalItemCount})
shipmentData.update({ID: itemDict})
log(f'\U0001F514 Sending Discord notification for {ID}...', 'info')
newline = '\n'
sendDiscordNotification(content=f':package: New shipment detected :package:\nShipment contents:\n{newline.join(f"- {MSKU}: {Count}" for MSKU, Count in shipmentData[ID].items())}')
updateSentNotifications(ID)
parseInboundShipments()

View File

@ -1,40 +0,0 @@
[INFO] Got shipment creation date: 2024-07-10T09:34:31Z
[INFO] Current time: 2024-07-12 00:00:02.372223
[INFO] Shipment creation time: 2024-07-10 09:34:31
[INFO] Time delta: 1 day, 14:25:31.372223
[INFO] Got shipment creation date: 2024-07-09T15:47:43Z
[INFO] Current time: 2024-07-12 00:00:02.374225
[INFO] Shipment creation time: 2024-07-09 15:47:43
[INFO] Time delta: 2 days, 8:12:19.374225
[INFO] Got shipment creation date: 2024-07-08T12:23:20Z
[INFO] Current time: 2024-07-12 00:00:02.374439
[INFO] Shipment creation time: 2024-07-08 12:23:20
[INFO] Time delta: 3 days, 11:36:42.374439
[INFO] Got shipment creation date: 2024-07-05T12:57:09Z
[INFO] Current time: 2024-07-12 00:00:02.374646
[INFO] Shipment creation time: 2024-07-05 12:57:09
[INFO] Time delta: 6 days, 11:02:53.374646
[INFO] Got shipment creation date: 2024-07-05T11:39:06Z
[INFO] Current time: 2024-07-12 00:00:02.374851
[INFO] Shipment creation time: 2024-07-05 11:39:06
[INFO] Time delta: 6 days, 12:20:56.374851
[INFO] Got shipment creation date: 2024-07-04T13:50:17Z
[INFO] Current time: 2024-07-12 00:00:02.375084
[INFO] Shipment creation time: 2024-07-04 13:50:17
[INFO] Time delta: 7 days, 10:09:45.375084
[INFO] Got shipment creation date: 2024-07-03T13:29:03Z
[INFO] Current time: 2024-07-12 00:00:02.375289
[INFO] Shipment creation time: 2024-07-03 13:29:03
[INFO] Time delta: 8 days, 10:30:59.375289
[INFO] Got shipment creation date: 2024-07-02T14:06:09Z
[INFO] Current time: 2024-07-12 00:00:02.375483
[INFO] Shipment creation time: 2024-07-02 14:06:09
[INFO] Time delta: 9 days, 9:53:53.375483
[INFO] Got shipment creation date: 2024-07-01T14:54:48Z
[INFO] Current time: 2024-07-12 00:00:02.375667
[INFO] Shipment creation time: 2024-07-01 14:54:48
[INFO] Time delta: 10 days, 9:05:14.375667
[INFO] Got shipment creation date: 2024-06-28T06:20:41Z
[INFO] Current time: 2024-07-12 00:00:02.375851
[INFO] Shipment creation time: 2024-06-28 06:20:41
[INFO] Time delta: 13 days, 17:39:21.375851

View File

@ -0,0 +1,41 @@
[INFO] 🔄 Getting shipments...
[INFO] Got shipment creation date: 2024-07-12T13:07:50Z
[INFO] Current time: 2024-07-12 19:40:02.303950
[INFO] Shipment creation time: 2024-07-12 13:07:50
[INFO] Time delta: 6:32:12.303950
[INFO] Got shipment creation date: 2024-07-10T09:34:31Z
[INFO] Current time: 2024-07-12 19:40:02.306061
[INFO] Shipment creation time: 2024-07-10 09:34:31
[INFO] Time delta: 2 days, 10:05:31.306061
[INFO] Got shipment creation date: 2024-07-09T15:47:43Z
[INFO] Current time: 2024-07-12 19:40:02.306273
[INFO] Shipment creation time: 2024-07-09 15:47:43
[INFO] Time delta: 3 days, 3:52:19.306273
[INFO] Got shipment creation date: 2024-07-08T12:23:20Z
[INFO] Current time: 2024-07-12 19:40:02.306490
[INFO] Shipment creation time: 2024-07-08 12:23:20
[INFO] Time delta: 4 days, 7:16:42.306490
[INFO] Got shipment creation date: 2024-07-05T12:57:09Z
[INFO] Current time: 2024-07-12 19:40:02.306689
[INFO] Shipment creation time: 2024-07-05 12:57:09
[INFO] Time delta: 7 days, 6:42:53.306689
[INFO] Got shipment creation date: 2024-07-05T11:39:06Z
[INFO] Current time: 2024-07-12 19:40:02.306884
[INFO] Shipment creation time: 2024-07-05 11:39:06
[INFO] Time delta: 7 days, 8:00:56.306884
[INFO] Got shipment creation date: 2024-07-04T13:50:17Z
[INFO] Current time: 2024-07-12 19:40:02.307080
[INFO] Shipment creation time: 2024-07-04 13:50:17
[INFO] Time delta: 8 days, 5:49:45.307080
[INFO] Got shipment creation date: 2024-07-03T13:29:03Z
[INFO] Current time: 2024-07-12 19:40:02.307283
[INFO] Shipment creation time: 2024-07-03 13:29:03
[INFO] Time delta: 9 days, 6:10:59.307283
[INFO] Got shipment creation date: 2024-07-02T14:06:09Z
[INFO] Current time: 2024-07-12 19:40:02.307511
[INFO] Shipment creation time: 2024-07-02 14:06:09
[INFO] Time delta: 10 days, 5:33:53.307511
[INFO] Got shipment creation date: 2024-07-01T14:54:48Z
[INFO] Current time: 2024-07-12 19:40:02.307714
[INFO] Shipment creation time: 2024-07-01 14:54:48
[INFO] Time delta: 11 days, 4:45:14.307714