From 8320c7ec0911f9926b99ae0d32b45f6af9976246 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 5 Oct 2019 20:20:50 +0100 Subject: [PATCH] pullTrafficInfo completed --- .../pullTrafficInfo.cpython-37.pyc | Bin 0 -> 4913 bytes slack-bot/pullTrafficInfo.py | 113 +++++++++++++++++- slack-bot/sendToSQS.py | 7 ++ 3 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 slack-bot/__pycache__/pullTrafficInfo.cpython-37.pyc create mode 100644 slack-bot/sendToSQS.py diff --git a/slack-bot/__pycache__/pullTrafficInfo.cpython-37.pyc b/slack-bot/__pycache__/pullTrafficInfo.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e39ef707eafbf4fb2adff2f65bea62ea4391b5a4 GIT binary patch literal 4913 zcmai2TW=f372X>!qDVv2+7({oD|jyRRlbI2mA}H*@vQMz`39aV-y4q6 zUWEvo*-B5SN7Czd1OKzA8+)6!VN@8duSQ{*Fx3|<1lx&XDqqC{ERzF|gktD1j}5#% zJZ0>07E8T(5UW@ocw^R;@nE)-i_jY<4L@#-_8XP_BcAjILJ66)&g^TkL`in@Hi)AW zL_Oxmk@A9wy2R7RH>aOtvrp;$`6Fsc#amCt7)bdz(q|WE4oj+4i>n#$71@2soQkBn-y!NX|LQMLCOW<8=7VAHUd| zF@0l7Wcuap?N}>IGX2!2Lj3~nAo2qa&0=ai6jRtviPIT2xvm`8EP@m%?y`P-fRm5L;4tSX(dr;Pzpr_F84NShcZtv< zZ8|L_o+%s$jyjwj^^E-gqer4o_41TzBxHVA2~H8H1zyO)0NSiIH)8Qf4M*x_q`%a& zphh71unc|Cb9N_N7$`%T6XVD_woVMwn4s_46AP`wVZc4=!K1+3?45Q6irj-a!Ft+d z&(D#o7qdb#8VV^3AeWV07n-hIH!DrGH!IIHCacZxctq!4w=*XZVON&1Q7)k&f8@G; z=p~8k{=@k12hBcIyvbEO0HjGAHHR|Z6TV8CN$C0eH#@Ov4q@yI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x&57F5lNr|*3 z93vk>t(Rv*EL~qHYf$JC(CHd$DMajjqSp0%(MmUdjZRuwp!3E8ojLUx!6nv8e3X-e zR()M=!-%qCUXWx3F^Knqc1b@{(OyfCEqEv!{D(;BP`F95yUdQ_gRG1axl8{mXcwuI zS$Q3^@)|X7Q}Yfr^LS{8X&OZ!>sui*D)v7)^$GClK>Q^A^wd96?}FPj^`%s0+bubVoT zt*=p06yd)D6h#%+9mISTQoriDPexuiohi94kA2tG#Y?uViy-{H(2u#?1cO;MAzkpN zuP7JQiIBWXJw^>3f^1SlB4xE{CFP4S1lc(fV2ctbN$G;F*{JeL=o{tBaHB=btd^=P z)mnL}Tr8K$CB3FzX_rx}i6|Hi@;v+X*${t<3 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'] @classmethod @@ -17,7 +49,7 @@ class getTrafficInfo(object): + '-traffic-delays') browser = getTrafficInfo.getWebDriver(driver, url) getTrafficInfo.verfiyMotorway(browser, motorway) - return cls(browser) + return cls(browser, motorway) @staticmethod def getWebDriver(driver, url): @@ -47,11 +79,80 @@ class getTrafficInfo(object): pass def findIncidents(self): + self.incidentBrowser = [] for item in self.incidentTypes: xpath = f'//*[@id="{item}"]' - setattr(self, f'item', self.browser.find_element_by_xpath(xpath)) - print(self.item.text) + 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('
  • ')) + return self + + def getIncidentInformation(self): + self.incidentInformation = [] + pattern = r".*\.*title=\".*\".*\>((.|\n)*?)\" + for item in self.incidentHTML: + self.incidentInformation.append(re.findall(pattern, item)) + return self + + 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))] + currentTime = datetime.now().strftime('%H:%M') + 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)) + 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(self.incidentInformation[i][0][0]) + self.output.append(random.choice(self.sarcasticMessage)) return self -inst = getTrafficInfo.getTrafficURL('M62').findIncidents() +# inst = getTrafficInfo.getTrafficURL('A50').findIncidents() \ +# .getIncidentInformation().generateOutput() +# for i in inst.output: +# print(i) diff --git a/slack-bot/sendToSQS.py b/slack-bot/sendToSQS.py new file mode 100644 index 0000000..e5314eb --- /dev/null +++ b/slack-bot/sendToSQS.py @@ -0,0 +1,7 @@ +import boto3 +from pullTrafficInfo import getTrafficInfo + +session = boto3.Session(profile_name='plex-aws') +sqs = session.resource('sqs') + +queue = sqs.create_queue(QueueName='test', Attributes={'DelaySeconds': '5'})