def _get_mentions_in_submission(counter, submission_):
    """Finds mentions of a course in a submission's title, selftext, and comments.

    :param counter: counter to print in table row
    :type counter: int
    :param submission_: a praw submission object
    :type submission_: praw.objects.Submission
    :return: a PostWithMentions object which has the post ID and a list of strings of mentions
    :rtype: PostWithMentions
    """
    mentions_list = []
    mentions_list.extend(_get_mentions_in_string(submission_.title))

    mentions_list.extend(_get_mentions_in_string(submission_.selftext))

    flat_comments = praw.helpers.flatten_tree(submission_.comments)
    for comment in flat_comments:
        if comment.author is None or comment.author.name == 'ucsc-class-info-bot':
            continue
        mentions_list.extend(_get_mentions_in_string(comment.body))

    mentions_list = _remove_list_duplicates_preserve_order(mentions_list)

    print('{num}{_}{id}{_}{author}{_}{title}{_}{mentions}'
          .format(num = trunc_pad(str(counter), 'num'),
                  id = trunc_pad(submission_.id, "id"),
                  author = trunc_pad(submission_.author.name, "author"),
                  title = trunc_pad(submission_.title, "title"),
                  mentions = mentions_list,
                  _ = '  '))

    if not mentions_list:  # if list is empty
        return None
    else:
        return PostWithMentions(submission_.id, mentions_list)
def find_mentions(reddit, num_posts_, running_on_own = False):
    """Finds and saves to disk course mentions in new posts on /r/UCSC.

    :param running_on_own: whether file is being ran by itself or imported by reddit_bot.py
    :type running_on_own: bool
    :param reddit: authorized reddit praw object
    :type reddit: praw.Reddit
    :param num_posts_:
    :type num_posts_: int
    :return: list of post with found mentions
    :rtype: list
    """

    # use this to find mentions in only one post
    # tools.save_found_mentions([_get_mentions_in_submission(0, reddit.get_submission(submission_id = "49h1o9"))])
    # return

    print('{num}{_}{id}{_}{author}{_}{title}{_}mentions'
          .format(num = trunc_pad("#", 'num'),
                  id = trunc_pad("id"),
                  author = trunc_pad("author"),
                  title = trunc_pad("title"),
                  _ = '  ').upper())

    subreddit = reddit.get_subreddit('ucsc')
    list_of_posts_with_mentions = []

    for counter, submission in enumerate(subreddit.get_new(limit = num_posts_), start = 1):
        found_mentions = _get_mentions_in_submission(counter, submission)
        if found_mentions is not None:
            list_of_posts_with_mentions.append(found_mentions)

    if running_on_own:
        tools.save_found_mentions(list_of_posts_with_mentions)

    print("------------------------------")
    for post_with_mention in list_of_posts_with_mentions:
        print(str(post_with_mention))

    return list_of_posts_with_mentions
def _print_csv_row(submission_, action, mentions_current, mentions_previous):
    """Prints a CSV row to stdout to be used as a log about what happened with a comment.

    :param submission_: Submission object that you are commenting on
    :type submission_:  praw.objects.Submission
    :param action: string describing the action taken
    :type action: str
    :param mentions_current: list of current class mentions
    :type mentions_current: list
    :param mentions_previous: list of class mentions last known about
    :type mentions_previous: list
    """
    print(  # I have put the string on it's own line b/c PyCharm's formatter and PEP inspector want different things
        '{id}{_}{author}{_}{title}{_}{action}{_}{mentions_current}{_}{mentions_previous}'
            .format(
            id = trunc_pad(submission_.id, "id"),
            author = trunc_pad(submission_.author.name, "author"),
            title = trunc_pad(submission_.title, "title"),
            action = trunc_pad(action, "action"),
            mentions_current = mentions_current,
            mentions_previous = mentions_previous,
            _ = '  '))
    :param running_on_own: whether file is being ran by itself or imported by reddit_bot.py
    :type running_on_own: bool
    :param reddit: authorized reddit praw object
    :type reddit: praw.Reddit
    :param new_mentions_list: list of mentions
    :type new_mentions_list: list
    """
    if new_mentions_list:
        new_mention = new_mentions_list.pop(0)
    else:
        print("No more mentions.")
        return
    _post_comment_helper(new_mention, reddit)
    if running_on_own:
        tools.save_found_mentions(new_mentions_list)
    post_comments(new_mentions_list, reddit, running_on_own)


existing_posts_with_comments = tools.load_posts_with_comments()
db = db_core.load_database()

if __name__ == "__main__":
    print('{id}{_}{author}{_}{title}{_}{action}{_}current mentions{_}previous mentions'
          .format(id = trunc_pad("id"),
                  author = trunc_pad("author"),
                  title = trunc_pad("title"),
                  action = trunc_pad("action"),
                  _ = '  '))
    post_comments(tools.load_found_mentions(), tools.auth_reddit(), running_on_own = True)