updating receiveFromSQS.py

This commit is contained in:
2019-10-08 02:42:29 +01:00
parent 713084c336
commit 13b9a6d039
5 changed files with 190 additions and 114 deletions

View File

@@ -37,6 +37,7 @@ class getTrafficInfo(object):
Generate a sequential list for an output:
getTrafficInfo.getOutput()
"""
def __init__(self, browser, motorway):
super(getTrafficInfo, self).__init__()
self.browser = browser
@@ -49,8 +50,11 @@ class getTrafficInfo(object):
@classmethod
def getTrafficURL(cls, motorway, driver='chrome'):
url = ('https://www.trafficdelays.co.uk/' + motorway.lower()
+ '-traffic-delays')
url = (
'https://www.trafficdelays.co.uk/'
+ motorway.lower()
+ '-traffic-delays'
)
browser = getTrafficInfo.getWebDriver(driver, url)
getTrafficInfo.verfiyMotorway(browser, motorway)
return cls(browser, motorway)
@@ -72,13 +76,16 @@ class getTrafficInfo(object):
@staticmethod
def verfiyMotorway(browser, motorway):
try:
verify = browser.find_element_by_xpath('/html/body/div[1]/div/div/'
'div/section/div/div/'
'div[1]')
if verify.text in ('It looks like the link pointing here '
'was faulty. Maybe try searching?'):
raise Exception(f'No traffic information available for'
f' {motorway}')
verify = browser.find_element_by_xpath(
'/html/body/div[1]/div/div/' 'div/section/div/div/' 'div[1]'
)
if verify.text in (
'It looks like the link pointing here '
'was faulty. Maybe try searching?'
):
raise Exception(
f'No traffic information available for' f' {motorway}'
)
except NoSuchElementException:
pass
@@ -86,8 +93,9 @@ class getTrafficInfo(object):
self.incidentBrowser = []
for item in self.incidentTypes:
xpath = f'//*[@id="{item}"]'
self.incidentBrowser.append(self.browser.find_element_by_xpath
(xpath))
self.incidentBrowser.append(
self.browser.find_element_by_xpath(xpath)
)
self.getIncidentHTML()
return self
@@ -99,8 +107,9 @@ class getTrafficInfo(object):
def getIncidentCount(self):
self.incidentCount = []
for item, i in zip(self.incidentBrowser,
range(0, len(self.incidentHTML))):
for item, i in zip(
self.incidentBrowser, range(0, len(self.incidentHTML))
):
self.incidentCount.append(self.incidentHTML[i].count('<li>'))
return self
@@ -114,44 +123,60 @@ class getTrafficInfo(object):
def generateOutput(self):
self.getIncidentCount()
self.output = []
self.sarcasticMessage = [(f'Hey Andy, have you thought about getting'
' the train?'
+ emoji.emojize(f' :bullettrain_front:',
use_aliases=True)),
(f'Hey Andy, maybe flying would be quicker?'
+ emoji.emojize(f' :helicopter:',
use_aliases=True)),
(f'Don\'t fret, Andy can always work from'
' home!'
+ emoji.emojize(f' :house_with_garden:',
use_aliases=True))]
self.sarcasticMessage = [
(
f'Hey Andy, have you thought about getting'
' the train?'
+ emoji.emojize(f' :bullettrain_front:', use_aliases=True)
),
(
f'Hey Andy, maybe flying would be quicker?'
+ emoji.emojize(f' :helicopter:', use_aliases=True)
),
(
f'Don\'t fret, Andy can always work from'
' home!'
+ emoji.emojize(f' :house_with_garden:', use_aliases=True)
),
]
currentTime = datetime.now().strftime('%H:%M:%S')
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}!'
' :police_car::rotating_light:',
use_aliases=True))
for item, i in zip(self.incidentCount,
range(0, len(self.incidentTypes))):
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}!'
' :police_car::rotating_light:',
use_aliases=True,
)
)
for item, i in zip(
self.incidentCount, range(0, len(self.incidentTypes))
):
if item == 0:
self.output.append(emoji.emojize
(f'There are currently no'
' reported'
f' {self.incidentTypes[i]} incidents on'
f' the {self.motorway} :thinking_face:',
use_aliases=True))
self.output.append(
emoji.emojize(
f'There are currently no'
' reported'
f' {self.incidentTypes[i]} incidents on'
f' the {self.motorway} :thinking_face:',
use_aliases=True,
)
)
pass
else:
self.output.append(emoji.emojize
(f'There are currently'
f' {self.incidentCount[i]} reported'
f' {self.incidentTypes[i]} incidents'
f' reported on the {self.motorway}'
f' :scream:',
use_aliases=True))
self.output.append(
emoji.emojize(
f'There are currently'
f' {self.incidentCount[i]} reported'
f' {self.incidentTypes[i]} incidents'
f' reported on the {self.motorway}'
f' :scream:',
use_aliases=True,
)
)
self.output.append(self.incidentInformation[0][i][0])
self.output.append(random.choice(self.sarcasticMessage))
self.output.append('END')