Ejemplo n.º 1
0
def main():
    print "-----------------"
    print "| Running tests |"
    print "-----------------"
    unittest.main()
    notify_mac()
Ejemplo n.º 2
0
def sub_group():
    # Load the properties
    saved_props = load_properties()

    # Access token
    sublets_oauth_access_token = saved_props['sublets_oauth_access_token']

    # Access token expiration
    access_token_expiration = saved_props['access_token_expiration']

    # API App ID
    sublets_api_id = saved_props['sublets_api_id']

    # API App secret key
    sublets_secret_key = saved_props['sublets_secret_key']

    # ID of the FB group
    group_id = saved_props['group_id']

    # IDs of admins (unused right now, might remove later)
    admin_ids = saved_props['admin_ids']

    # FQL query for the group
    group_query = "SELECT post_id, message, actor_id FROM stream WHERE " + \
                  "source_id=" + group_id + " LIMIT 50"

    # Get current time
    now_time = time.time()

    # For logging purposes
    log("CURRENT CST TIMESTAMP: " + datetime.datetime.fromtimestamp(
        now_time - 21600).strftime('%Y-%m-%d %H:%M:%S'), Color.UNDERLINE)

    # Make sure the access token is still valid
    if access_token_expiration < now_time:
        sys.exit("API Token is expired")

    # Warn if the token's expiring soon
    if access_token_expiration - now_time < 604800:
        log("Warning - access token expires in less than a week", Color.RED)
        log("-- Expires on " + datetime.datetime.fromtimestamp(
            access_token_expiration).strftime('%Y-%m-%d %H:%M:%S'))

        # If you want it to automatically when it's close to exp.
        global extend_key
        extend_key = True

    # Extend the access token, default is ~2 months from current date
    if extend_key:
        extend_access_token(saved_props, sublets_oauth_access_token, sublets_api_id,
                            sublets_secret_key)

    # Log in, try to get posts
    graph = facepy.GraphAPI(sublets_oauth_access_token)

    # Make our first request, get the group posts
    group_posts = graph.fql(query=group_query)

    # Load the pickled cache of valid posts
    valid_posts = []
    log("Checking valid cache.", Color.BOLD)
    valid_posts = load_cache(valid_db, valid_posts)
    log('--Valid cache size: ' + str(len(valid_posts)), Color.BOLD)
    invalid_count = 0

    # Loop over retrieved posts
    for post in group_posts["data"]:

        # Important data received
        post_message = post['message']  # Content of the post
        post_id = post['post_id']  # Unique ID of the post

        # Unique ID of the person that posted it
        actor_id = post['actor_id']

        # Ignore mods and certain posts
        if int(actor_id) in admin_ids:
            log('\n--Ignored post: ' + post_id, Color.BLUE)
            continue

        # Boolean for tracking if the post is valid
        valid_post = True

        # Log the message details
        # log("\n" + post_message[0:75].replace('\n', "") + "...\n--POST ID: " +
        # str(post_id) + "\n--ACTOR ID: " + str(actor_id))

        # Check for pricing
        if not check_price_validity(post_message):
            valid_post = False
            log('----$', Color.RED)
            invalid_count += 1

        # Check for tag validity, including tags that say rooming and offering
        tags = get_tags(post_message)
        if not validate_tags(tags):
            valid_post = False
            log('----Tag', Color.RED)
            invalid_count += 1

        # Check post length.
        # Allow short ones if there's a craigslist link or parking
        if len(post_message) < 200 \
                and "craigslist" not in post_message.lower() \
                and not check_for_parking_tag(post_message):
            valid_post = False
            log('----Length', Color.RED)
            invalid_count += 1

        # Not a valid post
        if not valid_post:
            if dry_run:
                log("Dry - invalid deletion", Color.RED)
                log("--ID: " + post_id, Color.RED)
                log("--Message: " + post_message, Color.RED)
                log("\n")
            else:
                graph.delete(post_id)
        else:
            valid_posts.append(post_id)

    if not dry_run:
        log("Deleted " + str(invalid_count) + " invalid posts", Color.RED)

    # # Delete posts older than 30 days
    delete_old_posts(graph, group_id, admin_ids)

    # Save the updated caches
    log('Saving valid cache', Color.BOLD)
    save_cache(valid_db, valid_posts)

    save_properties(saved_props)

    # Done
    notify_mac()
Ejemplo n.º 3
0
def sub_group():
    # Load the properties
    saved_props = load_properties()

    # Access token
    sublets_oauth_access_token = saved_props['sublets_oauth_access_token']

    # Access token expiration
    access_token_expiration = saved_props['access_token_expiration']

    # API App ID
    sublets_api_id = saved_props['sublets_api_id']

    # API App secret key
    sublets_secret_key = saved_props['sublets_secret_key']

    # ID of the FB group
    group_id = saved_props['group_id']

    # IDs of admins (unused right now, might remove later)
    admin_ids = saved_props['admin_ids']

    # FQL query for the group
    group_query = "SELECT post_id, message, actor_id FROM stream WHERE " + \
                  "source_id=" + group_id + " LIMIT 50"

    # Get current time
    now_time = time.time()

    # For logging purposes
    log(
        "CURRENT CST TIMESTAMP: " +
        datetime.datetime.fromtimestamp(now_time -
                                        21600).strftime('%Y-%m-%d %H:%M:%S'),
        Color.UNDERLINE)

    # Make sure the access token is still valid
    if access_token_expiration < now_time:
        sys.exit("API Token is expired")

    # Warn if the token's expiring soon
    if access_token_expiration - now_time < 604800:
        log("Warning - access token expires in less than a week", Color.RED)
        log("-- Expires on " + datetime.datetime.fromtimestamp(
            access_token_expiration).strftime('%Y-%m-%d %H:%M:%S'))

        # If you want it to automatically when it's close to exp.
        global extend_key
        extend_key = True

    # Extend the access token, default is ~2 months from current date
    if extend_key:
        extend_access_token(saved_props, sublets_oauth_access_token,
                            sublets_api_id, sublets_secret_key)

    # Log in, try to get posts
    graph = facepy.GraphAPI(sublets_oauth_access_token)

    # Make our first request, get the group posts
    group_posts = graph.fql(query=group_query)

    # Load the pickled cache of valid posts
    valid_posts = []
    log("Checking valid cache.", Color.BOLD)
    valid_posts = load_cache(valid_db, valid_posts)
    log('--Valid cache size: ' + str(len(valid_posts)), Color.BOLD)
    invalid_count = 0

    # Loop over retrieved posts
    for post in group_posts["data"]:

        # Important data received
        post_message = post['message']  # Content of the post
        post_id = post['post_id']  # Unique ID of the post

        # Unique ID of the person that posted it
        actor_id = post['actor_id']

        # Ignore mods and certain posts
        if int(actor_id) in admin_ids:
            log('\n--Ignored post: ' + post_id, Color.BLUE)
            continue

        # Boolean for tracking if the post is valid
        valid_post = True

        # Log the message details
        # log("\n" + post_message[0:75].replace('\n', "") + "...\n--POST ID: " +
        # str(post_id) + "\n--ACTOR ID: " + str(actor_id))

        # Check for pricing
        if not check_price_validity(post_message):
            valid_post = False
            log('----$', Color.RED)
            invalid_count += 1

        # Check for tag validity, including tags that say rooming and offering
        tags = get_tags(post_message)
        if not validate_tags(tags):
            valid_post = False
            log('----Tag', Color.RED)
            invalid_count += 1

        # Check post length.
        # Allow short ones if there's a craigslist link or parking
        if len(post_message) < 200 \
                and "craigslist" not in post_message.lower() \
                and not check_for_parking_tag(post_message):
            valid_post = False
            log('----Length', Color.RED)
            invalid_count += 1

        # Not a valid post
        if not valid_post:
            if dry_run:
                log("Dry - invalid deletion", Color.RED)
                log("--ID: " + post_id, Color.RED)
                log("--Message: " + post_message, Color.RED)
                log("\n")
            else:
                graph.delete(post_id)
        else:
            valid_posts.append(post_id)

    if not dry_run:
        log("Deleted " + str(invalid_count) + " invalid posts", Color.RED)

    # # Delete posts older than 30 days
    delete_old_posts(graph, group_id, admin_ids)

    # Save the updated caches
    log('Saving valid cache', Color.BOLD)
    save_cache(valid_db, valid_posts)

    save_properties(saved_props)

    # Done
    notify_mac()
Ejemplo n.º 4
0
def main():
    print "-----------------"
    print "| Running tests |"
    print "-----------------"
    unittest.main()
    notify_mac()