-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathdaily_knowledge_bot.py
More file actions
296 lines (235 loc) · 8.82 KB
/
Copy pathdaily_knowledge_bot.py
File metadata and controls
296 lines (235 loc) · 8.82 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""
Daily Knowledge Bot
This script uses the Perplexity API to fetch an interesting fact about a rotating
topic each day. It can be scheduled to run daily using cron or Task Scheduler.
Usage:
python daily_knowledge_bot.py
Requirements:
- requests
- python-dotenv
"""
import os
import json
import logging
import sys
import random
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Union
import requests
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("daily_knowledge_bot.log"),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger("daily_knowledge_bot")
class ConfigurationError(Exception):
"""Exception raised for errors in the configuration."""
pass
class PerplexityClient:
"""Client for interacting with the Perplexity API."""
BASE_URL = "https://api.perplexity.ai/chat/completions"
def __init__(self, api_key: str):
"""
Initialize the Perplexity API client.
Args:
api_key: API key for authentication
"""
if not api_key:
raise ConfigurationError("Perplexity API key is required")
self.api_key = api_key
self.headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
def get_fact(self, topic: str, max_tokens: int = 150, temperature: float = 0.7) -> str:
"""
Fetch an interesting fact about the given topic.
Args:
topic: The topic to get a fact about
max_tokens: Maximum number of tokens in the response
temperature: Controls randomness (0-1)
Returns:
An interesting fact about the topic
Raises:
requests.exceptions.RequestException: If API request fails
"""
data = {
"model": "sonar",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that provides interesting, accurate, and concise facts. Respond with only one fascinating fact, kept under 100 words."
},
{
"role": "user",
"content": f"Tell me an interesting fact about {topic} that most people don't know."
}
],
"max_tokens": max_tokens,
"temperature": temperature
}
response = requests.post(self.BASE_URL, headers=self.headers, json=data, timeout=30)
response.raise_for_status()
result = response.json()
return result["choices"][0]["message"]["content"]
class DailyFactService:
"""Service to manage retrieval and storage of daily facts."""
def __init__(self, client: PerplexityClient, output_dir: Path = None):
"""
Initialize the daily fact service.
Args:
client: Perplexity API client
output_dir: Directory to save fact files
"""
self.client = client
self.output_dir = output_dir or Path.cwd()
self.output_dir.mkdir(exist_ok=True)
# Default topics
self.topics = [
"astronomy",
"history",
"biology",
"technology",
"psychology",
"ocean life",
"ancient civilizations",
"quantum physics",
"art history",
"culinary science"
]
def load_topics_from_file(self, filepath: Union[str, Path]) -> None:
"""
Load topics from a configuration file.
Args:
filepath: Path to the topics file (one topic per line)
"""
try:
topics_file = Path(filepath)
if topics_file.exists():
with open(topics_file, "r") as f:
topics = [line.strip() for line in f if line.strip()]
if topics:
self.topics = topics
logger.info(f"Loaded {len(topics)} topics from {filepath}")
else:
logger.warning(f"No topics found in {filepath}, using defaults")
else:
logger.warning(f"Topics file {filepath} not found, using defaults")
except Exception as e:
logger.error(f"Error loading topics file: {e}")
def get_daily_topic(self) -> str:
"""
Select a topic for today.
Returns:
The selected topic
"""
day = datetime.now().day
# Prevent index errors with modulo and ensure we don't get -1 on the last day
topic_index = day % len(self.topics)
if topic_index == 0 and len(self.topics) > 0:
topic_index = len(self.topics) - 1
else:
topic_index -= 1
return self.topics[topic_index]
def get_random_topic(self) -> str:
"""
Select a random topic as a fallback.
Returns:
A randomly selected topic
"""
return random.choice(self.topics)
def get_and_save_daily_fact(self) -> Dict[str, str]:
"""
Get today's fact and save it to a file.
Returns:
Dictionary with topic, fact, and file information
"""
# Try to get the daily topic, fall back to random if there's an error
try:
topic = self.get_daily_topic()
except Exception as e:
logger.error(f"Error getting daily topic: {e}")
topic = self.get_random_topic()
logger.info(f"Getting today's fact about: {topic}")
try:
fact = self.client.get_fact(topic)
# Save the fact
timestamp = datetime.now().strftime("%Y-%m-%d")
filename = self.output_dir / f"daily_fact_{timestamp}.txt"
with open(filename, "w") as f:
f.write(f"DAILY FACT - {timestamp}\n")
f.write(f"Topic: {topic}\n\n")
f.write(fact)
logger.info(f"Fact saved to {filename}")
return {
"topic": topic,
"fact": fact,
"filename": str(filename)
}
except requests.exceptions.RequestException as e:
logger.error(f"API request error: {e}")
raise
except Exception as e:
logger.error(f"Unexpected error: {e}")
raise
def load_config() -> Dict[str, str]:
"""
Load configuration from environment variables or .env file.
Returns:
Dictionary of configuration values
"""
# Load environment variables from .env file if present
load_dotenv()
# Get API key from environment variables
api_key = os.environ.get("PERPLEXITY_API_KEY")
# Get output directory from environment variables or use default
output_dir = os.environ.get("OUTPUT_DIR", "./facts")
# Get topics file path from environment variables
topics_file = os.environ.get("TOPICS_FILE", "./topics.txt")
return {
"api_key": api_key,
"output_dir": output_dir,
"topics_file": topics_file
}
def main():
"""Main function that runs the daily knowledge bot."""
try:
# Load configuration
config = load_config()
# Validate API key
if not config["api_key"]:
logger.error("API key is required. Set PERPLEXITY_API_KEY environment variable or add it to .env file.")
sys.exit(1)
# Initialize API client
client = PerplexityClient(config["api_key"])
# Create output directory
output_dir = Path(config["output_dir"])
output_dir.mkdir(exist_ok=True)
# Initialize service
fact_service = DailyFactService(client, output_dir)
# Load custom topics if available
if config["topics_file"]:
fact_service.load_topics_from_file(config["topics_file"])
# Get and save today's fact
result = fact_service.get_and_save_daily_fact()
# Display the results
print(f"\nToday's {result['topic']} fact: {result['fact']}")
print(f"Saved to: {result['filename']}")
except ConfigurationError as e:
logger.error(f"Configuration error: {e}")
sys.exit(1)
except requests.exceptions.RequestException as e:
logger.error(f"API communication error: {e}")
sys.exit(2)
except Exception as e:
logger.error(f"Unhandled error: {e}")
sys.exit(3)
if __name__ == "__main__":
main()