Ejemplo n.º 1
0
def homepage():

    if request.method == "GET":
        return render_template("index.html")

    if request.method == "POST":
        server_utils.log(request.form)
        name = request.form["name"]
        number = request.form["phone"]
        time = request.form["time"]
        submit_type = request.form["submit_type"]

        twilio_client = router.twilio_client
        err_code = server_utils.validate_inputs(name, number, twilio_client,
                                                time, submit_type)

        if err_code == server_utils.NO_ERR:
            pass
        else:
            err_msg = SERVER_ERR_MSGS[err_code]
            server_utils.log(
                "Error code {0} with inputs NAME: {1} | NUMBER: {2} | TIME: {3} | SUBMIT_TYPE: {4}"
                .format(err_code, name, number, time, submit_type))
            router.notify_matt(submit_type, name, number, time, FAILED)
            return render_template(ERR_VIEW, err_msg=err_msg)

        timestr = server_utils.parse_time(time)
        if submit_type == server_utils.TRY:
            router.run_once(name, number)
            if "NOTIFY" in os.environ.keys(
            ) and os.environ["NOTIFY"] == NOTIFY:  # short-circuit check
                router.notify_matt(submit_type, name, number, time, SUCCESS)
            return render_template(TRY_VIEW, number=number)
        if submit_type == server_utils.SUB:
            router.add_db_entry(name, number, timestr)
            router.send_sub_confirmation(name, number, time)
            if "NOTIFY" in os.environ.keys(
            ) and os.environ["NOTIFY"] == NOTIFY:
                router.notify_matt(submit_type, name, number, time, SUCCESS)
            return render_template(SUB_VIEW, name=name)

    return render_template("index.html")
Ejemplo n.º 2
0
    def __init__(self, name, phone_num, comic_num):

        try:
            int(comic_num)
        except ValueError:
            server_utils.log("Comic number needs to be able to be converted to int when creating MMS object")
            return

        try:
            assert isinstance(name, str), "Name needs to be a string when creating MMS object"
            assert isinstance(phone_num, str), "Phone number needs to be a string when creating MMS object"
        except AssertionError as e:
            server_utils.log(e)
            return

        self.name = name
        self.phone_num = phone_num
        self.comic_num = int(comic_num)
        self.comic_url = None
        self.message = ''
        self.updated = False
Ejemplo n.º 3
0
def find_comic_url(comic_num):
    page_url = MAIN_SITE + str(comic_num) + "/"
    page = urlopen(page_url)
    soup = BeautifulSoup(page, 'html.parser')

    try:
        comic_div = soup.find('div', {'id': 'comic'})
        img_url = comic_div.find('img').attrs['src']
    except Exception as e:
        # TODO replace with some better way to report error
        server_utils.log(
            "Error with finding img_url. Maybe xkcd page format changed. Listed error:"
        )
        server_utils.log(str(e))

    img_url = "https:" + img_url
    proper_form = check_url(img_url)

    if proper_form:
        return img_url
    else:
        return None
Ejemplo n.º 4
0
def run(timestr):

    server_utils.log("RUNNING with timestr " + timestr)

    # Retrieve most_recent_comic_num
    mrcn = scrape_utils.most_recent_comic_num()

    # Send to db, update if necessary
    db_utils.update_mrcn(mrcn)

    # Request users at certain time slot; db should automatically update here
    # Should receive a list of MMS objects with name, phone #, comic #, (empty) caption
    mms_list = db_utils.retrieve_mms_list(timestr)

    # Call scraper and replace all comic #s with comic url and add in caption
    for mms in mms_list:
        # If one mms update errors, the others should still send
        try:
            comic_num = mms.comic_num
            comic_url = scrape_utils.find_comic_url(comic_num)
            comic_caption = scrape_utils.find_comic_caption(comic_num)
            mms.update(comic_url, comic_caption)
        except Exception as e:
            server_utils.log(e)
            continue

    # Call twilio and send all comics to phone # with greeting + caption
    for mms in mms_list:
        # If one message sent errors, the others should still send
        try:
            if mms.name == "YEETchen":
                twilio_client.send_mms(mms)
            else:
                twilio_client.send_captionless_mms(mms)
        except Exception as e:
            server_utils.log(e)
            continue
Ejemplo n.º 5
0
import router
import clock_utils, server_utils


def execute_job():
    timestr = clock_utils.get_time()
    router.run(timestr)


server_utils.log("Scheduler executing")
execute_job()