58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
from selenium import webdriver
|
|
from selenium.common.exceptions import NoSuchElementException
|
|
from datetime import datetime
|
|
import re
|
|
|
|
|
|
class getTrafficInfo(object):
|
|
"""docstring for getTrafficInfo"""
|
|
def __init__(self, browser):
|
|
super(getTrafficInfo, self).__init__()
|
|
self.browser = browser
|
|
self.incidentTypes = ['congestion', 'accident']
|
|
|
|
@classmethod
|
|
def getTrafficURL(cls, motorway, driver='chrome'):
|
|
url = ('https://www.trafficdelays.co.uk/' + motorway.lower()
|
|
+ '-traffic-delays')
|
|
browser = getTrafficInfo.getWebDriver(driver, url)
|
|
getTrafficInfo.verfiyMotorway(browser, motorway)
|
|
return cls(browser)
|
|
|
|
@staticmethod
|
|
def getWebDriver(driver, url):
|
|
driver = driver.lower()
|
|
if driver == 'chrome':
|
|
# Prepare the option for the Chromedriver
|
|
options = webdriver.ChromeOptions()
|
|
options.add_argument('headless')
|
|
# Start the Chromedriver
|
|
browser = webdriver.Chrome(options=options)
|
|
browser.get(url)
|
|
return browser
|
|
else:
|
|
raise Exception(f'Driver {driver} not supported')
|
|
|
|
@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}')
|
|
except NoSuchElementException:
|
|
pass
|
|
|
|
def findIncidents(self):
|
|
for item in self.incidentTypes:
|
|
xpath = f'//*[@id="{item}"]'
|
|
setattr(self, f'item', self.browser.find_element_by_xpath(xpath))
|
|
print(self.item.text)
|
|
return self
|
|
|
|
|
|
inst = getTrafficInfo.getTrafficURL('M62').findIncidents()
|