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
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
def get_access_token():
|
| 10 |
+
|
| 11 |
+
client_secret = os.getenv("CLIENT_SECRET")
|
| 12 |
+
client_id = os.getenv("CLIENT_ID")
|
| 13 |
+
password = os.getenv("PASSWORD")
|
| 14 |
+
username = "Conscious_Abroad_946"
|
| 15 |
+
user_agent = "my_reddit_app/0.1 by Conscious_Abroad_946"
|
| 16 |
+
|
| 17 |
+
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
|
| 18 |
+
|
| 19 |
+
data = {
|
| 20 |
+
'grant_type': 'password',
|
| 21 |
+
'username': username,
|
| 22 |
+
'password': password
|
| 23 |
+
}
|
| 24 |
+
headers = {'User-Agent': user_agent}
|
| 25 |
+
|
| 26 |
+
response = requests.post(
|
| 27 |
+
'https://www.reddit.com/api/v1/access_token',
|
| 28 |
+
auth=auth,
|
| 29 |
+
data=data,
|
| 30 |
+
headers=headers
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
access_token = ""
|
| 34 |
+
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
token = response.json()['access_token']
|
| 37 |
+
access_token += token
|
| 38 |
+
print(f'Access token: {token}')
|
| 39 |
+
else:
|
| 40 |
+
print(f'Error: {response.status_code}, {response.text}')
|
| 41 |
+
|
| 42 |
+
print(f"access_token after oauth call is: {access_token}")
|
| 43 |
+
return access_token
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
#column a - links
|
| 54 |
+
#column b - posts
|
| 55 |
+
#column c - ai generated ready-made response
|
| 56 |
+
#even list of links is meaningful improvement
|
| 57 |
+
|
| 58 |
+
def to_list(x):
|
| 59 |
+
if x == None:
|
| 60 |
+
return []
|
| 61 |
+
if isinstance(x, str):
|
| 62 |
+
return [s.strip() for s in x.split(",")]
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def reddit_hack(keyword, *, subreddits=None):
|
| 67 |
+
|
| 68 |
+
#sub_reddit_list = ["legaladviceuk", "housinguk", "ukpersonalfinance"]#
|
| 69 |
+
sub_reddit_list = to_list(subreddits)
|
| 70 |
+
|
| 71 |
+
results_overall = []
|
| 72 |
+
|
| 73 |
+
access_token = get_access_token()
|
| 74 |
+
|
| 75 |
+
if sub_reddit_list == []:
|
| 76 |
+
return "no subreddits entered"
|
| 77 |
+
|
| 78 |
+
for subreddit in sub_reddit_list:
|
| 79 |
+
|
| 80 |
+
url = f"https://oauth.reddit.com/r/{subreddit}/search"
|
| 81 |
+
|
| 82 |
+
# Query parameters
|
| 83 |
+
params = {
|
| 84 |
+
"q": keyword, # Search query
|
| 85 |
+
"limit": 50, # Max number of results (up to 100)
|
| 86 |
+
"sort": "relevance", # Sort by relevance
|
| 87 |
+
"t": "month", # Time filter
|
| 88 |
+
"restrict_sr": "true", # Restrict to the subreddit
|
| 89 |
+
"include_facets": "false", # Don't include facets
|
| 90 |
+
"sr_detail": "false", # Don't expand subreddit details
|
| 91 |
+
# Optional params:
|
| 92 |
+
# "after": "t3_xyz", # for pagination
|
| 93 |
+
# "before": "t3_abc",
|
| 94 |
+
# "type": "link,user" # restrict types of results
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
# Headers with OAuth2 token
|
| 98 |
+
headers = {
|
| 99 |
+
"Authorization": f"bearer {access_token}",
|
| 100 |
+
"User-Agent": "inauguralai-reddit-search-bot/1.0"
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# Make the request
|
| 104 |
+
response = requests.get(url, headers=headers, params=params)
|
| 105 |
+
|
| 106 |
+
# Handle response
|
| 107 |
+
if response.status_code == 200:
|
| 108 |
+
results = response.json()
|
| 109 |
+
if len(results) == 0:
|
| 110 |
+
print("there are no results!")
|
| 111 |
+
for idx, post in enumerate(results.get("data", {}).get("children", []), 1):
|
| 112 |
+
data = post["data"]
|
| 113 |
+
print(f"{idx}. {data['title']} (Score: {data['score']})")
|
| 114 |
+
print(f" Link: https://reddit.com{data['permalink']}\n")
|
| 115 |
+
|
| 116 |
+
results_overall.append(data)
|
| 117 |
+
else:
|
| 118 |
+
print(f"Error {response.status_code}: {response.text}")
|
| 119 |
+
|
| 120 |
+
#google client id, 625949348680-343bebli3ol5c8aflufclntj67j2s3gg.apps.googleusercontent.com
|
| 121 |
+
#google client secret, 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
|
| 122 |
+
|
| 123 |
+
results_json = json.dumps(results_overall, ensure_ascii=False, indent=2)
|
| 124 |
+
print("JSON string:\n", results_json)
|
| 125 |
+
|
| 126 |
+
results_text = "\n".join(
|
| 127 |
+
f"{i+1}. [{r['subreddit']}] {r['title']} (Score: {r['score']})\n https://reddit.com{r['permalink']}"
|
| 128 |
+
for i, r in enumerate(results_overall)
|
| 129 |
+
)
|
| 130 |
+
print("\nPretty text:\n", results_text)
|
| 131 |
+
return results_text
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def main(keyword, subreddits):
|
| 137 |
+
print(f"subreddits are {subreddits}")
|
| 138 |
+
result = reddit_hack(keyword, subreddits=subreddits)
|
| 139 |
+
return result
|
| 140 |
+
|
| 141 |
+
#reddit_hack("ccj", subreddits="legaladviceuk, housinguk, ukpersonalfinance")
|
| 142 |
+
#main("ccj","legaladviceuk, housinguk, ukpersonalfinance")
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
demo = gr.Interface(
|
| 147 |
+
fn=main,
|
| 148 |
+
inputs=["text", "text"], #one for artist_name, other for prompt
|
| 149 |
+
outputs="text",
|
| 150 |
+
title="redditHack",
|
| 151 |
+
description="enter keyword, and comma seerate string of subreddits"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
demo.launch(share=True)
|