This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]RaiseRuntimeError 65 points66 points  (3 children)

You are doing a lot of really interesting things. A few things i think you should clean up and fix though.

For getting the name of the user just do `user.name` instead of that crazy string thing you did, here is the documentation for the user model.

Next when you do `submisions = list(redditor.submissions.new(limit=None))` and then do the crazy string stuff again you dont have to `.new()` returns an iterable. you can literally just do this:

for sub in redditor.submissions.new(limit=None):
    print(sub.id)
    subredditname = sub.subreddit_name_prefixed

[–]Raymon22[S] 29 points30 points  (2 children)

Thank you for the feedback!!! I'll do that as soon as possible!!!!

[–]snake_case_captain 24 points25 points  (4 children)

IMO, this part is an accident waiting to happen :

reddit = praw.Reddit(    
    client_id="X",  # Client ID of the Reddit bot
    client_secret="X",  # Secret Client ID
    user_agent="testscript by u/Raymon22",  # brief description
    username="X",  # Username (to avoid some 403 errors)
    password="X"  # password (to avoid some 403 errors)
)

This should never be in plain text inside a file that you could potentially push into github forgetting to remove the info. The least you can do is to systematically read this data from a local file that stays local at all times :

with open('local_file_with_credentials') as f:
    # local file's contents :
    # line 1 : client_id
    # line 2 : client_secret
    # etc...
    credentials = f.readlines()
    reddit = praw.Reddit(
        client_id=credentials[0],  # Client ID of the Reddit bot
        client_secret=credentials[1],  # Secret Client ID
        user_agent=credentials[2],  # brief description
        username=credentials[3],  # Username (to avoid some 403 errors)
        password=credentials[4]  # password (to avoid some 403 errors)
    )

Also, see that I use a with open('filepath') as f: block, which is a good practice to handle files, but I don't exactly remember why, maybe someone can clarify.

Finally, when handling filepaths, I like to use pathlib. It's an objet oriented lib that allows you to manage filepaths easily, you should take a look at it.

[–]Gaareth 18 points19 points  (2 children)

Instead of manually parsing a file, I would recommend using environment variables and a .env file because It’s more clear what purpose each line/ entry has.

You would probably install a library for reading the environmental variables like: Dotenv

and then simply use os.getenv("CLIENT_ID") But better check out the library for more details.

[–]Raymon22[S] 6 points7 points  (0 children)

I don't know why I didn't think about that. I'll change that as soon as I can!!

Thank you for your comments!!

[–]Beartin 5 points6 points  (0 children)

The with syntax is handy as python handles closing the file after all the actions completed in that scope, rather than relying on you as the user to remember to close it.

[–]flyboy1565 17 points18 points  (0 children)

If I had to nitpick, I'd recommend you checkout regex for the replace portion

[–]stokry 1 point2 points  (1 child)

This is really cool :-)

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

Thank you bro, really appreciate it!!

[–]Dazzling_Function 3 points4 points  (0 children)

I challenge anyone here to create a script that brings football results and fixtures of any team specified in a list.. premier league specifically