from bs4 import BeautifulSoup
from selenium import webdriver
import emoji
from datetime import datetime
import re

url = 'https://www.trafficdelays.co.uk/m62-traffic-delays/'
# prepare the  option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')

# start the chrome driver
browser = webdriver.Chrome(options=options)
browser.get(url)
html = browser.page_source


congestion = browser.find_element_by_xpath('//*[@id="congestion"]')
accident = browser.find_element_by_xpath('//*[@id="accident"]')

congestionText = congestion.text
congestionHTML = congestion.get_attribute('innerHTML')
congestionCount = congestionHTML.count('<li>')
pattern = r".*\<li\>.*title=\".*\".*\>((.|\n)*?)\<br\>"
congestionRegexExtraction = re.findall(pattern, congestionHTML)

accidentText = accident.text
accidentHTML = accident.get_attribute('innerHTML')
accidentCount = accidentHTML.count('<li>')
accidentRegexExtraction = re.findall(pattern, accidentHTML)


def printBreak():
    print('\n')


currentTime = datetime.now().strftime('%H:%M:%S')

print(emoji.emojize('Did someone say M62 :anguished:!? Let'
                    '\'s check the latest updates from Highways'
                    f' England! as of {currentTime}'
                    ':police_car::rotating_light:',
                    use_aliases=True))
printBreak()
if congestionCount == 0:
    print(emoji.emojize(f'There are currently no reported congestions on the'
                        f' M62 :thinking_face:', use_aliases=True))
if congestionCount != 0:
    print(emoji.emojize(f'There are currently {congestionCount} incident(s)'
                        f' on the M62 :scream:', use_aliases=True))
    for i in range(0, congestionCount):
        print(congestionRegexExtraction[i][0] + '\n')
printBreak()
if accidentCount == 0:
    print(emoji.emojize(f'There are currently no reported accidents on the'
                        f' M62 :thinking_face:', use_aliases=True))
if accidentCount != 0:
    print(emoji.emojize(f'There are currently {accidentCount} incident(s)'
                        f' on the M62 :scream:', use_aliases=True))
    for i in range(0, accidentCount):
        print(accidentRegexExtraction[i][0] + '\n')
printBreak()
print(f'Hey Andy, have you thought about getting the train?'
      + emoji.emojize(f' :bullettrain_front:', use_aliases=True))
print(f'Hey Andy, maybe flying would be quicker?'
      + emoji.emojize(f' :helicopter:', use_aliases=True))
print(f'Don\'t fret, he can always work from home!'
      + emoji.emojize(f' :house_with_garden:', use_aliases=True))

browser.quit()
