diff --git a/slack-bot/traffic-scraper/prd/12-10-2019_03:23:21.json b/slack-bot/traffic-scraper/prd/12-10-2019_03:23:21.json deleted file mode 100644 index 93970fb..0000000 --- a/slack-bot/traffic-scraper/prd/12-10-2019_03:23:21.json +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/slack-bot/traffic-scraper/prd/12-10-2019_16:13:43.json b/slack-bot/traffic-scraper/prd/12-10-2019_16:13:43.json deleted file mode 100644 index 4929ad2..0000000 --- a/slack-bot/traffic-scraper/prd/12-10-2019_16:13:43.json +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/slack-bot/traffic-scraper/prd/__pycache__/pullTrafficInfo.cpython-37.pyc b/slack-bot/traffic-scraper/prd/__pycache__/pullTrafficInfo.cpython-37.pyc index 747d955..07d4ba9 100644 Binary files a/slack-bot/traffic-scraper/prd/__pycache__/pullTrafficInfo.cpython-37.pyc and b/slack-bot/traffic-scraper/prd/__pycache__/pullTrafficInfo.cpython-37.pyc differ diff --git a/slack-bot/traffic-scraper/prd/pullTrafficInfo.py b/slack-bot/traffic-scraper/prd/pullTrafficInfo.py index 5c7fd0f..a7614d1 100644 --- a/slack-bot/traffic-scraper/prd/pullTrafficInfo.py +++ b/slack-bot/traffic-scraper/prd/pullTrafficInfo.py @@ -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,7 +142,7 @@ 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}!?' @@ -182,11 +184,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) diff --git a/slack-bot/traffic-scraper/prd/slackBot.py b/slack-bot/traffic-scraper/prd/slackBot.py new file mode 100644 index 0000000..aa5c438 --- /dev/null +++ b/slack-bot/traffic-scraper/prd/slackBot.py @@ -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']) diff --git a/slack-bot/traffic-scraper/prd/test.py b/slack-bot/traffic-scraper/prd/test.py new file mode 100644 index 0000000..7641918 --- /dev/null +++ b/slack-bot/traffic-scraper/prd/test.py @@ -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'])