Merge branch 'develop'

This commit is contained in:
2019-11-19 23:50:37 +00:00
11 changed files with 200 additions and 17 deletions

View File

@@ -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"]

View File

@@ -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"]

View File

@@ -0,0 +1 @@
{"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}

View File

@@ -1,8 +1,17 @@
import json
import glob
import os
with open('/Users/dtomlinson/OneDrive - William Hill Organisation Limited'
'/Mac/git_repos/python-VM/slack-bot/traffic-scraper/'
'prd/12-10-2019_02:53:32.json', 'r') as jsonFile:
listOfFiles = glob.glob(f'{os.getcwd()}/*.json')
latestFile = max(listOfFiles, key=os.path.getctime)
with open(latestFile, 'r') as jsonFile:
data = json.load(jsonFile)
for item in data:
print(item)
listOfFiles.remove(latestFile)
if len(listOfFiles) > 2:
for files in listOfFiles:
os.remove(files)

View File

@@ -4,6 +4,8 @@ from datetime import datetime
import re
import emoji
import random
import json
import os
class getTrafficInfo(object):
@@ -44,9 +46,9 @@ class getTrafficInfo(object):
self.motorway = motorway
self.incidentTypes = ['congestion', 'accident']
# def __del__(self):
# print('Quitting')
# self.browser.quit()
def __del__(self):
print('Quitting')
self.browser.quit()
@classmethod
def getTrafficURL(cls, motorway, driver='chrome'):
@@ -140,11 +142,9 @@ class getTrafficInfo(object):
),
]
currentTime = datetime.now().strftime('%H:%M:%S')
self.output.append('START')
# self.output.append('START')
self.output.append(
emoji.emojize(
f'Did someone say {self.motorway}!?'
' :anguished:'
' Let\'s check the latest updates'
' from Highways England as of'
f' {currentTime}!'
@@ -172,7 +172,7 @@ class getTrafficInfo(object):
f'There are currently'
f' {self.incidentCount[i]} reported'
f' {self.incidentTypes[i]} incidents'
f' reported on the {self.motorway}'
f' on the {self.motorway}'
f' :scream:',
use_aliases=True,
)
@@ -182,11 +182,19 @@ class getTrafficInfo(object):
except IndexError:
pass
self.output.append(random.choice(self.sarcasticMessage))
self.output.append('END')
# self.output.append('END')
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()
for i in inst.output:
print(i)
# inst = getTrafficInfo.getTrafficURL('M62').findIncidents() \
# .getIncidentInformation().generateOutput()
# for i in inst.output:
# print(i)

View File

@@ -0,0 +1,143 @@
import json
import os
import glob
import sys
from datetime import datetime
import requests
import base64
sys.path.append(os.getcwd())
import pullTrafficInfo
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:
congestionMessageEnd = i
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}')
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}"""
jsonPayload = json.loads(payload)
timeNow = datetime.now().timestamp()
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'])
newJsonPayload = json.dumps(jsonPayload)
# print(newJsonPayload)
with open(f'{os.getcwd()}/slackURL.json', 'r') as slackURL:
url = json.load(slackURL)
url = base64.b64decode(url.encode()).decode()
querystring = {"": ""}
headers = {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.17.1',
'Accept': '*/*',
'Cache-Control': 'no-cache',
'Postman-Token': 'a4b63f2d-43b1-4ce7-898a-2fd03952ad7d,46ba7deb-472c-4328'
'-a261-9c2000da2f11',
'Host': 'hooks.slack.com',
'Accept-Encoding': 'gzip, deflate',
'Content-Length': '1277',
'Connection': 'keep-alive',
'cache-control': 'no-cache',
}
response = requests.request(
'POST',
url,
data=newJsonPayload.encode(),
headers=headers,
params=querystring,
)
print(response.text)

View File

@@ -0,0 +1 @@
"aHR0cHM6Ly9ob29rcy5zbGFjay5jb20vc2VydmljZXMvVDAyRDExQjRLL0JQQkFGUFA3WC9kTk8xRTBCaVg1WUlHMWgxblZtU0E3c28="

View File

@@ -0,0 +1,23 @@
import requests
url = "https://hooks.slack.com/services/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"
querystring = {"":""}
payload = "{\"attachments\": [\n {\n \"title\": \"Let's check the latest updates from Highways England as of 22:30:12! \\ud83d\\ude93\\ud83d\\udea8\",\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\": \"There are currently 1 reported congestion incidents on the M62 😱\",\n\t\t \"value\": \"Location : The M62 westbound exit slip at junction J32 .\\n Reason : Congestion.\\n Status : Currently Active.\\n Return To Normal : Normal traffic conditions are expected between 23:00 and 23:15 on 12 October 2019.\\n Delay : There are currently delays of 10 minutes against expected traffic.\",\n\t\t \"short\": false\n\t\t \t},\n\t\t \t{\n\t\t \"title\": \"Accident Incidents\",\n\t\t \"value\": \"There are currently no reported accident incidents on the M62 \\ud83e\\udd14\",\n\t\t \"short\": false\n\t\t \t},\n\t \t\t{\n\t\t \"title\": \"\",\n\t\t \"value\": \"Hey Andy, have you thought about getting the train? \\ud83d\\ude85 \",\n\t\t \"short\": false\n\t\t \t}\n\t \t]\n }\n ]\n}"
headers = {
'Content-Type': "application/json",
'User-Agent': "PostmanRuntime/7.17.1",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "a4b63f2d-43b1-4ce7-898a-2fd03952ad7d,46ba7deb-472c-4328-a261-9c2000da2f11",
'Host': "hooks.slack.com",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "1277",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload.encode(), headers=headers, params=querystring)
print(response.text)