Example #1
0
def post_entries(url, entrylist):
    entrycount = 0
    for entry in entrylist:
        postdata = entry.get_data()
        try:
            http.post_portaln(url, config.PORTALN_ACTION["post_food"], postdata)
            entrycount += 1
        except http.HTTPException as e:
            raise
    return entrycount
Example #2
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)