Files
python-VM/slack-bot/traffic-scraper/prd/slackBot1.py
2019-12-05 03:56:49 +00:00

145 lines
4.7 KiB
Python

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)