Skip to content

Instantly share code, notes, and snippets.

@senwerks
Last active February 7, 2025 06:45
Show Gist options
  • Save senwerks/a14c20fda26994470e50b77aab35a1fd to your computer and use it in GitHub Desktop.
Save senwerks/a14c20fda26994470e50b77aab35a1fd to your computer and use it in GitHub Desktop.
RSS to OpenAI
# /// script
# requires-python = ">=3"
# dependencies = ['openai', 'feedparser']
# ///
import feedparser
import openai
import json
OPENAI_API_KEY = 'OpenAI-key-goes-here'
# Define RSS feed URL
RSS_FEED_URL = "https://www.example.org/feed.rss" # Replace with your desired RSS feed URL
# Define keywords for filtering articles
KEYWORDS = [
"topic", "keyword", "etc"
]
# Fetch and parse the RSS feed
feed = feedparser.parse(RSS_FEED_URL)
# Construct RSS content as a formatted string
rss_content = f"Feed Title: {feed.feed.get('title', 'No Title')}\nFeed Link: {feed.feed.get('link', 'No Link')}\n\n"
articles = []
for entry in feed.entries:
article_data = {
"title": entry.get('title', 'No Title'),
"link": entry.get('link', 'No Link'),
"description": entry.get('summary', 'No Description'),
"published": entry.get('published', 'No Published Date')
}
articles.append(article_data)
rss_content += "\n".join(
f"Title: {a['title']}\nLink: {a['link']}\nPublished: {a['published']}\nDescription: {a['description']}\n"
for a in articles
)
# Construct the prompt for GPT-4
prompt = f"""
You are an AI assistant that processes RSS feed data and extracts relevant articles.
The provided text contains articles from an RSS feed. Extract only the articles that mention one of the following topics, or anything related to these topics:
{', '.join(KEYWORDS)}.
Reply with the list in pure valid JSON with no code markdown tags or any wrapper content.
Here is the RSS feed content:
{rss_content}
"""
# Call the OpenAI API
client = openai.OpenAI(api_key=OPENAI_API_KEY)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": "You are an AI that processes RSS feeds."},
{"role": "user", "content": prompt}],
temperature=0.7
)
print(response)
# Extract JSON response from GPT-4
filtered_articles = response.choices[0].message.content
# Save the JSON output
output_file = "filtered_articles.json"
with open(output_file, "w", encoding="utf-8") as file:
file.write(filtered_articles)
print(f"Filtered articles saved to {output_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment