示例#1
0
文件: bot.py 项目: tutorly/sherlock
    def run(self):
        '''This is the method that drives the entire program.'''
        # Ask the user what mode they want to run the bot in.
        self.currentMode = self.modeSelect()

        # Last output before loop
        print(f'Starting bot in: {self.currentMode}')

        # While loop.
        while (1):
            print('-------------')

            # Let the scraper scrape for new cases.
            self.scraper.scrape()

            # Check if there is a new case from that scrape.
            if self.validator.checkForNewCase():
                print('Sherlock has found a new case.')

                # Logic to decide who gets emails when there is a new case.
                if self.currentMode == Environment.TEST:
                    Courier.sendEmailsToAdminOnly(
                        'Covid Case sent to admin emails.')

                if self.currentMode == Environment.PROD:
                    # Courier.sendEmailsToEveryoneInMailingList('New covid-19 case confirmed on campus.')
                    pass

            self.cleanUp()
            print('-------------')

            # Sleep for as long as indicated in self.loopInterval.
            time.sleep(self.loopInterval)
示例#2
0
    def cleanLists(self):
        '''The goal of this function is to make all of the data uniform.'''
        # Check to make sure length is the same for both lists
        if len(self.cases) != len(self.dates):
            msg = f'ERROR. Cases list length: {len(self.cases)}. Dates list length: {len(self.dates)}'
            Courier.sendEmailsToAdminOnly(msg)
            sys.exit('Bot stopped due to length issues.')

        # Look for all \xa0 characters and remove them
        for i in range(0, len(self.cases) - 1):
            self.cases[i] = self.cases[i].replace(u'\xa0', ' ').strip()
            self.dates[i] = self.dates[i].replace(u'\xa0', ' ').strip()
示例#3
0
 def getHTMLFromURL(self, url):
     '''Makes a server request for the given url, ensures the response is ok, then returns a formatted html object.'''
     try:
         response = requests.get(
             url
         )  # Attempt to make a request to the server and wait for the response.
         if response.status_code != 200:  # If the response is not 200 (OK), wait and try again (recursively).
             print(
                 f'Could not connect to {url}. Retrying in {self._timeout_duration} seconds...'
             )
             time.sleep(self._timeout_duration)
             Courier.sendEmailsToAdminOnly(
                 'Could not connect to SPU website.'
             )  # TODO Take this out at some point.
             self.getHTMLFromURL(
                 url
             )  # TODO Make sure we don't overflow the call stack (TIMEOUT_DURATION must be reasonable)
         else:
             return html.fromstring(
                 response.content
             )  # Pull the content out of the response and format it as an HTML object
     except ConnectionError:
         Courier.sendEmailsToAdminOnly('Connection Error!')