-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
207 lines (174 loc) · 7.21 KB
/
main.py
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
from feed_parser import FeedParser
from summarizer import Summarizer
from email_sender import EmailSender
import json
from datetime import datetime
import schedule
import time
from logger import setup_logger, log_section, log_summary
import argparse
import logging
from article_cache import ArticleCache
import os
from pathlib import Path
logger = setup_logger(__name__)
# Set logger to only show INFO and above (will skip DEBUG level messages)
logger.setLevel(logging.INFO)
def load_config():
logger.info("Attempting to load configuration")
config_paths = [
'/app/config/config.json', # Cloud Run mounted volume
'config.json' # Local development
]
for path in config_paths:
try:
logger.info(f"Trying to load config from: {path}")
with open(path, 'r') as f:
config = json.load(f)
logger.info(f"Successfully loaded config from {path}")
return config
except FileNotFoundError:
logger.warning(f"Config not found at {path}")
continue
raise FileNotFoundError("Could not find config.json in any location")
def process_feeds(feed_urls):
logger.info("Starting feed processing")
summarizer = Summarizer()
summaries = []
for feed_url in feed_urls:
logger.info(f"Processing feed: {feed_url}")
parser = FeedParser(feed_url)
articles = parser.parse_feed()
for article in articles:
logger.debug(f"Summarizing article: {article['title']}")
summary = summarizer.summarize(article['text'])
if summary:
summaries.append({
'title': article['title'],
'link': article['link'],
'summary': summary,
'published': article['published'].isoformat(),
'source': feed_url,
'category': article['category']
})
# Save summaries
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_dir = '/Users/azadneenan/Documents/RSS-Summarizer/articles'
filename = f'summaries_{timestamp}.json'
filepath = f'{output_dir}/{filename}'
logger.info(f"Saving {len(summaries)} summaries to {filepath}")
try:
with open(filepath, 'w') as f:
json.dump(summaries, f, indent=2)
logger.info("Summaries saved successfully")
except Exception as e:
logger.error(f"Error saving summaries: {str(e)}")
# Send email
logger.info("Initiating email sending")
email_sender = EmailSender()
if email_sender.send_email(summaries):
logger.info("Email sent successfully")
else:
logger.error("Failed to send email")
def run_daily():
logger = setup_logger(__name__)
summarizer = Summarizer()
cache = ArticleCache()
all_summaries = []
logger.info("Starting daily run")
# Load config first
try:
config = load_config()
feed_urls = config['feed_urls']
logger.info(f"Loaded {len(feed_urls)} feed URLs from config")
except Exception as e:
logger.error(f"Failed to load config: {str(e)}")
raise
total_feeds = len(feed_urls)
processed_feeds = 0
for feed_url in feed_urls:
processed_feeds += 1
logger.info(f"Progress: {processed_feeds}/{total_feeds} feeds ({(processed_feeds/total_feeds)*100:.1f}%)")
logger.info(f"Processing feed: {feed_url}")
try:
parser = FeedParser(feed_url) # Create parser once
articles = parser.parse_feed() # Parse feed once
if not articles:
logger.warning(f"No articles found in {feed_url}")
continue
logger.info(f"Found {len(articles)} articles in {feed_url}")
# Process only new articles
new_articles = []
for article in articles:
is_cached = cache.is_processed(article['link'])
logger.info(f"Article {article['link']} cached: {is_cached}")
if not is_cached:
new_articles.append(article)
if new_articles:
logger.info(f"Found {len(new_articles)} new articles to process")
for article in new_articles:
summary = summarizer.summarize(article['text'])
if summary:
cache.add_article(article['link'])
summary_with_metadata = {
'title': article['title'],
'link': article['link'],
'published': article['published'],
'source': feed_url,
**summary # Unpack the summary and category
}
all_summaries.append(summary_with_metadata)
else:
logger.info("Found 0 new articles to process")
except Exception as e:
logger.error(f"Error processing feed {feed_url}: {str(e)}")
if all_summaries:
# Convert datetime objects to strings before saving
formatted_summaries = []
for summary in all_summaries:
formatted_summary = summary.copy()
if 'published' in formatted_summary:
formatted_summary['published'] = formatted_summary['published'].isoformat()
formatted_summaries.append(formatted_summary)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# Get project root directory and create output path
project_root = Path(__file__).parent
output_dir = project_root / 'articles' / 'summaries'
os.makedirs(output_dir, exist_ok=True)
filename = f'summaries_{timestamp}.json'
filepath = output_dir / filename
try:
with open(filepath, 'w') as f:
json.dump(formatted_summaries, f, indent=2)
logger.info(f"Summaries saved successfully to {filepath}")
except Exception as e:
logger.error(f"Error saving summaries: {str(e)}")
# Send email
logger.info("Initiating email sending")
email_sender = EmailSender()
if email_sender.send_summaries(all_summaries):
logger.info("Email sent successfully")
else:
logger.error("Failed to send email")
else:
logger.warning("No summaries to save or send")
def main():
logger = setup_logger(__name__)
logger.info("Starting Feed Summarizer application")
# Schedule jobs
schedule.every().day.at("09:00").do(run_daily)
schedule.every().day.at("17:00").do(run_daily)
logger.info("Scheduled jobs: 9:00 AM and 5:00 PM daily")
while True:
schedule.run_pending()
time.sleep(60)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='RSS Feed Summarizer')
parser.add_argument('--run-once', action='store_true', help='Run once and exit')
args = parser.parse_args()
if args.run_once:
logger.info("Running single execution")
run_daily()
else:
logger.info("Starting scheduled execution")
main()