Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 46 additions & 25 deletions lambdas/security-newsletter/src/handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""
Main handler.
"""

import os
import logging
from datetime import datetime, timedelta
Expand All @@ -11,7 +7,7 @@
import pytz


TABLE_ARN = os.environ["DYNAMODB_TABLE_ARN"]
TABLE_ARN = "none" # os.environ["DYNAMODB_TABLE_ARN"]
ARTIFACT_TYPE = "newsletter"

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

latest_articles = get_latest_article_with_timezone(fetch_hacker_news_rss())
# Fetch articles from both feeds
bleeping_articles = fetch_bleeping_computer_rss()
hacker_articles = fetch_hacker_news_rss()

# Combine articles from both feeds
all_articles = bleeping_articles + hacker_articles

# Get today's articles from the combined list
latest_articles = get_latest_article_with_timezone(all_articles)

logging.info("Latest articles: %s", latest_articles)
links = [article["link"] for article in latest_articles]
return publish_message_to_table(links)

# Extract links from all articles
newsletter_links = [article["link"] for article in latest_articles]

return publish_message_to_table(newsletter_links)


def get_latest_article_with_timezone(articles, timezone_str="UTC"):
Expand All @@ -41,25 +48,25 @@ def get_latest_article_with_timezone(articles, timezone_str="UTC"):
todays_articles = []
for article in articles:
date_str = article["published"]
parsed_date = datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
formatted_date = parsed_date.isoformat()
pub_date = datetime.fromisoformat(formatted_date.replace("Z", "+00:00"))
pub_date = pub_date.astimezone(tz)
# Handle 'GMT' timezone suffix
if date_str.endswith(" GMT"):
date_str = date_str.replace(" GMT", " +0000")
try:
parsed_date = datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %z")
except ValueError as e:
logging.error("Error parsing date '%s': %s", date_str, e)
continue
pub_date = parsed_date.astimezone(tz)
if pub_date.date() == today:
todays_articles.append(article)

return todays_articles


def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"):
"""Fetch articles from The Hacker News RSS feed."""
# Parse the RSS feed
def fetch_bleeping_computer_rss(feed_url="https://www.bleepingcomputer.com/feed/"):
"""Fetch articles from the Bleeping Computer RSS feed."""
feed = feedparser.parse(feed_url)

if feed.bozo:
raise ValueError(f"Error parsing feed: {feed.bozo_exception}")

# Extract articles
articles = []
for entry in feed.entries:
articles.append(
Expand All @@ -70,19 +77,34 @@ def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"
"summary": entry.summary,
}
)

return articles


def fetch_hacker_news_rss(feed_url="https://feeds.feedburner.com/TheHackersNews"):
"""Fetch articles from The Hacker News RSS feed."""
feed = feedparser.parse(feed_url)
if feed.bozo:
raise ValueError(f"Error parsing feed: {feed.bozo_exception}")
h_articles = []
for entry in feed.entries:
h_articles.append(
{
"title": entry.title,
"link": entry.link,
"published": entry.published,
"summary": entry.summary,
}
)
return h_articles


def publish_message_to_table(links: str):
"""
Sends a message to the SQS queue.
Sends a message to the DynamoDB table.
Returns a dictionary with the message and the message ID.
"""
logging.info("Sending message to SQS queue")

logging.info("Sending message to DynamoDB table")
dynamodb_client = boto3.client("dynamodb")

for link in links:
logging.info("Link: %s", link)
response = dynamodb_client.put_item(
Expand All @@ -96,5 +118,4 @@ def publish_message_to_table(links: str):
},
)
logging.info("Response: %s", response)

return {"message": "Message has been logged to DynamoDB!", "links": links}
Loading