Esempio n. 1
0
def main():
    # Starting Log object to save at files/ later
    log = Log(PATH, 'users')
    try:
        # Starting reddit
        reddit = get_reddit_credentials(PATH)
        # Reading new messages.
        new = read_messages(SEND_FLAG, reddit)
        if not new:
            print("no new users")
            log.save("no-users")
            exit(0)
        list_users = load_users(PATH)  # get the stored users
        log.old = len(list_users)
        # if not list_users:
        #     raise Exception('LoadingUserError')
        sub, unsub = new  # new is a list with new users, and user unsubscribing
        log.actual = len(sub)
        log.new = len(unsub)
        list_users = remove_users(list_users,
                                  unsub)  # updates the list, removing
        for u in sub:
            u.send_welcome(SEND_FLAG, SIGNATURE,
                           reddit)  # send the new users a welcome
            list_users.append(u)

        save_users(list_users, PATH)  # stores it again in the file.

        log.update_resume(new_users=len(sub), total_users=len(list_users))
        log.save('success')

    except Exception as e:
        print("Exception occurred: ", e)
        log.save('error')
        log.error(e)
def main():
    # Starting Log object. Saves a log after execution in logs/
    print("Starting competitions module.")
    log = Log(PATH, 'competition')
    try:

        # Getting new competitions from WCA's API
        print("Reading new competitions.")
        actual = get_new_competitions(LINK_API, PATH)
        log.actual = len(actual)
        print("Found %d competitions at WCA" % len(actual))

        # Getting competitions from files/competitions.json
        print("Reading old competitions.")
        old = load_competitions(PATH)
        log.old = len(old)
        print("Found %d competitions at json" % len(old))

        # Comparing to get which competition was just announced
        print("Comparing both...")
        new = compare_competitions(old, actual)
        log.new = len(new)
        print("Found %d new competitions" % len(new))

        if len(new) != 0:
            # Loading users and starting reddit, and then sending.
            print("Loading users.")
            users = load_users(PATH)
            print("Starting reddit.")
            reddit = get_reddit_credentials(PATH)
            print("Matching competitions...")
            match_competitions_users(new, users, SEND_FLAG, SIGNATURE, reddit)

        # updating competitions on files/competitions.json
        print("Saving competitions.json file.")
        save_competitions(actual, PATH)

        # saving log
        log.update_resume(new_competitions=len(new),
                          total_competitions=len(actual))
        log.save('success')

    except Exception as e:
        print("Exception occurred: ", e)
        log.error(e)
        log.save('error')

    print("Done!")
    return