completed receiveFromSQS.py

This commit is contained in:
2019-10-11 01:07:42 +01:00
parent 0773fa28c5
commit 36bf49f0ca
4 changed files with 51 additions and 20 deletions

View File

@@ -0,0 +1 @@
["START", "Did someone say M62!? \ud83d\ude27 Let's check the latest updates from Highways England as of 00:30:21! \ud83d\ude93\ud83d\udea8", "There are currently no reported congestion incidents on the M62 \ud83e\udd14", "There are currently no reported accident incidents on the M62 \ud83e\udd14", "Hey Andy, have you thought about getting the train? \ud83d\ude85", "END"]

View File

@@ -177,13 +177,16 @@ class getTrafficInfo(object):
use_aliases=True,
)
)
self.output.append(self.incidentInformation[0][i][0])
try:
self.output.append(self.incidentInformation[i][0][0])
except IndexError:
pass
self.output.append(random.choice(self.sarcasticMessage))
self.output.append('END')
return self
# inst = getTrafficInfo.getTrafficURL('A50').findIncidents() \
# .getIncidentInformation().generateOutput()
# for i in inst.output:
# print(i)
inst = getTrafficInfo.getTrafficURL('M62').findIncidents() \
.getIncidentInformation().generateOutput()
for i in inst.output:
print(i)

View File

@@ -1,6 +1,8 @@
import boto3
import base64
import math
import json
from datetime import datetime
class receiveFromSQS(object):
@@ -35,20 +37,18 @@ class receiveFromSQS(object):
self.loops = int(
math.ceil(totalNumberOfMessages / maxNumberOfMessages)
)
loopTrack = 0
if totalNumberOfMessages <= 10:
maxNumberOfMessages = totalNumberOfMessages
else:
# Find how many times total-loop*max, on last loop
# replace max with this
maxNumberOfMessagesFinal = self.loops * maxNumberOfMessages - abs(10 - (
totalNumberOfMessages - (self.loops * maxNumberOfMessages)
))
maxNumberOfMessagesFinal = 10 - (
(self.loops * maxNumberOfMessages) - totalNumberOfMessages
)
print(maxNumberOfMessagesFinal)
if self.loops == 0:
raise RuntimeError('No messages in the queue')
for i in range(0, self.loops):
loopTrack = 0
if loopTrack == self.loops:
if loopTrack == self.loops - 1 and totalNumberOfMessages > 10:
maxNumberOfMessages = maxNumberOfMessagesFinal
self.resp.append(
self.sqs.receive_message(
@@ -56,12 +56,18 @@ class receiveFromSQS(object):
MaxNumberOfMessages=maxNumberOfMessages,
)
)
entries = [ # Needs Keyerror try
{'Id': msg['MessageId'], 'ReceiptHandle': msg['ReceiptHandle']}
try:
entries = [
{
'Id': msg['MessageId'],
'ReceiptHandle': msg['ReceiptHandle'],
}
for msg in self.resp[i]['Messages']
]
self._deleteSQSMessages(entries)
loopTrack += 1
except KeyError:
print("No messages in the queue")
return self
def _extractMessageFromSQS(self, totalNumberOfMessages):
@@ -119,7 +125,22 @@ class receiveFromSQS(object):
self.receiveAllMessages(
b64=b64, _totalNumberOfMessages=numberOfMessages
)
pass
return self
def generateOutput(self, type='json'):
if type == 'json':
self.output = json.dumps(self.messages)
return self.output
def savetoDisk(self, path):
self.timeNow = datetime.now().strftime('%d-%m-%Y_%H:%M:%S')
if self.output is None:
self.generateOutput()
if len(self.messages) > 0:
with open(f'{path}/{self.timeNow}.json', 'w+') as outputFile:
outputFile.write(self.output)
else:
print('No messages to save')
inst = receiveFromSQS.createSession(
@@ -128,6 +149,12 @@ inst = receiveFromSQS.createSession(
'.com/745437999005/slack-bot.fifo',
)
inst.receiveNMessages(numberOfMessages=12)
output = inst.receiveNMessages(numberOfMessages=6).generateOutput(type='json')
inst.savetoDisk(
'/Users/dtomlinson/OneDrive - William Hill'
' Organisation Limited/Mac/git_repos/python-VM/slack-bot/traffic-scraper'
'/prd'
)
# inst.receiveAllMessages()
for item in inst.messages:
print(item)