Exemple #1
0
    def send_fatal(self, error):
        if config.CONFIG["mail_enabled"]:
            nl = config.CONFIG["mail_newline"]
            mail_subject = "FetchFood FAILED!"
            mail_content = ("A fatal error occurred at:" + nl + nl +
                            datehelper.to_string(datehelper.current_date(), datehelper.PRECISION_DATE) + nl +
                            datehelper.to_string(datehelper.current_date(), datehelper.PRECISION_TIME) + nl + nl +
                            "FATAL ERROR: " + str(error))

            if self.has_error:
                mail_content += (nl + nl + "These additional (non-fatal) errors occurred during program execution:" + nl + self.get_errors_compiled(nl))
            print mail_content
            mail.sendmail(mail_subject, mail_content)
        sys.exit(1)
Exemple #2
0
    def generate_entry(self, entry_string):
        if datehelper.is_weekday(entry_string):
            new_weekday = datehelper.weekday_to_weekdaynumber(entry_string, self.weekday)
            if new_weekday > self.weekday:
                self.weekday = new_weekday
                self.date = datehelper.weekday_to_date(self.init_date, self.weekday)
                self.date_string = datehelper.to_string(self.date)
            return None

        elif (not re.match(FoodEntryGenerator.REGEX_WEEK, entry_string)
            and not re.match(FoodEntryGenerator.REGEX_GOODMEAL, entry_string)
            and not re.match(FoodEntryGenerator.REGEX_NEWLINE, entry_string)
            and not re.match(FoodEntryGenerator.REGEX_INFO, entry_string)):
            typeobject = re.match(FoodEntryGenerator.REGEX_TYPE, entry_string)

            if typeobject is not None:
                try:
                    type_ = typeobject.group().strip()
                except AttributeError:
                    type_ = config.FOOD_UNKNOWN_TYPE
            else:
                type_ = config.FOOD_DEFAULT_TYPE

            content = entry_string.replace(type_, "").strip()

            if "*" in content:
                return FoodEntry(self.date_string, type_, content.replace("*", ""), True)
            else:
                return FoodEntry(self.date_string, type_, content, False)

        elif re.match(FoodEntryGenerator.REGEX_INFO, entry_string):
            self.generated_info = re.match(FoodEntryGenerator.REGEX_INFO, entry_string).group().replace("*=", "").strip()

        else:
            return None
Exemple #3
0
def main():
    start_time = time.time()
    errorhandler = error.ErrorHandler()
    try:
        entrylist = generate_food_entries(config.TARGET_URL)
    except http.HTTPException as e:
        errorhandler.add_error(e, config.ERROR_FATAL["postback"])

    entrycount = 0
    for url in config.PORTALN_POST_URLS:
        try:
            http.post_portaln(url, config.PORTALN_ACTION["clear_table"]) #Clear database table.
        except http.HTTPException as e:
            errorhandler.add_error(e, config.ERROR_FATAL["clear_table"])
        try:
            entrycount = post_entries(url, entrylist)
        except http.HTTPException as e:
            errorhandler.add_error(e, config.ERROR_FATAL["post_entry"])

    total_time = time.time() - start_time

    nl = config.CONFIG["mail_newline"]
    mail_content = ("fetchfood.py completed at:" + nl + nl +
    datehelper.to_string(datehelper.current_date(), datehelper.PRECISION_DATE) + nl +
    datehelper.to_string(datehelper.current_date(), datehelper.PRECISION_TIME) + nl + nl +
    "Entries posted: " + str(entrycount) + nl +
    "Execution time: %.1fs") % total_time

    if errorhandler.has_error:
        print("error")
        mail_content += nl + nl + "These (non-fatal) errors occurred during execution:" + nl + errorhandler.get_errors_compiled()

    if config.CONFIG["mail_enabled"]:
        mail.sendmail("FetchFood Completed!", mail_content)
    elif config.DEBUG:
        print(mail_content)
    sys.exit(0)
Exemple #4
0
 def __init__(self, init_date):
     self.init_date = init_date
     self.date = init_date
     self.date_string = datehelper.to_string(self.date)
     self.weekday = 0
     self.generated_info = None