completed receiveFromSQS.py
This commit is contained in:
1
slack-bot/traffic-scraper/prd/11-10-2019_00:30:27.json
Normal file
1
slack-bot/traffic-scraper/prd/11-10-2019_00:30:27.json
Normal 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"]
|
||||||
Binary file not shown.
@@ -177,13 +177,16 @@ class getTrafficInfo(object):
|
|||||||
use_aliases=True,
|
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(random.choice(self.sarcasticMessage))
|
||||||
self.output.append('END')
|
self.output.append('END')
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
# inst = getTrafficInfo.getTrafficURL('A50').findIncidents() \
|
inst = getTrafficInfo.getTrafficURL('M62').findIncidents() \
|
||||||
# .getIncidentInformation().generateOutput()
|
.getIncidentInformation().generateOutput()
|
||||||
# for i in inst.output:
|
for i in inst.output:
|
||||||
# print(i)
|
print(i)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import boto3
|
import boto3
|
||||||
import base64
|
import base64
|
||||||
import math
|
import math
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class receiveFromSQS(object):
|
class receiveFromSQS(object):
|
||||||
@@ -35,20 +37,18 @@ class receiveFromSQS(object):
|
|||||||
self.loops = int(
|
self.loops = int(
|
||||||
math.ceil(totalNumberOfMessages / maxNumberOfMessages)
|
math.ceil(totalNumberOfMessages / maxNumberOfMessages)
|
||||||
)
|
)
|
||||||
|
loopTrack = 0
|
||||||
if totalNumberOfMessages <= 10:
|
if totalNumberOfMessages <= 10:
|
||||||
maxNumberOfMessages = totalNumberOfMessages
|
maxNumberOfMessages = totalNumberOfMessages
|
||||||
else:
|
else:
|
||||||
# Find how many times total-loop*max, on last loop
|
maxNumberOfMessagesFinal = 10 - (
|
||||||
# replace max with this
|
(self.loops * maxNumberOfMessages) - totalNumberOfMessages
|
||||||
maxNumberOfMessagesFinal = self.loops * maxNumberOfMessages - abs(10 - (
|
)
|
||||||
totalNumberOfMessages - (self.loops * maxNumberOfMessages)
|
|
||||||
))
|
|
||||||
print(maxNumberOfMessagesFinal)
|
print(maxNumberOfMessagesFinal)
|
||||||
if self.loops == 0:
|
if self.loops == 0:
|
||||||
raise RuntimeError('No messages in the queue')
|
raise RuntimeError('No messages in the queue')
|
||||||
for i in range(0, self.loops):
|
for i in range(0, self.loops):
|
||||||
loopTrack = 0
|
if loopTrack == self.loops - 1 and totalNumberOfMessages > 10:
|
||||||
if loopTrack == self.loops:
|
|
||||||
maxNumberOfMessages = maxNumberOfMessagesFinal
|
maxNumberOfMessages = maxNumberOfMessagesFinal
|
||||||
self.resp.append(
|
self.resp.append(
|
||||||
self.sqs.receive_message(
|
self.sqs.receive_message(
|
||||||
@@ -56,12 +56,18 @@ class receiveFromSQS(object):
|
|||||||
MaxNumberOfMessages=maxNumberOfMessages,
|
MaxNumberOfMessages=maxNumberOfMessages,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
entries = [ # Needs Keyerror try
|
try:
|
||||||
{'Id': msg['MessageId'], 'ReceiptHandle': msg['ReceiptHandle']}
|
entries = [
|
||||||
|
{
|
||||||
|
'Id': msg['MessageId'],
|
||||||
|
'ReceiptHandle': msg['ReceiptHandle'],
|
||||||
|
}
|
||||||
for msg in self.resp[i]['Messages']
|
for msg in self.resp[i]['Messages']
|
||||||
]
|
]
|
||||||
self._deleteSQSMessages(entries)
|
self._deleteSQSMessages(entries)
|
||||||
loopTrack += 1
|
loopTrack += 1
|
||||||
|
except KeyError:
|
||||||
|
print("No messages in the queue")
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _extractMessageFromSQS(self, totalNumberOfMessages):
|
def _extractMessageFromSQS(self, totalNumberOfMessages):
|
||||||
@@ -119,7 +125,22 @@ class receiveFromSQS(object):
|
|||||||
self.receiveAllMessages(
|
self.receiveAllMessages(
|
||||||
b64=b64, _totalNumberOfMessages=numberOfMessages
|
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(
|
inst = receiveFromSQS.createSession(
|
||||||
@@ -128,6 +149,12 @@ inst = receiveFromSQS.createSession(
|
|||||||
'.com/745437999005/slack-bot.fifo',
|
'.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:
|
for item in inst.messages:
|
||||||
print(item)
|
print(item)
|
||||||
|
|||||||
Reference in New Issue
Block a user