Add option to set flair.
This commit is contained in:
@@ -51,29 +51,40 @@ def find_latest_image(pattern):
|
||||
print(f"Error finding image file: {e}")
|
||||
return None
|
||||
|
||||
def get_flair_id(subreddit, flair_text):
|
||||
"""
|
||||
Attempts to find the ID of a flair by its text.
|
||||
Returns the ID string or None if not found or an error occurs.
|
||||
"""
|
||||
if not flair_text:
|
||||
return None
|
||||
|
||||
print(f"Attempting to find Flair ID for text: '{flair_text}'...")
|
||||
try:
|
||||
flairs = subreddit.flair.link_templates
|
||||
for flair in flairs:
|
||||
if flair['text'].lower() == flair_text.lower():
|
||||
print(f" -> Found Flair ID: {flair['id']}")
|
||||
return flair['id']
|
||||
print(" -> Warning: No matching flair text found.")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f" -> Warning: Could not fetch flairs for this subreddit (Error: {e}). Proceeding without flair.")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to find an image and post it to Reddit."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Find the latest sentiment image and post it to a subreddit."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--subreddit",
|
||||
help="The source subreddit of the image to post. (Defaults to overall summary)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-w",
|
||||
"--weekly",
|
||||
action="store_true",
|
||||
help="Post the weekly summary instead of the daily one.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--target-subreddit",
|
||||
default="rstat",
|
||||
help="The subreddit to post the image to. (Default: rstat)",
|
||||
)
|
||||
parser.add_argument("-s", "--subreddit", help="The source subreddit of the image to post. (Defaults to overall summary)")
|
||||
parser.add_argument("-w", "--weekly", action="store_true", help="Post the weekly summary instead of the daily one.")
|
||||
parser.add_argument("-t", "--target-subreddit", default="rstat", help="The subreddit to post the image to. (Default: rstat)")
|
||||
parser.add_argument("--flair-text", help="The text of the flair to search for (e.g., 'Daily Summary').")
|
||||
parser.add_argument("--flair-id", help="Manually provide a specific Flair ID (overrides --flair-text).")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# --- 1. Determine filename pattern and post title ---
|
||||
@@ -114,12 +125,23 @@ def main():
|
||||
|
||||
try:
|
||||
target_sub = reddit.subreddit(args.target_subreddit)
|
||||
|
||||
# --- NEW SMART FLAIR LOGIC ---
|
||||
final_flair_id = None
|
||||
if args.flair_id:
|
||||
# If the user provides a specific ID, use it directly.
|
||||
print(f"Using provided Flair ID: {args.flair_id}")
|
||||
final_flair_id = args.flair_id
|
||||
elif args.flair_text:
|
||||
# If they provide text, try to find the ID automatically.
|
||||
final_flair_id = get_flair_id(target_sub, args.flair_text)
|
||||
|
||||
print(f"Submitting '{post_title}' to r/{target_sub.display_name}...")
|
||||
|
||||
submission = target_sub.submit_image(
|
||||
title=post_title,
|
||||
image_path=image_to_post,
|
||||
flair_id=None, # Optional: You can add a flair ID here if you want
|
||||
flair_id=final_flair_id # This will be the found ID or None
|
||||
)
|
||||
|
||||
print("\n--- Post Successful! ---")
|
||||
@@ -127,7 +149,8 @@ def main():
|
||||
|
||||
except Exception as e:
|
||||
print(f"\nAn error occurred while posting to Reddit: {e}")
|
||||
|
||||
if 'FLAIR_REQUIRED' in str(e):
|
||||
print("\nHint: This subreddit requires a flair. Try finding the flair text or ID and use the --flair-text or --flair-id argument.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user