Spaces:
Sleeping
Sleeping
Update src/reddit_client.py
Browse files- src/reddit_client.py +17 -25
src/reddit_client.py
CHANGED
|
@@ -6,52 +6,44 @@ from dotenv import load_dotenv
|
|
| 6 |
|
| 7 |
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), '..', '.env'))
|
| 8 |
|
| 9 |
-
|
| 10 |
clients = []
|
| 11 |
-
|
| 12 |
-
for i in range(1, MAX_CLIENTS + 1):
|
| 13 |
cid = os.getenv(f"REDDIT_CLIENT_ID_{i}")
|
| 14 |
-
|
| 15 |
agent = os.getenv(f"REDDIT_USER_AGENT_{i}")
|
| 16 |
username = os.getenv(f"REDDIT_USERNAME_{i}")
|
| 17 |
password = os.getenv(f"REDDIT_PASSWORD_{i}")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
def get_reddit_client():
|
| 33 |
return clients[int(time.time()) % len(clients)]
|
| 34 |
|
| 35 |
def search_reddit(brand, limit=10, retries=3):
|
| 36 |
-
now = time.time()
|
| 37 |
-
if brand in _search_cache:
|
| 38 |
-
ts, cached_posts = _search_cache[brand]
|
| 39 |
-
if now - ts < CACHE_TTL_SECONDS:
|
| 40 |
-
logging.info(f"Cache hit for '{brand}'")
|
| 41 |
-
return cached_posts
|
| 42 |
-
|
| 43 |
delay = 1
|
| 44 |
for attempt in range(retries):
|
| 45 |
try:
|
| 46 |
-
reddit = get_reddit_client()
|
| 47 |
posts = []
|
|
|
|
| 48 |
for post in reddit.subreddit("all").search(brand, sort="new", limit=limit):
|
| 49 |
posts.append(post)
|
| 50 |
time.sleep(1)
|
| 51 |
-
_search_cache[brand] = (time.time(), posts)
|
| 52 |
return posts
|
| 53 |
except Exception as e:
|
| 54 |
-
logging.warning(f"
|
| 55 |
time.sleep(delay)
|
| 56 |
delay *= 2
|
| 57 |
return []
|
|
|
|
| 6 |
|
| 7 |
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), '..', '.env'))
|
| 8 |
|
| 9 |
+
# Load multiple clients
|
| 10 |
clients = []
|
| 11 |
+
for i in range(1, 4): # Only up to 3 clients
|
|
|
|
| 12 |
cid = os.getenv(f"REDDIT_CLIENT_ID_{i}")
|
| 13 |
+
csecret = os.getenv(f"REDDIT_CLIENT_SECRET_{i}")
|
| 14 |
agent = os.getenv(f"REDDIT_USER_AGENT_{i}")
|
| 15 |
username = os.getenv(f"REDDIT_USERNAME_{i}")
|
| 16 |
password = os.getenv(f"REDDIT_PASSWORD_{i}")
|
| 17 |
+
|
| 18 |
+
if cid and csecret and agent and username and password:
|
| 19 |
+
reddit = praw.Reddit(
|
| 20 |
+
client_id=cid,
|
| 21 |
+
client_secret=csecret,
|
| 22 |
+
user_agent=agent,
|
| 23 |
+
username=username,
|
| 24 |
+
password=password,
|
|
|
|
| 25 |
)
|
| 26 |
+
reddit.read_only = False
|
| 27 |
+
clients.append(reddit)
|
| 28 |
|
| 29 |
+
if not clients:
|
| 30 |
+
raise ValueError("No valid Reddit clients loaded from environment variables.")
|
| 31 |
|
| 32 |
def get_reddit_client():
|
| 33 |
return clients[int(time.time()) % len(clients)]
|
| 34 |
|
| 35 |
def search_reddit(brand, limit=10, retries=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
delay = 1
|
| 37 |
for attempt in range(retries):
|
| 38 |
try:
|
|
|
|
| 39 |
posts = []
|
| 40 |
+
reddit = get_reddit_client()
|
| 41 |
for post in reddit.subreddit("all").search(brand, sort="new", limit=limit):
|
| 42 |
posts.append(post)
|
| 43 |
time.sleep(1)
|
|
|
|
| 44 |
return posts
|
| 45 |
except Exception as e:
|
| 46 |
+
logging.warning(f"[Retry {attempt+1}] Reddit search failed: {e}")
|
| 47 |
time.sleep(delay)
|
| 48 |
delay *= 2
|
| 49 |
return []
|