This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
Sanity check on PRAW bulk flair updating
Post Body

Hello /r/redditdev

I just wanted a sanity check, I put together a script (please don't judge the quality of the coding, I don't do much as far as python, and was more guess and test from some examples I found and threw together.)

I compiled 4 different functions, Finding, Replacing, Backup, and Restore of flair.

Finding was just to find and display all users/flair based on a search for Flair_Text/CSS or both. Replacing takes that a step further and lets me search for and replace Flair_Text, or CSS for all users it finds, but I expanded that further to also dump all found flairs into a csv file in the backup side of things, and restore allows me use a backed up/dumped csv of all flairs from one sub with a particular css or text flair I had searched for, and then either restore it to the same sub, or to a different sub if I wanted to sync flairs between different but related subreddits.

I just wanted a sanity check that this code isn't going to get my account suspended, as I want to be sure to play by the rules and not cause any issues. I'm going to guess the reading of data is a lot less critical for just finding and backing up, but as for replacing and restoring, here's a snippet of the primary code for restoring from a previously backed up csv:

timestr = time.strftime("%Y%m%d-%H%M%S")
r = praw.Reddit("reddit_login")

subreddit_name = input("Which Subreddit? ")
subreddit = r.subreddit(subreddit_name)

restore_filename = input("Filename to restore? ")

f = open(subreddit_name "_restoreflair-" timestr ".log", "a")

num_restored = 0

with open(restore_filename, mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count  = 1

        subreddit.flair.set(row["user"], text=row["flair_text"], css_class=row["flair_css_class"])
        num_restored  = 1
        print (str(row["user"])   " Restored to Flair_Text: "   str(row["flair_text"])   " and Flair_CSS_Class: "   str(row["flair_css_class"])   ".")
        f.write(str(row["user"])   " Restored Text Flair: "   str(row["flair_text"])   "  and CSS Flair: "   str(row["flair_css_class"])   "\n")

    print (str(num_restored)   " flairs have been restored to "   subreddit_name   ".")
    f.write("\n\n"   str(num_restored)   " flairs have been restored to "   subreddit_name   ".")

    f.close()

If someone can provide a sanity check that using this to backup 400-800 flair from one sub, and restore to another isn't going to cause issues for me, that would be great! I've already tested it, and it seem to work fine. Backing up/displaying flair is pretty quick and it can dump 800 flair in a matter of a few minutes. Restoring took a little bit longer and was initially echoing/printing out 2-4 users per second, but after a few minutes slowed to about 1 per second.

I looked everywhere for something to do what I wanted, but the best I could find was a 12 year old script on github that didn't even use PRAW, so I thought best to look at (attempting) to code something myself, and while it seems to function perfectly fine, I'm just not sure of the implications of how it works on the backend.

The replace script works the same as the restore script, although it just gets the input during the running of the script on what text/css to look for, and what to replace it with. The Restore script just reads from a previously saved csv file containing the user,flair_text,flair_css_class

Thanks!

Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
10,434
Link Karma
8,045
Comment Karma
2,113
Profile updated: 4 days ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago