from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from datetime import datetime import re import emoji import random class getTrafficInfo(object): """ Pulls the latest traffic information for a major road or motorway from trafficdelays.co.uk. Parameters: motorway: str -> a string containing the A-road or motorway driver: str -> the browser/driver to run under (chrome default) Usage: ** required ** Create a driver for your motorway: getTrafficInfo.getTrafficURL(motorway, driver) **options** Get information for incident types: getTrafficInfo.findIncidents() Get HTML from the page: getTrafficInfo.getIncidentHTML() Get a count of how many incidents for each type: getTrafficInfo.getIncidentCount() Get the text detailing the incident: getTrafficInfo.getIncidentInformation() Generate a sequential list for an output: getTrafficInfo.getOutput() """ def __init__(self, browser, motorway): super(getTrafficInfo, self).__init__() self.browser = browser self.motorway = motorway self.incidentTypes = ['congestion', 'accident'] # def __del__(self): # print('Quitting') # self.browser.quit() @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, motorway) @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): self.incidentBrowser = [] for item in self.incidentTypes: xpath = f'//*[@id="{item}"]' self.incidentBrowser.append(self.browser.find_element_by_xpath (xpath)) self.getIncidentHTML() return self def getIncidentHTML(self): self.incidentHTML = [] for item in self.incidentBrowser: self.incidentHTML.append(item.get_attribute('innerHTML')) return self def getIncidentCount(self): self.incidentCount = [] for item, i in zip(self.incidentBrowser, range(0, len(self.incidentHTML))): self.incidentCount.append(self.incidentHTML[i].count('