Added better flair tools.
This commit is contained in:
76
rstat_tool/flair_finder.py
Normal file
76
rstat_tool/flair_finder.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# rstat_tool/flair_finder.py
|
||||
# A dedicated tool to find available link flairs for a subreddit.
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
import praw
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path
|
||||
|
||||
def get_reddit_instance_for_flairs():
|
||||
"""
|
||||
Initializes and returns an authenticated PRAW instance using the refresh token.
|
||||
This is a copy of the robust authentication from the posting script.
|
||||
"""
|
||||
# Find the .env file relative to the project root
|
||||
env_path = Path(__file__).parent.parent / '.env'
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
|
||||
client_id = os.getenv("REDDIT_CLIENT_ID")
|
||||
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
|
||||
user_agent = os.getenv("REDDIT_USER_AGENT")
|
||||
refresh_token = os.getenv("REDDIT_REFRESH_TOKEN")
|
||||
|
||||
if not all([client_id, client_secret, user_agent, refresh_token]):
|
||||
print("Error: Reddit API credentials (including REDDIT_REFRESH_TOKEN) must be set in .env file.", file=sys.stderr)
|
||||
return None
|
||||
|
||||
return praw.Reddit(
|
||||
client_id=client_id,
|
||||
client_secret=client_secret,
|
||||
user_agent=user_agent,
|
||||
refresh_token=refresh_token
|
||||
)
|
||||
|
||||
def main():
|
||||
"""Main function to fetch and display flairs."""
|
||||
parser = argparse.ArgumentParser(description="Fetch and display available post flairs for a subreddit.")
|
||||
parser.add_argument("subreddit", help="The name of the subreddit to check.")
|
||||
args = parser.parse_args()
|
||||
|
||||
reddit = get_reddit_instance_for_flairs()
|
||||
if not reddit:
|
||||
sys.exit(1)
|
||||
|
||||
print(f"\n--- Attempting to Fetch Post Flairs for r/{args.subreddit} ---")
|
||||
|
||||
try:
|
||||
# This uses PRAW's generic GET request method to hit the specific API endpoint.
|
||||
api_path = f"/r/{args.subreddit}/api/link_flair_v2.json"
|
||||
flairs = reddit.get(api_path, params={"raw_json": 1})
|
||||
|
||||
if not flairs:
|
||||
print("No flairs found or flair list is empty for this subreddit.")
|
||||
return
|
||||
|
||||
print("\n--- Available Post Flairs ---")
|
||||
found_count = 0
|
||||
for flair in flairs:
|
||||
flair_text = flair.get('text')
|
||||
flair_id = flair.get('id')
|
||||
if flair_text and flair_id:
|
||||
print(f" Flair Text: '{flair_text}'")
|
||||
print(f" Flair ID: {flair_id}\n")
|
||||
found_count += 1
|
||||
|
||||
if found_count == 0:
|
||||
print("No flairs with both text and ID were found.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\nAn error occurred: {e}", file=sys.stderr)
|
||||
print("Hint: Please ensure the subreddit exists and that your authenticated user has permission to view it.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user