raway / app.py
wuhp's picture
Update app.py
a725de0 verified
import gradio as gr
import praw
import re
# ================== REDDIT CONFIG ==================
REDDIT_CLIENT_ID = "pWkYNUvj3o3wsqiOiki8Zg"
REDDIT_CLIENT_SECRET = "1f85lA8fvBVY9L2BThpp4VHa92rcCw"
REDDIT_USER_AGENT = "wuhp web app by u/wuhp24"
# ==================================================
reddit = praw.Reddit(
client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT,
check_for_async=False
)
def extract_post_id(url: str) -> str:
match = re.search(r"comments/([a-z0-9]+)/", url)
if not match:
raise ValueError("Invalid Reddit post URL")
return match.group(1)
def fetch_entries(url):
try:
submission = reddit.submission(id=extract_post_id(url))
submission.comments.replace_more(limit=None)
op_username = str(submission.author)
seen_users = {}
for comment in submission.comments.list():
if comment.author is None:
continue
username = str(comment.author)
# Exclusions
if username.lower() == "automoderator":
continue
if username == op_username:
continue
# Only first comment counts
if username not in seen_users:
seen_users[username] = comment.body.strip()
if not seen_users:
return "", "", "⚠️ No valid entries found."
# List 1: names + comments
with_comments = []
for user, comment in seen_users.items():
with_comments.append(f"{user}: {comment}")
with_comments_text = "\n".join(with_comments)
# List 2: names only (wheel-ready)
names_only_text = "\n".join(seen_users.keys())
status = f"πŸŽ‰ Total valid entries: {len(seen_users)}"
return with_comments_text, names_only_text, status
except Exception as e:
return "", "", f"❌ Error: {e}"
# ================== GRADIO UI ==================
with gr.Blocks() as demo:
gr.Markdown("# 🎁 Reddit Giveaway Entry Extractor")
gr.Markdown(
"- Uses Reddit official API\n"
"- One entry per user\n"
"- Excludes AutoModerator & OP\n"
"- Output is wheel-site ready (transparent & fair)"
)
url_input = gr.Textbox(
label="Reddit Post URL",
placeholder="Paste Reddit post URL here"
)
fetch_btn = gr.Button("Fetch Entries")
comments_output = gr.Textbox(
label="Entries (Username + Comment)",
lines=18
)
names_output = gr.Textbox(
label="Entries (Usernames Only – Paste Into Wheel)",
lines=18
)
status_output = gr.Textbox(label="Status")
fetch_btn.click(
fetch_entries,
inputs=url_input,
outputs=[comments_output, names_output, status_output]
)
demo.launch()