forked from jakobdylanc/llmcord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose_reddit.py
More file actions
254 lines (208 loc) · 8.33 KB
/
Copy pathdiagnose_reddit.py
File metadata and controls
254 lines (208 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
"""
Diagnostic script for Reddit monitoring configuration
Run this to check if your Reddit setup is configured correctly
"""
import os
import sys
import json
from pathlib import Path
import yaml
def print_section(title):
print(f"\n{'='*60}")
print(f" {title}")
print(f"{'='*60}\n")
def check_env_vars():
print_section("Environment Variables Check")
required_vars = {
'REDDIT_CLIENT_ID': 'Reddit API Client ID',
'REDDIT_CLIENT_SECRET': 'Reddit API Client Secret',
'REDDIT_REPORTGEN_SUBREDDITS': 'Subreddits to monitor',
'REDDIT_REPORTGEN_KEYWORDS': 'Keywords to search for',
'REDDIT_REPORTS_ENABLED': 'Enable Reddit reports',
}
optional_vars = {
'REDDIT_REPORTGEN_EXCLUDE_KEYWORDS': 'Keywords to exclude',
}
all_set = True
print("Required Variables:")
for var, desc in required_vars.items():
value = os.getenv(var)
if value:
# Mask sensitive values
if 'SECRET' in var or 'TOKEN' in var:
display = f"{value[:8]}..." if len(value) > 8 else "***"
else:
display = value
print(f" ✓ {var}: {display}")
else:
print(f" ✗ {var}: NOT SET - {desc}")
all_set = False
print("\nOptional Variables:")
for var, desc in optional_vars.items():
value = os.getenv(var)
if value:
print(f" ✓ {var}: {value}")
else:
print(f" - {var}: not set (optional)")
return all_set
def check_config_file():
print_section("Configuration File Check")
config_path = Path("config_report.yaml")
if not config_path.exists():
print(f"✗ Config file not found: {config_path}")
return False
print(f"✓ Config file exists: {config_path}")
try:
with open(config_path, 'r', encoding='utf-8') as f:
raw_config = yaml.safe_load(f)
# Expand environment variables
def expand_env(obj):
if isinstance(obj, dict):
return {k: expand_env(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [expand_env(v) for v in obj]
elif isinstance(obj, str):
return os.path.expandvars(obj)
return obj
config = expand_env(raw_config)
reddit_config = config.get('reddit', {})
print("\nReddit Configuration:")
print(f" enabled: {reddit_config.get('reddit_reports_enabled')}")
client_id = reddit_config.get('client_id', '')
if client_id and not client_id.startswith('$'):
print(f" client_id: {client_id[:8]}... (set)")
else:
print(f" client_id: {client_id} (NOT RESOLVED)")
client_secret = reddit_config.get('client_secret', '')
if client_secret and not client_secret.startswith('$'):
print(f" client_secret: ****** (set)")
else:
print(f" client_secret: {client_secret} (NOT RESOLVED)")
subreddits = reddit_config.get('subreddits', [])
if isinstance(subreddits, str):
if subreddits.startswith('$'):
print(f" subreddits: {subreddits} (NOT RESOLVED)")
else:
subs_list = [s.strip() for s in subreddits.split(',') if s.strip()]
print(f" subreddits: {subs_list} ({len(subs_list)} subreddits)")
elif isinstance(subreddits, list):
print(f" subreddits: {subreddits} ({len(subreddits)} subreddits)")
else:
print(f" subreddits: {subreddits} (INVALID TYPE)")
keywords = reddit_config.get('keywords', [])
if isinstance(keywords, str):
if keywords.startswith('$'):
print(f" keywords: {keywords} (NOT RESOLVED)")
else:
kw_list = [k.strip() for k in keywords.split(',') if k.strip()]
print(f" keywords: {kw_list} ({len(kw_list)} keywords)")
elif isinstance(keywords, list):
print(f" keywords: {keywords} ({len(keywords)} keywords)")
else:
print(f" keywords: {keywords}")
return True
except Exception as e:
print(f"✗ Error loading config: {e}")
return False
def check_directories():
print_section("Data Directories Check")
dirs_to_check = [
"data/reddit/events",
"data/reddit/searches",
"data/logs",
"reports",
]
for dir_path in dirs_to_check:
p = Path(dir_path)
if p.exists():
items = list(p.glob("*"))
print(f" ✓ {dir_path}: exists ({len(items)} items)")
if items:
for item in items[:5]: # Show first 5
print(f" - {item.name}")
if len(items) > 5:
print(f" ... and {len(items) - 5} more")
else:
print(f" - {dir_path}: does not exist (will be created on first run)")
def check_reddit_api():
print_section("Reddit API Connection Test")
try:
import praw
print("✓ praw library is installed")
except ImportError:
print("✗ praw library is NOT installed")
print(" Install with: pip install praw")
return False
client_id = os.getenv('REDDIT_CLIENT_ID')
client_secret = os.getenv('REDDIT_CLIENT_SECRET')
if not client_id or not client_secret:
print("✗ Reddit API credentials not set in environment variables")
return False
try:
print("\nAttempting to connect to Reddit API...")
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent="daocord/0.1 diagnostic script",
)
# Try to fetch one post from r/test
subreddit = reddit.subreddit("test")
for post in subreddit.hot(limit=1):
print(f"✓ Successfully connected to Reddit API")
print(f" Test post: {post.title[:50]}...")
return True
except Exception as e:
print(f"✗ Failed to connect to Reddit API: {e}")
return False
def check_logs():
print_section("Recent API Call Logs")
log_path = Path("data/logs/reddit_calls.jsonl")
if not log_path.exists():
print(f" No log file found at {log_path}")
print(" (This is normal if you haven't run the monitor yet)")
return
try:
with open(log_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
if not lines:
print(" Log file is empty")
return
print(f" Found {len(lines)} log entries")
print("\n Last 5 entries:")
for line in lines[-5:]:
try:
entry = json.loads(line)
ts = entry.get('ts', 'unknown')
endpoint = entry.get('endpoint', 'unknown')
status = entry.get('status', entry.get('error', 'unknown'))
print(f" {ts}: {endpoint} - {status}")
except:
print(f" (unparseable entry)")
except Exception as e:
print(f" Error reading log: {e}")
def main():
print("\n" + "="*60)
print(" REDDIT MONITORING DIAGNOSTIC TOOL")
print("="*60)
results = {}
results['env_vars'] = check_env_vars()
results['config'] = check_config_file()
check_directories()
results['api'] = check_reddit_api()
check_logs()
print_section("Summary")
if all(results.values()):
print("✓ All checks passed! Your Reddit monitoring should work.")
print("\nNext steps:")
print(" 1. Run the bot: python main_report.py")
print(" 2. Or test manually: python -m tools.reddit_search search \"dogs\" --config config_report.yaml --verbose")
else:
print("✗ Some checks failed. Please fix the issues above.")
print("\nCommon fixes:")
print(" 1. Set environment variables in your system or .env file")
print(" 2. Get Reddit API credentials from: https://www.reddit.com/prefs/apps")
print(" 3. Install dependencies: pip install -r requirements.txt")
return 0 if all(results.values()) else 1
if __name__ == "__main__":
sys.exit(main())