Skip to content

Commit d6b98b5

Browse files
authored
Merge pull request #14 from Deadpool2000/patch-1
Update handler.py
2 parents 9cb1f08 + 9654bb6 commit d6b98b5

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
Main handler.
3-
"""
4-
51
import os
62
import logging
73
from datetime import datetime, timedelta
@@ -11,7 +7,7 @@
117
import pytz
128

139

14-
TABLE_ARN = os.environ["DYNAMODB_TABLE_ARN"]
10+
TABLE_ARN = "none" # os.environ["DYNAMODB_TABLE_ARN"]
1511
ARTIFACT_TYPE = "newsletter"
1612

1713
# Logging Configuration
@@ -25,11 +21,22 @@ def main(event, _):
2521
"""
2622
logging.info("Event: %s", event)
2723

28-
latest_articles = get_latest_article_with_timezone(fetch_hacker_news_rss())
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
30+
31+
# Get today's articles from the combined list
32+
latest_articles = get_latest_article_with_timezone(all_articles)
2933

3034
logging.info("Latest articles: %s", latest_articles)
31-
links = [article["link"] for article in latest_articles]
32-
return publish_message_to_table(links)
35+
36+
# Extract links from all articles
37+
newsletter_links = [article["link"] for article in latest_articles]
38+
39+
return publish_message_to_table(newsletter_links)
3340

3441

3542
def get_latest_article_with_timezone(articles, timezone_str="UTC"):
@@ -41,25 +48,25 @@ def get_latest_article_with_timezone(articles, timezone_str="UTC"):
4148
todays_articles = []
4249
for article in articles:
4350
date_str = article["published"]
44-
parsed_date = datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
45-
formatted_date = parsed_date.isoformat()
46-
pub_date = datetime.fromisoformat(formatted_date.replace("Z", "+00:00"))
47-
pub_date = pub_date.astimezone(tz)
51+
# Handle 'GMT' timezone suffix
52+
if date_str.endswith(" GMT"):
53+
date_str = date_str.replace(" GMT", " +0000")
54+
try:
55+
parsed_date = datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
56+
except ValueError as e:
57+
logging.error("Error parsing date '%s': %s", date_str, e)
58+
continue
59+
pub_date = parsed_date.astimezone(tz)
4860
if pub_date.date() == today:
4961
todays_articles.append(article)
50-
5162
return todays_articles
5263

5364

54-
def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"):
55-
"""Fetch articles from The Hacker News RSS feed."""
56-
# Parse the RSS feed
65+
def fetch_bleeping_computer_rss(feed_url="https://www.bleepingcomputer.com/feed/"):
66+
"""Fetch articles from the Bleeping Computer RSS feed."""
5767
feed = feedparser.parse(feed_url)
58-
5968
if feed.bozo:
6069
raise ValueError(f"Error parsing feed: {feed.bozo_exception}")
61-
62-
# Extract articles
6370
articles = []
6471
for entry in feed.entries:
6572
articles.append(
@@ -70,19 +77,34 @@ def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"
7077
"summary": entry.summary,
7178
}
7279
)
73-
7480
return articles
7581

7682

83+
def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"):
84+
"""Fetch articles from The Hacker News RSS feed."""
85+
feed = feedparser.parse(feed_url)
86+
if feed.bozo:
87+
raise ValueError(f"Error parsing feed: {feed.bozo_exception}")
88+
h_articles = []
89+
for entry in feed.entries:
90+
h_articles.append(
91+
{
92+
"title": entry.title,
93+
"link": entry.link,
94+
"published": entry.published,
95+
"summary": entry.summary,
96+
}
97+
)
98+
return h_articles
99+
100+
77101
def publish_message_to_table(links: str):
78102
"""
79-
Sends a message to the SQS queue.
103+
Sends a message to the DynamoDB table.
80104
Returns a dictionary with the message and the message ID.
81105
"""
82-
logging.info("Sending message to SQS queue")
83-
106+
logging.info("Sending message to DynamoDB table")
84107
dynamodb_client = boto3.client("dynamodb")
85-
86108
for link in links:
87109
logging.info("Link: %s", link)
88110
response = dynamodb_client.put_item(
@@ -96,5 +118,4 @@ def publish_message_to_table(links: str):
96118
},
97119
)
98120
logging.info("Response: %s", response)
99-
100121
return {"message": "Message has been logged to DynamoDB!", "links": links}

0 commit comments

Comments
 (0)