redditHack / app.py
Derfel2025's picture
added ability to search all reddit if no subreddit added
5046196
import requests
import json
import gradio as gr
from dotenv import load_dotenv
import os
load_dotenv()
def get_access_token():
client_secret = os.getenv("CLIENT_SECRET")
client_id = os.getenv("CLIENT_ID")
password = os.getenv("PASSWORD")
username = "Conscious_Abroad_946"
user_agent = "my_reddit_app/0.1 by Conscious_Abroad_946"
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
data = {
'grant_type': 'password',
'username': username,
'password': password
}
headers = {'User-Agent': user_agent}
response = requests.post(
'https://www.reddit.com/api/v1/access_token',
auth=auth,
data=data,
headers=headers
)
access_token = ""
if response.status_code == 200:
token = response.json()['access_token']
access_token += token
print(f'Access token: {token}')
else:
print(f'Error: {response.status_code}, {response.text}')
print(f"access_token after oauth call is: {access_token}")
return access_token
#column a - links
#column b - posts
#column c - ai generated ready-made response
#even list of links is meaningful improvement
def to_list(x):
if x == None:
return None
if isinstance(x, str):
return [s.strip() for s in x.split(",") if s.strip()] or None
def reddit_hack(keyword, count=10, *, subreddits=None):
#sub_reddit_list = ["legaladviceuk", "housinguk", "ukpersonalfinance"]#
sub_reddit_list = to_list(subreddits)
results_overall = []
access_token = get_access_token()
if not sub_reddit_list:#if no subreddits, then search everything
url = "https://oauth.reddit.com/search"
params = {
"q": keyword,
"limit": count, # clamp to [1,100]
"sort": "relevance",
"t": "year",
"type": "link", # posts only (omit if you want comments/users too)
# "after": "t3_...", # pagination if you add it later
}
headers = {
"Authorization": f"bearer {access_token}",
"User-Agent": "inauguralai-reddit-search-bot/1.0",
}
response = requests.get(url, headers=headers, params=params, timeout=20)
if response.status_code == 200:
results = response.json()
if len(results) == 0:
print("there are no results!")
for idx, post in enumerate(results.get("data", {}).get("children", []), 1):
data = post["data"]
print(f"{idx}. {data['title']} (Score: {data['score']})")
print(f" Link: https://reddit.com{data['permalink']}\n")
results_overall.append(data)
else:
print(f"Error {response.status_code}: {response.text}")
else:
for subreddit in sub_reddit_list:
url = f"https://oauth.reddit.com/r/{subreddit}/search"
# Query parameters
params = {
"q": keyword, # Search query
"limit": count, # Max number of results (up to 100)
"sort": "relevance", # Sort by relevance
"t": "year", # Time filter
"restrict_sr": "true", # Restrict to the subreddit
"include_facets": "false",# Don't include facets
"sr_detail": "false", # Don't expand subreddit details
# Optional params:
# "after": "t3_xyz", # for pagination
# "before": "t3_abc",
# "type": "link,user" # restrict types of results
}
# Headers with OAuth2 token
headers = {
"Authorization": f"bearer {access_token}",
"User-Agent": "inauguralai-reddit-search-bot/1.0"
}
# Make the request
response = requests.get(url, headers=headers, params=params)
# Handle response
if response.status_code == 200:
results = response.json()
if len(results) == 0:
print("there are no results!")
for idx, post in enumerate(results.get("data", {}).get("children", []), 1):
data = post["data"]
print(f"{idx}. {data['title']} (Score: {data['score']})")
print(f" Link: https://reddit.com{data['permalink']}\n")
results_overall.append(data)
else:
print(f"Error {response.status_code}: {response.text}")
#google client id, 625949348680-343bebli3ol5c8aflufclntj67j2s3gg.apps.googleusercontent.com
#google client secret, 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
results_json = json.dumps(results_overall, ensure_ascii=False, indent=2)
print("JSON string:\n", results_json)
results_text = "\n".join(
f"{i+1}. [{r['subreddit']}] {r['title']} (Score: {r['score']})\n https://reddit.com{r['permalink']}"
for i, r in enumerate(results_overall)
)
print("\nPretty text:\n", results_text)
return results_text
def main(keyword, count, subreddits):
print(f"subreddits are {subreddits}")
result = reddit_hack(keyword, count, subreddits=subreddits)
return result
#reddit_hack("ccj", subreddits="legaladviceuk, housinguk, ukpersonalfinance")
#main("ccj","legaladviceuk, housinguk, ukpersonalfinance")
demo = gr.Interface(
fn=main,
inputs=[
gr.Textbox(label="Keyword", placeholder="e.g., ccj"),
gr.Number(label="Max posts", precision=0, minimum=1, value=10),
gr.Textbox(label="Subreddits (comma-separated). Or leave empty and search everywhere", placeholder="music, indieheads")
],
outputs=gr.Textbox(label="Result"),
title="redditHack",
description="enter keyword, and comma seerate string of subreddits"
)
demo.launch(share=True)