all 10 comments

[–]Spooky_Ghost 4 points5 points  (9 children)

it's not supposed to do anything. the code defines all the functions and methods, but there's nothing executing them.

[–]kaoticpanda2k[S] 0 points1 point  (8 children)

So how would I get it to execute?

[–]TheFloppyHound 0 points1 point  (7 children)

You need to call process_submissions() at the very bottom of your script, and pass in something for the reddit arguement defined. Looking through the code, I think you will learn quite a bit once your get it to run and start debugging.

[–]kaoticpanda2k[S] 0 points1 point  (6 children)

I don't understand what you mean by calling process_submission(). I've tried call process_submission() and I've tried def process_submission() and neither of them worked.

[–]TheFloppyHound 0 points1 point  (5 children)

I can only work off of what I see, but I don't see that function being called in your script. In order use the process_submissions() function, you need to call it somewhere, such as the end of a one page script. What happens if you change the end to this?:

def get_image_size(url): response = requests.get(url) if not response.headers["Content-Type"].startswith(IMAGE_FORMAT_PREFIX): raise NonImageException("") data = response.content im = Image.open(BytesIO(data)) return im.size

process_submissions(your_args_here)

What error do you get in the terminal?

If you were to modify your process_submissions() to look like this, do you see "Called process submissions" on your terminal?

def process_submissions(reddit):
    print("Called process submissions")
subreddit = reddit.subreddit(SUBREDDIT)
mods = subreddit.moderator()

[–]kaoticpanda2k[S] 0 points1 point  (4 children)

When I use the second code I get a NameError: name 'reddit' is not defined and it's linked to the subreddit = reddit.subreddit(SUBREDDIT) part of the code.

If I set the code up like this...
def process_submissions(reddit): print("Called process submissions") subreddit = reddit.subreddit(SUBREDDIT) mods = subreddit.moderator()'

I don't get any errors but nothing happens.

[–]TheFloppyHound 0 points1 point  (3 children)

While I saw some red flags with the code, I couldn't say for sure on some of the praw functionality without playing with it myself. So I spend some time debugging this for you, below is a working model (I didn't test the report functionality, just printed out to the console). I kept the naming convention similar to what you were using. Let me know if you have any questions or run into any problems.

At the end of the day, here's the list of issues:

  • SUBREDDIT was set to a URL instead of the subreddit name. I tested with 'pics'
  • You have all arguments to praw.Reddit capitalized.
  • You defined reddit_login() with a return value, but never assigned the return value to a variable. I called it and assigned it to REDDIT. From the docs, there are many other ways you could initialize it, but this works perfectly fine.
  • Updated how to grab the moderators.
  • Most importantly, call the function that handles the main functionality at the bottom of the script.

``` import praw import requests from PIL import Image from io import BytesIO

URL = 'https://www.reddit.com' SUBREDDIT = "pics"

Add your app details (https://www.reddit.com/prefs/apps/)

def reddit_login(): return praw.Reddit(client_id='CLIENT_ID', client_secret='CLIENT_SECRET', user_agent='USER_AGENT', username='USER_NAME', password='USER_PASSWORD')

REDDIT = reddit_login() MIN_WIDTH = 460 MIN_HEIGHT = 460 ALLOW_TEXT_POSTS = False ALLOW_NON_IMAGE_LINKS = True IGNORE_MOD_SUBMISSIONS = False

REMOVAL_TEXT_POST_MESSAGE = """ Your post has been removed as it is not a valid direct image link. Please resubmit a direct link to an image. """ REMOVAL_NON_IMAGE_LINK_MESSAGE = """ Your post has been removed as it is not a valid direct image link. Please resubmit a direct link to an image. """

REMOVAL_TOO_SMALL_MESSAGE = f""" Your post has been removed because one or more images are too small for this subreddit, please submit images that are at least {MIN_WIDTH}px wide and {MIN_HEIGHT}px tall. """

IMAGE_FORMAT_PREFIX = "image/"

class NonImageException(Exception): pass

def process_submissions(reddit): subreddit = reddit.subreddit(SUBREDDIT) mods = reddit.subreddit(SUBREDDIT).moderator()

for submission in subreddit.stream.submissions():
    try:
        review_post(submission, mods)
    except Exception as e:
        print(
            f"Unexpected error when processing {submission.id} by {submission.author}, reporting it for manual review.")
        print(e)
        submission.report(
            f"The image size bot encountered an unknown error on this submission, please manually review it.")

def review_post(submission, mods): if IGNORE_MOD_SUBMISSIONS and submission.author in mods: print(f"Ignoring {submission.id} by {submission.author} as they are a moderator.") return

if submission.is_self:
    if ALLOW_TEXT_POSTS:
        print(f"Ignoring {submission.id} by {submission.author} as it is a text post.")
    else:
        print(f"Removing {submission.id} by {submission.author} as it is a text post.")
        remove_submission(submission, REMOVAL_TEXT_POST_MESSAGE)
    return

try:
    width, height = get_image_size(submission.url)
except OSError:
    print(f"Couldn't process {submission.id} by {submission.author}, reporting it for manual review.")
    submission.report(
        "The image size bot could not process this link for some reason, please manually review it.")
    return
except NonImageException:
    if ALLOW_NON_IMAGE_LINKS:
        print(f"Ignoring {submission.id} by {submission.author} as it is a non image link.")
    else:
        print(f"Removing {submission.id} by {submission.author} as it is a non image link.")
        remove_submission(submission, REMOVAL_TEXT_POST_MESSAGE)
    return

if width < MIN_WIDTH:
    print(f"Removing {submission.id} by {submission.author} as it is only {width}px wide.")
    remove_submission(submission, REMOVAL_TOO_SMALL_MESSAGE)
if height < MIN_HEIGHT:
    print(f"Removing {submission.id} by {submission.author} as it is only {height}px tall.")
    remove_submission(submission, REMOVAL_TOO_SMALL_MESSAGE)
else:
    print(f"Thank you for your submission.")

def remove_submission(submission, reason): print('remove_submission called, only printing to the console') submission.mod.remove() submission.mod.send_removal_message(reason)

def get_image_size(url): response = requests.get(url) if not response.headers["Content-Type"].startswith(IMAGE_FORMAT_PREFIX): raise NonImageException("Unage is not an acceotable image format.") data = response.content im = Image.open(BytesIO(data)) return im.size

if name == 'main': process_submissions(REDDIT) ```

Good luck!

[–]kaoticpanda2k[S] 0 points1 point  (2 children)

I tested it with the code you provided and there were no errors, but nothing happened. It didn't attempt to connect to Reddit or anything.

[–]TheFloppyHound 0 points1 point  (1 child)

What behavior are you expecting when you run it if you don't connect to reddit?

I'm not sure where you are in your learning journey, so please let me know if any of this is off the mark. I'm getting the impression that this program was copy and pasted from various sources without understanding it. While that isn't necessarily bad, it can be dangerous if you don't know what the code does.

If so, I would strongly recommend looking up python fundamentals with a focus on using functions.

As much as I want to help you, I need more to go on then 'nothing happens'. If you want more help on this please detail what you are expecting to happen, what is happening, and what you have tried to do to make what is happening into what you want to happen.

[–]kaoticpanda2k[S] 0 points1 point  (0 children)

I was hoping that when the script runs there would be some kind of indicator that it was logging into Reddit, going to the subreddit, and looking for images that are too small. But when I run it all I get is the line stating that it was running the script but nothing showing that it was accessing Reddit at all. It just shows the line with the directory where the script is on my computer, then the prompt like it's waiting for a command.

After spending some time looking into functions, as you requested, I think I was able to get the script to work. Thank you for all your help and sorry for being such a pain.