Created
October 26, 2019 17:41
-
-
Save leviroth/c892664201446b6ec4776fbada38b36f to your computer and use it in GitHub Desktop.
"New Reddit" flair transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import sys | |
import praw | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-s", "--subreddit", help="subreddit", type=str, required=True) | |
parser.add_argument( | |
"-o", | |
"--output", | |
help="output flair file", | |
type=argparse.FileType("w"), | |
required=True, | |
) | |
args = parser.parse_args() | |
reddit = praw.Reddit(user_agent="flair downloader - by /u/L72_Elite_Kraken") | |
subreddit = reddit.subreddit(args.subreddit) | |
for flair in subreddit.flair(limit=None): | |
flair["user"] = str(flair["user"]) | |
print(json.dumps(flair), file=args.output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import collections | |
import json | |
import sys | |
import praw | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-s", "--subreddit", help="subreddit", type=str, required=True) | |
parser.add_argument( | |
"-i", "--inputs", help="input flair json", type=argparse.FileType(), required=True | |
) | |
parser.add_argument( | |
"-t", | |
"--templates", | |
help="new flair templates json", | |
type=argparse.FileType(), | |
required=True, | |
) | |
args = parser.parse_args() | |
templates_raw = json.load(args.templates) | |
templates = {template["css_class"]: template["id"] for template in templates_raw} | |
reddit = praw.Reddit(user_agent="flair uploader - by /u/L72_Elite_Kraken") | |
subreddit = reddit.subreddit(args.subreddit) | |
OldFlair = collections.namedtuple("OldFlair", ["redditor", "text", "flair_css_class"]) | |
NewFlair = collections.namedtuple("NewFlair", ["redditor", "text", "flair_template_id"]) | |
old_flairs = [] | |
for line in args.inputs: | |
flair_dict = json.loads(line) | |
flair = OldFlair( | |
redditor=flair_dict["user"], | |
text=flair_dict["flair_text"], | |
flair_css_class=flair_dict["flair_css_class"], | |
) | |
old_flairs.append(flair) | |
old_css_classes = {flair.flair_css_class for flair in old_flairs} | |
missing = old_css_classes - templates.keys() | |
if len(missing) > 0: | |
print("Old CSS classes missing from new templates: {}".format(missing)) | |
sys.exit(1) | |
new_flairs = [ | |
NewFlair( | |
redditor=old_flair.redditor, | |
text=old_flair.text, | |
flair_template_id=templates[old_flair.flair_css_class], | |
) | |
for old_flair in old_flairs | |
] | |
for flair in new_flairs: | |
try: | |
subreddit.flair.set( | |
redditor=flair.redditor, | |
text=flair.text, | |
flair_template_id=flair.flair_template_id, | |
) | |
except Exception as e: | |
print("UPLOAD EXCEPTION", e, flair, file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment