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'])