Spaces:
Running
Running
Update alisto_project/backend/ingest_reddit.py
Browse files
alisto_project/backend/ingest_reddit.py
CHANGED
|
@@ -143,15 +143,12 @@ async def process_post(post):
|
|
| 143 |
|
| 144 |
# 5. Main Loop (POLLING MODE)
|
| 145 |
async def scrape_reddit():
|
| 146 |
-
log("π Connecting to Reddit API (
|
| 147 |
|
| 148 |
client_id = os.getenv("REDDIT_CLIENT_ID")
|
| 149 |
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
|
| 150 |
|
| 151 |
-
|
| 152 |
-
log("β CRITICAL ERROR: Client ID or Secret missing in .env")
|
| 153 |
-
return
|
| 154 |
-
|
| 155 |
reddit = asyncpraw.Reddit(
|
| 156 |
client_id=client_id,
|
| 157 |
client_secret=client_secret,
|
|
@@ -160,28 +157,32 @@ async def scrape_reddit():
|
|
| 160 |
password=os.getenv("REDDIT_PASSWORD")
|
| 161 |
)
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
last_id = None
|
| 165 |
|
| 166 |
while True:
|
| 167 |
try:
|
| 168 |
subreddit = await reddit.subreddit(SUBREDDITS)
|
| 169 |
-
|
| 170 |
-
# Fetch ONLY 1 post to minimize bandwidth
|
| 171 |
async for post in subreddit.new(limit=1):
|
| 172 |
if post.id != last_id:
|
| 173 |
-
log(f"π₯ New Post
|
| 174 |
await process_post(post)
|
| 175 |
last_id = post.id
|
| 176 |
-
else:
|
| 177 |
-
# Silence "no new post" messages to keep logs clean
|
| 178 |
-
pass
|
| 179 |
-
|
| 180 |
-
# Wait 60 seconds (The Fix)
|
| 181 |
await asyncio.sleep(60)
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
-
log(f"β οΈ Connection glitch
|
|
|
|
| 185 |
await asyncio.sleep(120)
|
| 186 |
|
| 187 |
await reddit.close()
|
|
|
|
| 143 |
|
| 144 |
# 5. Main Loop (POLLING MODE)
|
| 145 |
async def scrape_reddit():
|
| 146 |
+
log("π Connecting to Reddit API (Debug Mode)...")
|
| 147 |
|
| 148 |
client_id = os.getenv("REDDIT_CLIENT_ID")
|
| 149 |
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
|
| 150 |
|
| 151 |
+
# 1. Initialize Reddit
|
|
|
|
|
|
|
|
|
|
| 152 |
reddit = asyncpraw.Reddit(
|
| 153 |
client_id=client_id,
|
| 154 |
client_secret=client_secret,
|
|
|
|
| 157 |
password=os.getenv("REDDIT_PASSWORD")
|
| 158 |
)
|
| 159 |
|
| 160 |
+
# --- THE TRUTH CHECK ---
|
| 161 |
+
# This tells us if the password actually worked
|
| 162 |
+
log(f"π§ DEBUG CHECK: Am I logged in as {os.getenv('REDDIT_USERNAME')}?")
|
| 163 |
+
is_read_only = await reddit.read_only
|
| 164 |
+
log(f"π IS READ ONLY MODE? {is_read_only}")
|
| 165 |
+
# True = Password Failed (You are Anonymous -> BLOCKED)
|
| 166 |
+
# False = Password Worked (You are Logged In)
|
| 167 |
+
# -----------------------
|
| 168 |
+
|
| 169 |
+
log(f"ποΈ ALISTO ACTIVE: Polling r/{SUBREDDITS}...")
|
| 170 |
+
|
| 171 |
last_id = None
|
| 172 |
|
| 173 |
while True:
|
| 174 |
try:
|
| 175 |
subreddit = await reddit.subreddit(SUBREDDITS)
|
|
|
|
|
|
|
| 176 |
async for post in subreddit.new(limit=1):
|
| 177 |
if post.id != last_id:
|
| 178 |
+
log(f"π₯ New Post: {post.title}")
|
| 179 |
await process_post(post)
|
| 180 |
last_id = post.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
await asyncio.sleep(60)
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
+
log(f"β οΈ Connection glitch: {e}")
|
| 185 |
+
# If 403 happens, we know exactly why now based on the Read Only check
|
| 186 |
await asyncio.sleep(120)
|
| 187 |
|
| 188 |
await reddit.close()
|