initial slack-bot
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
["START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "Did someone say M62!? \ud83d\ude27 Let's check the latest updates from Highways England as of 03:23:20! \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"]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
["START", "Did someone say M62!? \ud83d\ude27 Let's check the latest updates from Highways England as of 16:13:42! \ud83d\ude93\ud83d\udea8", "There are currently no reported congestion incidents on the M62 \ud83e\udd14", "There are currently 1 reported accident incidents reported on the M62 \ud83d\ude31", "Location : The M62 westbound between junctions J22 and J21 . \nLane Closures : The hard shoulder and lane 1 are closed. \nReason : Road traffic collision. \nStatus : Currently Active. \nTime To Clear : The event is expected to clear between 17:00 and 17:15 on 12 October 2019. \nReturn To Normal : Normal traffic conditions are expected between 17:00 and 17:15 on 12 October 2019. \nLanes Closed : There is one of three lanes closed.", "Hey Andy, maybe flying would be quicker? \ud83d\ude81", "END"]
|
|
||||||
Binary file not shown.
@@ -4,6 +4,8 @@ from datetime import datetime
|
|||||||
import re
|
import re
|
||||||
import emoji
|
import emoji
|
||||||
import random
|
import random
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class getTrafficInfo(object):
|
class getTrafficInfo(object):
|
||||||
@@ -44,9 +46,9 @@ class getTrafficInfo(object):
|
|||||||
self.motorway = motorway
|
self.motorway = motorway
|
||||||
self.incidentTypes = ['congestion', 'accident']
|
self.incidentTypes = ['congestion', 'accident']
|
||||||
|
|
||||||
# def __del__(self):
|
def __del__(self):
|
||||||
# print('Quitting')
|
print('Quitting')
|
||||||
# self.browser.quit()
|
self.browser.quit()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def getTrafficURL(cls, motorway, driver='chrome'):
|
def getTrafficURL(cls, motorway, driver='chrome'):
|
||||||
@@ -140,7 +142,7 @@ class getTrafficInfo(object):
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
currentTime = datetime.now().strftime('%H:%M:%S')
|
currentTime = datetime.now().strftime('%H:%M:%S')
|
||||||
self.output.append('START')
|
# self.output.append('START')
|
||||||
self.output.append(
|
self.output.append(
|
||||||
emoji.emojize(
|
emoji.emojize(
|
||||||
f'Did someone say {self.motorway}!?'
|
f'Did someone say {self.motorway}!?'
|
||||||
@@ -182,11 +184,19 @@ class getTrafficInfo(object):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
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
|
||||||
|
|
||||||
|
def savetoDisk(self, path=os.getcwd()):
|
||||||
|
self.timeNow = datetime.now().strftime('%d-%m-%Y_%H:%M:%S')
|
||||||
|
if len(self.output) > 0:
|
||||||
|
with open(f'{path}/{self.timeNow}.json', 'w+') as outputFile:
|
||||||
|
json.dump(self.output, outputFile)
|
||||||
|
else:
|
||||||
|
print('No messages to save')
|
||||||
|
|
||||||
inst = getTrafficInfo.getTrafficURL('M62').findIncidents() \
|
|
||||||
.getIncidentInformation().generateOutput()
|
# inst = getTrafficInfo.getTrafficURL('M62').findIncidents() \
|
||||||
for i in inst.output:
|
# .getIncidentInformation().generateOutput()
|
||||||
print(i)
|
# for i in inst.output:
|
||||||
|
# print(i)
|
||||||
|
|||||||
109
slack-bot/traffic-scraper/prd/slackBot.py
Normal file
109
slack-bot/traffic-scraper/prd/slackBot.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
import sys
|
||||||
|
from datetime import datetime
|
||||||
|
sys.path.append(os.getcwd())
|
||||||
|
import pullTrafficInfo
|
||||||
|
import http.client
|
||||||
|
|
||||||
|
inst = (
|
||||||
|
pullTrafficInfo.getTrafficInfo.getTrafficURL('M62')
|
||||||
|
.findIncidents()
|
||||||
|
.getIncidentInformation()
|
||||||
|
.generateOutput()
|
||||||
|
.savetoDisk()
|
||||||
|
)
|
||||||
|
|
||||||
|
listOfFiles = glob.glob(f'{os.getcwd()}/*.json')
|
||||||
|
latestFile = max(listOfFiles, key=os.path.getctime)
|
||||||
|
# latestFile = (f'{os.getcwd()}/12-10-2019_03:23:21.json')
|
||||||
|
# latestFile = (f'{os.getcwd()}/12-10-2019_16:13:43.json')
|
||||||
|
|
||||||
|
with open(latestFile, 'r') as jsonFile:
|
||||||
|
data = json.load(jsonFile)
|
||||||
|
|
||||||
|
# print(data)
|
||||||
|
|
||||||
|
congestionMessage = None
|
||||||
|
accidentMessage = None
|
||||||
|
congestionMessageStart = None
|
||||||
|
congestionMessageEnd = None
|
||||||
|
accidentMessageStart = None
|
||||||
|
accidentMessageEnd = None
|
||||||
|
|
||||||
|
messageTitle = data[0]
|
||||||
|
end = data[-1]
|
||||||
|
|
||||||
|
length = len(data)
|
||||||
|
for i, message in zip(range(0, length), data):
|
||||||
|
if (
|
||||||
|
'There are currently no reported congestion incidents on the M62 🤔'
|
||||||
|
in message
|
||||||
|
):
|
||||||
|
congestionTitle = 'Congestion Incidents'
|
||||||
|
congestionMessage = message
|
||||||
|
|
||||||
|
elif 'congestion incidents on the M62 😱' in message:
|
||||||
|
congestionTitle = message
|
||||||
|
congestionMessageStart = i + 1
|
||||||
|
|
||||||
|
if (
|
||||||
|
'There are currently no reported accident incidents on the M62 🤔'
|
||||||
|
in message
|
||||||
|
):
|
||||||
|
congestionMessageEnd = i
|
||||||
|
accidentTitle = 'Accident Incidents'
|
||||||
|
accidentMessage = message
|
||||||
|
|
||||||
|
elif 'accident incidents on the M62 😱' in message:
|
||||||
|
accidentTitle = message
|
||||||
|
accidentMessageStart = i + 1
|
||||||
|
accidentMessageEnd = length - 1
|
||||||
|
|
||||||
|
|
||||||
|
if congestionMessage is None:
|
||||||
|
congestionMessage = data[congestionMessageStart:congestionMessageEnd]
|
||||||
|
congestionMessage = ''.join(congestionMessage)
|
||||||
|
|
||||||
|
if accidentMessage is None:
|
||||||
|
accidentMessage = data[accidentMessageStart:accidentMessageEnd]
|
||||||
|
accidentMessage = ''.join(accidentMessage)
|
||||||
|
|
||||||
|
print(f'messageTitle: {messageTitle}' + '\n')
|
||||||
|
print(f'congestionTitle: {congestionTitle}' + '\n')
|
||||||
|
print(f'congestionMessage: {congestionMessage}' + '\n')
|
||||||
|
print(f'accidentTitle: {accidentTitle}' + '\n')
|
||||||
|
print(f'accidentMessage: {accidentMessage}' + '\n')
|
||||||
|
print(f'end: {end}')
|
||||||
|
|
||||||
|
conn = http.client.HTTPConnection("hooks,slack,com")
|
||||||
|
|
||||||
|
payload = """{\"attachments\": [\n {\n \"title\": \"",\n \"pretext\": \"Did someone say M62!? \\ud83d\\ude27\",\n \"text\": \"\",\n \"mrkdwn_in\": [\"text\", \"pretext\"],\n \"footer\": \"ec2-54-246-210-90.eu-west-1.compute.amazonaws.com\",\n \"footer_icon\": \"https://i.imgur.com/ADsI87O.png\",\n \"ts\": \"1570917914\",\n \"fields\": [\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t\t \t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"\",\n\t\t \"short\": false\n\t\t \t},\n\t \t\t{\n\t\t \"title\": \"\",\n\t\t \"value\": \" \",\n\t\t \"short\": false\n\t\t \t}\n\t \t]\n }\n ]\n}"""
|
||||||
|
|
||||||
|
|
||||||
|
timeNow = datetime.now().timestamp()
|
||||||
|
|
||||||
|
jsonPayload = json.loads(payload)
|
||||||
|
print(jsonPayload)
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['title'] = f'{messageTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['ts'] = timeNow
|
||||||
|
print(jsonPayload['attachments'][0]['ts'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][0]['title'] = f'{congestionTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][0]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][0]['value'] = f'{congestionMessage}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][0]['value'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][1]['title'] = f'{accidentTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][1]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][1]['value'] = f'{accidentMessage}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][1]['value'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][2]['value'] = f'{end}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][2]['value'])
|
||||||
30
slack-bot/traffic-scraper/prd/test.py
Normal file
30
slack-bot/traffic-scraper/prd/test.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
payload = """{\"attachments\": [\n {\n \"title\": \"{title}",\n \"pretext\": \"Did someone say M62!? \\ud83d\\ude27\",\n \"text\": \"\",\n \"mrkdwn_in\": [\"text\", \"pretext\"],\n \"footer\": \"ec2-54-246-210-90.eu-west-1.compute.amazonaws.com\",\n \"footer_icon\": \"https://i.imgur.com/ADsI87O.png\",\n \"ts\": \"1570917914\",\n \"fields\": [\n\t\t \t{\n\t\t \"title\": \"{congestionTitle}\",\n\t\t \"value\": \"{congestionMessage}\",\n\t\t \"short\": false\n\t\t \t},\n\t\t \t{\n\t\t \"title\": \"{accidentTitle}\",\n\t\t \"value\": \"{accidentMessage}\",\n\t\t \"short\": false\n\t\t \t},\n\t \t\t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"{end} \",\n\t\t \"short\": false\n\t\t \t}\n\t \t]\n }\n ]\n}"""
|
||||||
|
|
||||||
|
timeNow = datetime.now().timestamp()
|
||||||
|
|
||||||
|
jsonPayload = json.loads(payload)
|
||||||
|
print(jsonPayload)
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['title'] = f'{messageTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['ts'] = timeNow
|
||||||
|
print(jsonPayload['attachments'][0]['ts'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][0]['title'] = f'{congestionTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][0]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][0]['value'] = f'{congestionMessage}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][0]['value'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][1]['title'] = f'{accidentTitle}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][1]['title'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][1]['value'] = f'{accidentMessage}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][1]['value'])
|
||||||
|
|
||||||
|
jsonPayload['attachments'][0]['fields'][2]['value'] = f'{end}'
|
||||||
|
print(jsonPayload['attachments'][0]['fields'][2]['value'])
|
||||||
Reference in New Issue
Block a user