1- import os
21import logging
32from datetime import datetime , timedelta
4-
53import boto3
64import feedparser
75import pytz
8-
9-
10- TABLE_ARN = os .environ ["DYNAMODB_TABLE_ARN" ]
11- ARTIFACT_TYPE = "newsletter"
6+ from constants import ARTIFACT_TYPE , TABLE_ARN , FEEDS
127
138# Logging Configuration
149logging .getLogger ().setLevel (logging .INFO )
1510
11+ class NewsFeedFetcher :
12+ def __init__ (self , feed_name , feed_url ):
13+ """
14+ Initialize the RSS feed fetcher with a name and feed URL.
15+ :param feed_name: A descriptive name for the feed (e.g., "Bleeping Computer")
16+ :param feed_url: The RSS feed URL to fetch articles from
17+ """
18+ self .feed_name = feed_name
19+ self .feed_url = feed_url
20+
21+ def fetch_articles (self ):
22+ """
23+ Fetch articles from the specified RSS feed.
24+ returns a list of dictionaries containing the articles
25+ """
26+ feed = feedparser .parse (self .feed_url )
27+ if feed .bozo :
28+ raise ValueError (f"Error parsing feed '{ self .feed_name } ': { feed .bozo_exception } " )
29+
30+ articles = []
31+ for entry in feed .entries :
32+ articles .append (
33+ {
34+ "title" : entry .title ,
35+ "link" : entry .link ,
36+ "published" : entry .get ("published" , "N/A" ),
37+ "summary" : entry .get ("summary" , "N/A" ),
38+ }
39+ )
40+ return articles
41+
42+ def __repr__ (self ):
43+ return f"NewsFeedFetcher(feed_name='{ self .feed_name } ', feed_url='{ self .feed_url } ')"
44+
1645
1746def main (event , _ ):
1847 """
@@ -21,12 +50,15 @@ def main(event, _):
2150 """
2251 logging .info ("Event: %s" , event )
2352
24- # Fetch articles from both feeds
25- bleeping_articles = fetch_bleeping_computer_rss ()
26- hacker_articles = fetch_hacker_news_rss ()
27-
28- # Combine articles from both feeds
29- all_articles = bleeping_articles + hacker_articles
53+ all_articles = []
54+ for feed_info in FEEDS :
55+ fetcher = NewsFeedFetcher (feed_info ["name" ], feed_info ["url" ])
56+ try :
57+ articles = fetcher .fetch_articles ()
58+ logging .info (f"Fetched { len (articles )} articles from { feed_info ['name' ]} ." )
59+ all_articles .extend (articles )
60+ except ValueError as e :
61+ logging .error (f"Error fetching articles from { feed_info ['name' ]} : { e } " )
3062
3163 # Get today's articles from the combined list
3264 latest_articles = get_latest_article_with_timezone (all_articles )
0 commit comments