Exemplo n.º 1
0
def double_check_link(reddit, last_switcharoo: SwitcharooLog, roo: Switcharoo):
    comment = roo.comment
    comment_url = parse.RedditURL(comment.permalink)
    comment_url.params['context'] = roo.context
    link = parse.parse_comment(comment.body)
    if not link.comment_id:
        if not comment:
            print(roo, f"https://reddit.com{comment.permalink}?context={roo.context}")
            input()
        new_link = parse.find_roo_comment(comment)
        if new_link:
            if new_link.comment_id:
                if comment_url.comment_id == new_link.comment_id:
                    return
                try:
                    new_link.params['context'] = str(int(comment_url.params.get('context', 0)) +
                                                     int(new_link.params.get('context', 0)))
                except ValueError:
                    print(f"Got {comment_url.params['context']} and {new_link.params['context']}, what should it be?")
                    new_link.params['context'] = int(input())
                print("Should", f"https://reddit.com{comment.permalink}?context={roo.context}", "be actually",
                      new_link.to_link(reddit), "?")
                print("(y/n/new_link)")
                option = input()
                if option == "n":
                    return
                elif option != "y":
                    new_link = parse.RedditURL(option)
                last_switcharoo.update(roo, thread_id=new_link.thread_id, comment_id=new_link.comment_id,
                                       context=new_link.params.get("context", link.params.get('context', 0)))
                return
        print(roo, f"https://reddit.com{comment.permalink}?context={roo.context}")
        print("Paste in a new link to replace otherwise enter to continue")
        option = input()
        if option:
            new_link = parse.RedditURL(option)
            last_switcharoo.update(roo, thread_id=new_link.thread_id, comment_id=new_link.comment_id,
                                   context=new_link.params.get("context", link.params.get('context', 0)))
Exemplo n.º 2
0
def check_errors(reddit, last_switcharoo: SwitcharooLog, roo, init_db=False, submission=None, comment=None):
    """
    Check the submission to make sure it is correct
    :param comment:
    :param init_db:
    :param last_switcharoo:
    :param reddit: PRAW reddit instance
    :param submission: post to check
    :return:
    """
    tracker = IssueTracker()

    if submission:
        # Ignore announcements
        if submission.distinguished:
            return tracker

        # Verify it is a link post (not a self post)
        if submission.is_self:
            # If meta, determine if it was incorrectly submitted as meta
            if not parse.is_meta_title(submission.title):
                if parse.only_reddit_url(submission.title, submission.selftext):
                    tracker.submission_is_meta = True
                    return tracker
            # Removed self submissions should be kept track of as well
            if submission.removed_by_category is not None or submission.banned_at_utc is not None \
                    or submission.selftext == "[deleted]":
                tracker.submission_deleted = True
            return tracker

        # Verify it is a link to a reddit thread
        # If not, assume it's a faulty submission and delete.
        if submission.domain[-10:] != "reddit.com":
            tracker.submission_not_reddit = True
            return tracker

        if init_db:
            print(f"Roo: {submission.title} by {submission.author}")

        # It's a roo, add it to the list of all roos

        submission_url = parse.RedditURL(submission.url)

        if submission.removed_by_category is not None or submission.banned_at_utc is not None \
                or submission.selftext == "[deleted]":
            tracker.submission_deleted = True

            # Temporary check
            # Forgive automoderator removals if they mean not sending a fix message
            if submission.banned_by == "AutoModerator":
                previous_roo = last_switcharoo.next_good(roo)
                if previous_roo:
                    previous_link = parse.parse_comment(previous_roo.comment.body)
                    # previous_link = parse.RedditURL(previous_link)
                    submission_url = parse.RedditURL(submission.url)
                    if previous_link.thread_id == submission_url.thread_id \
                            and previous_link.comment_id == submission_url.comment_id:
                        print(f"{roo.id} Previous comment was linked to this one, which was removed by automod")
                        tracker.submission_deleted = False
                    else:
                        print(
                            f"{roo.id} Banned by automoderator but previous comment isn't linked to it so leaving as is")

        # Some URLs may not pass the stricter check, probably because they did something wrong
        if not submission_url.is_reddit_url:
            tracker.submission_bad_url = True
            return tracker

        # Verify it contains context param
        if "context" not in submission_url.params:
            tracker.submission_lacks_context = True
            return tracker

        # Try to get the context value
        try:
            context = int(submission_url.params['context'])
        except (KeyError, ValueError):  # context is not in URL params or not a number
            tracker.submission_lacks_context = True
            return tracker

        # If we are in the middle of adding this to the db, add the context amount
        if init_db:
            last_switcharoo.update(roo, context=context)

        # Check if it has multiple ? in it (like "?st=JDHTGB67&sh=f66dbbbe?context=3)
        if submission.url.count("?") > 1:
            tracker.submission_multiple_params = True
            return tracker

        # Verify it doesn't contain a slash at the end (which ignores the URL params) (Issue #5)
        if submission.url.count("?"):
            if "/" in submission.url[submission.url.index("?"):]:
                tracker.submission_link_final_slash = True

        # If there was a comment in the link, make the comment object
        if submission_url.comment_id:
            comment = reddit.comment(submission_url.comment_id)
        else:  # If there was no comment in the link, take action
            tracker.submission_linked_thread = True
            return tracker

        # Todo: Make sure there is not already a good roo older than this one with these thread and comment ids

        # If we are in the middle of adding this to the db, add the thread and comment ids now
        if init_db:
            roo = last_switcharoo.update(roo, thread_id=submission_url.thread_id, comment_id=submission_url.comment_id,
                                         subreddit=submission_url.subreddit)

    else:
        # Not sure why this was happening every time
        # roo = last_switcharoo.update(roo, comment_id=comment.id)
        if init_db:
            roo = last_switcharoo.update(roo, comment_id=comment.id)

    # If comment was deleted, this will make an error. The try alleviates that
    try:
        comment.refresh()
    except (praw.exceptions.ClientException, praw.exceptions.PRAWException):
        s = reddit.submission(roo.thread_id)
        try:
            subreddit = s.subreddit
        except prawcore.exceptions.Forbidden:
            print("Forbidded from post, is the subreddit privated?")
            # If this hasn't even been added to the database yet, we have no idea what subreddit
            # To even make this kind of decision on. Reject it
            if init_db:
                tracker.subreddit_privated = True
                return tracker
            # Otherwise, attempt to check the database for this subreddit's status
            allowed = decide_subreddit_privated(reddit, last_switcharoo, roo.subreddit)
            if allowed is None or allowed is True:  # If we are awaiting a response or allowing, pass judgement for now
                return None
            else:   # Mods marked this subreddit as been permanently privated, mark broken
                tracker.subreddit_privated = True
                return tracker
        else:
            tracker.comment_deleted = True
            return tracker

    # Deleted comments sometimes don't generate errors
    if comment.body == "[removed]" or comment.body == "[deleted]":
        tracker.comment_deleted = True
        return tracker

    # Good date info to have on hand in the upcoming checks
    created = submission if submission else comment
    created = datetime.utcfromtimestamp(created.created_utc)

    # Get link in comment
    comment_url = parse.parse_comment(comment.body)

    # If there is no link, report it
    if not comment_url.is_reddit_url:
        """
        At this point, we need to decide if the roo is salvageable. We need to search the comments
        to see if there is an actual roo here and request a correction to it's link (and make it 
        the new last_good_submission). Otherwise if there is no roo, skip it by returning the current 
        last_good_submission and yell at them for linking something that isn't a roo.
        """
        tracker.comment_has_no_link = True
        return tracker

    if created > datetime(year=2021, month=3, day=1):
        if submission and comment:
            if comment.author != submission.author:
                tracker.user_mismatch = True
                return tracker

    # If the comment url links to the switcharoo subreddit, report it
    if comment_url.subreddit == "switcharoo":
        tracker.submission_linked_post = True
        return tracker

    # We'll need the last verified good switcharoo from here on
    last_good_submission = last_switcharoo.last_good(before_roo=roo, offset=0)

    # check if there is a last good submission to verify against
    if last_good_submission:
        # Is this switcharoo comment linked to the last good switcharoo?
        if comment_url.thread_id == last_good_submission.thread_id and \
                comment_url.comment_id == last_good_submission.comment_id:
            # Hooray! Linked to correct comment. Now check for context param

            # Verify it contains context param
            if "context" not in comment_url.params:
                if datetime(year=2021, month=3, day=10) < created:
                    tracker.comment_lacks_context = True
                # else:
                #     print("Ignoring bad context cause it's old")

            # Try to get the context value
            try:
                context = int(comment_url.params['context'])
            except (KeyError, ValueError):  # context is not a number
                thing = submission if submission else comment
                if datetime(year=2021, month=3, day=10) < created:
                    tracker.comment_lacks_context = True  # Should be a different error
                # else:
                #     print("Ignoring bad context cause it's old")

            if tracker.submission_deleted:
                if datetime(year=2021, month=3, day=10) > created:
                    # Check if previous comment is linked to it
                    previous_roo = last_switcharoo.next_good(roo)
                    if previous_roo:
                        previous_link = parse.parse_comment(previous_roo.comment.body)
                        submission_url = parse.RedditURL(submission.url)
                        if previous_link.thread_id == submission_url.thread_id \
                                and previous_link.comment_id == submission_url.comment_id:
                            print(f"{roo.id} Previous comment links to it and it's not causing trouble, keeping")
                            tracker.submission_deleted = False
                        else:
                            print(f"{roo.id} Could be alright but previous roo is not linked to it so keeping deleted")

        else:
            # Was this linked correctly linked to another roo?
            linked_roo = last_switcharoo.search(comment_url.thread_id, comment_url.comment_id)
            if linked_roo:
                # User correctly linked, the roo was just bad
                # If this is a problematic old roo, don't touch it unless we have to
                if datetime(year=2021, month=3, day=10) > created:
                    linked_issues = last_switcharoo.get_issues(linked_roo)
                    if linked_issues.comment_deleted or linked_issues.comment_has_no_link \
                            or linked_issues.user_noncompliance:
                        tracker.comment_linked_bad_roo = True
                    else:
                        print(f"{roo.id} is linked to the wrong roo but it's OK so I'm ignoring it")
                        if tracker.submission_deleted:
                            # Check if previous comment is linked to it
                            previous_roo = last_switcharoo.next_good(roo)
                            if previous_roo:
                                previous_link = parse.parse_comment(previous_roo.comment.body)
                                # previous_link = parse.RedditURL(previous_link)
                                submission_url = parse.RedditURL(submission.url)
                                if previous_link.thread_id == submission_url.thread_id \
                                        and previous_link.comment_id == submission_url.comment_id:
                                    print("Previous comment was linked to this one so I'm leaving that intact as well")
                                    tracker.submission_deleted = False
                                else:
                                    print("However, it was skipped over in the chain so I'm keeping it removed")
                        else:
                            print("However, the previous roo isn't linked to it so it'll stay deleted")
                else:
                    tracker.comment_linked_bad_roo = True
            else:
                # Extra check: Is this from before roo logging? Then allow roos that at least get the thread right
                # However, roos that aren't from this time get no such special privilege and are marked wrong
                if not (datetime(year=2019, month=2, day=23) > created and
                        comment_url.thread_id == last_good_submission.thread_id):
                    # I dunno what the user linked but they didn't link any switcharoo from the database
                    tracker.comment_linked_wrong = True
    else:
        print("Didn't have a last submission to check against")

    return tracker
Exemplo n.º 3
0
def process(reddit, submission, last_switcharoo, action):
    """
    Check the submission to make sure it is correct
    :param reddit: PRAW reddit instance
    :param submission: post to check
    :param last_good_submission: a dict of the last good submission in the chain's thread_id, comment_id, and submission object
    :param last_submission: url of the last switcharoo submission
    :param action: an action class to call for performing actions
    :return:
    """

    # Verify it is a link post (not a self post)
    if submission.is_self:
        return

    # Verify it is a link to a reddit thread
    # If not, assume it's a faulty submission and delete.
    if submission.domain[-10:] != "reddit.com":
        action.add_issue(submission_not_reddit)
        action.act(submission)
        return

    print("Roo:", submission.title)

    # It's a roo, add it to the list of all roos
    last_switcharoo.add_last(submission.url)

    # Check if it has multiple ? in it (like "?st=JDHTGB67&sh=f66dbbbe?context=3)
    if submission.url.count("?") > 1:
        action.add_issue(submission_multiple_params)
        action.act(submission)
        return

    # Verify it contains ?context
    if "?context" not in submission.url:
        action.add_issue(submission_lacks_context)

    # Verify it doesn't contain a slash at the end (which ignores the URL params) (Issue #5)
    if "/" in submission.url[submission.url.index("?"):]:
        action.add_issue(submission_link_final_slash)

    # Create object from comment (what the submission is linking to)
    thread_id, comment_id = parse.thread_url_to_id(submission.url)

    # If there was a comment in the link, make the comment object
    if comment_id:
        comment = reddit.comment(comment_id)
    else:  # If there was no comment in the link, take action
        action.add_issue(submission_linked_thread)
        action.act(submission)
        return

    # If comment was deleted, this will make an error. The try alleviates that
    try:
        comment.refresh()
    except (praw.exceptions.ClientException, praw.exceptions.PRAWException):
        action.add_issue(comment_deleted)
        action.act(submission)
        return

    # Deleted comments sometimes don't generate errors
    if comment.author is None or comment.body == "[removed]":
        action.add_issue(comment_deleted)
        action.act(submission, last_switcharoo.last_good())
        return

    # Get link in comment
    comment_link = parse.parse_comment(comment.body)

    # If there is no link, report it
    if not comment_link:
        """
        At this point, we need to decide if the roo is salvageable. We need to search the comments
        to see if there is an actual roo here and request a correction to it's link (and make it 
        the new last_good_submission). Otherwise if there is no roo, skip it by returning the current 
        last_good_submission and yell at them for linking something that isn't a roo.
        """
        action.add_issue(comment_has_no_link)
        action.act(submission)
        return

    # What comment and thread does this submission's switcharoo link to? It should be the last good one
    next_thread_id, next_comment_id = parse.thread_url_to_id(comment_link)

    # We'll need the last verified good switcharoo from here on
    last_good_submission = last_switcharoo.last_good()

    # check if there is a last good submission to verify against
    if last_good_submission:

        # Is this switcharoo comment linked to the last good switcharoo?
        if next_thread_id == last_good_submission.thread_id and \
                        next_comment_id == last_good_submission.comment_id:
            # Woohoo! Linked to correct comment. Now check for ?context
            if "?context" not in comment_link:
                action.add_issue(comment_lacks_context)
            else:
                print("  Linked correctly to next level in roo")
        else:
            # Was this linked correctly linked to the last thread and it wasn't good or
            # was this linked to something else?
            last_thread_id, last_comment_id = parse.thread_url_to_id(
                last_switcharoo.last_submitted())
            if next_thread_id == last_thread_id and next_comment_id == last_comment_id:
                # User correctly linked, the roo was just bad
                action.add_issue(comment_linked_bad_roo)
            else:
                # I dunno what the user linked but it didn't link the last good or last posted
                action.add_issue(comment_linked_wrong)
    else:
        print("Didn't have a last submission to check against")

    action.act(submission, last_good_submission)

    last_switcharoo.add_good(submission, thread_id, comment_id)

    return
Exemplo n.º 4
0
        else:
            print("\nUnable to find a link. Go here, find the switcharoo, and paste the link \n"
                  "to the next one here. \n\n", last_url)
            url = input()
            thread_id, comment_id = parse.thread_url_to_id(url)

        comment = reddit.comment(comment_id)

        date = pendulum.from_timestamp(comment.created_utc, tz="UTC")
        date = date.in_timezone("local")

        print("{} {} {}".format(comment.body, date, comment.author, comment.permalink))
        roo_count += 1

        last_url = url
        url = parse.parse_comment(comment.body)
        # If not url, search
        if not url:
            print("Roo linked incorrectly")
            url = parse.find_roo_comment(comment)
            print("Guessing", url)
            # print("y for yes, otherwise paste URL")
            # response = input()
            # if response.lower() != "y":
            #     url = response

    except prawcore.exceptions.RequestException:    # Unable to connect to Reddit
        print("Unable to connect to Reddit, is the internet down?")
        time.sleep(consts.sleep_time * 2)

    except prawcore.exceptions.ResponseException as e:
Exemplo n.º 5
0
def process(reddit, submission, last_switcharoo, action):
    """
    Check the submission to make sure it is correct
    :param reddit: PRAW reddit instance
    :param submission: post to check
    :param last_good_submission: a dict of the last good submission in the chain's thread_id, comment_id, and submission object
    :param last_submission: url of the last switcharoo submission
    :param action: an action class to call for performing actions
    :return:
    """

    # Verify it is a link post (not a self post)
    if submission.is_self:
        # If meta, determine if it was incorrectly submitted as meta
        if not parse.is_meta_title(submission.title):
            if parse.only_reddit_url(submission.selftext):
                action.add_issue(submission_is_meta)
                action.act(submission)
        roo = last_switcharoo.add(submission.id,
                                  link_post=False,
                                  time=datetime.utcfromtimestamp(
                                      submission.created_utc))
        return

    # Ignore announcements
    if submission.distinguished:
        return

    # Verify it is a link to a reddit thread
    # If not, assume it's a faulty submission and delete.
    if submission.domain[-10:] != "reddit.com":
        action.add_issue(submission_not_reddit)
        action.act(submission)
        return

    print("Roo:", submission.title)

    # It's a roo, add it to the list of all roos
    # Mark it as unfinished in processing in case the roo doesn't finish getting processed
    roo = last_switcharoo.add(submission.id,
                              roo_issues=[issues.submission_processing],
                              time=datetime.utcfromtimestamp(
                                  submission.created_utc))

    # Redo next three checks with regex

    regex = parse.REPatterns.reddit_strict_parse.findall(submission.url)
    url_params = parse.process_url_params(regex[0][-1])
    print(regex, url_params)
    context = int(url_params['context']) if "context" in url_params else 0
    last_switcharoo.update(roo, context=context)

    # Check if it has multiple ? in it (like "?st=JDHTGB67&sh=f66dbbbe?context=3)
    if submission.url.count("?") > 1:
        action.add_issue(submission_multiple_params)
        action.act(submission)
        last_switcharoo.update(roo, roo_issues=action.issues)
        return

    # Verify it contains ?context
    if "?context" not in submission.url:
        action.add_issue(submission_lacks_context)

    # Verify it doesn't contain a slash at the end (which ignores the URL params) (Issue #5)
    if submission.url.count("?"):
        if "/" in submission.url[submission.url.index("?"):]:
            action.add_issue(submission_link_final_slash)

    # Create object from comment (what the submission is linking to)
    thread_id, comment_id = parse.thread_url_to_id(submission.url)
    roo = last_switcharoo.update(roo,
                                 thread_id=thread_id,
                                 comment_id=comment_id)

    # If there was a comment in the link, make the comment object
    if comment_id:
        comment = reddit.comment(comment_id)
    else:  # If there was no comment in the link, take action
        action.add_issue(submission_linked_thread)
        action.act(submission)
        last_switcharoo.update(roo, roo_issues=action.issues)
        return

    # If comment was deleted, this will make an error. The try alleviates that
    try:
        comment.refresh()
    except (praw.exceptions.ClientException, praw.exceptions.PRAWException):
        action.add_issue(comment_deleted)
        action.act(submission)
        last_switcharoo.update(roo, roo_issues=action.issues)
        return

    # Deleted comments sometimes don't generate errors
    if comment.body == "[removed]":
        action.add_issue(comment_deleted)
        action.act(submission, last_switcharoo.last_good(offset=1))
        last_switcharoo.update(roo, roo_issues=action.issues)
        return

    # Get link in comment
    comment_link = parse.parse_comment(comment.body)

    # If there is no link, report it
    if not comment_link:
        """
        At this point, we need to decide if the roo is salvageable. We need to search the comments
        to see if there is an actual roo here and request a correction to it's link (and make it 
        the new last_good_submission). Otherwise if there is no roo, skip it by returning the current 
        last_good_submission and yell at them for linking something that isn't a roo.
        """
        action.add_issue(comment_has_no_link)
        action.act(submission)
        last_switcharoo.update(roo, roo_issues=action.issues)
        return

    # What comment and thread does this submission's switcharoo link to? It should be the last good one
    next_thread_id, next_comment_id = parse.thread_url_to_id(comment_link)

    # We'll need the last verified good switcharoo from here on
    last_good_submission = last_switcharoo.last_good(before_roo=roo, offset=1)

    # check if there is a last good submission to verify against
    if last_good_submission:

        # Is this switcharoo comment linked to the last good switcharoo?
        if next_thread_id == last_good_submission.thread_id and \
                        next_comment_id == last_good_submission.comment_id:
            # Woohoo! Linked to correct comment. Now check for ?context
            if "?context" not in comment_link:
                action.add_issue(comment_lacks_context)
            else:
                print("  Linked correctly to next level in roo")
        else:
            # Was this linked correctly linked to another roo?
            linked_roo = last_switcharoo.search(next_thread_id,
                                                next_comment_id)
            if linked_roo:
                # User correctly linked, the roo was just bad
                action.add_issue(comment_linked_bad_roo)
            else:
                # I dunno what the user linked but it didn't link the last good or last posted
                action.add_issue(comment_linked_wrong)
    else:
        print("Didn't have a last submission to check against")

    action.act(submission, last_good_submission)

    last_switcharoo.update(roo,
                           roo_issues=action.issues,
                           remove_issues=[issues.submission_processing])
    # last_switcharoo.add_good(submission, thread_id, comment_id)

    return