Skip to content

Instantly share code, notes, and snippets.

@antoinefortin
Last active July 27, 2023 04:07
Show Gist options
  • Save antoinefortin/203a15bd3dab41e38b63cc899916d6e8 to your computer and use it in GitHub Desktop.
Save antoinefortin/203a15bd3dab41e38b63cc899916d6e8 to your computer and use it in GitHub Desktop.
import tweepy
import time
import feedparser
## Review: This should by passed as argument from a function
# Twitter API keys and access tokens
API_KEY = 'YOUR_TWITTER_API_KEY'
API_SECRET_KEY = 'YOUR_TWITTER_API_SECRET_KEY'
ACCESS_TOKEN = 'YOUR_TWITTER_ACCESS_TOKEN'
ACCESS_TOKEN_SECRET = 'YOUR_TWITTER_ACCESS_TOKEN_SECRET'
## Review: Move that to a constructor
# Initialize Tweepy with Twitter API credentials
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# Review: Get new articles sould take a references to a URL where you build the data from each feed,
# def get_new_articles(rss_feed):
# Function to fetch news articles from CNN and Fox News RSS feeds
def get_news_articles():
cnn_feed_url = "CNN_RSS_FEED_URL" # Replace with the actual CNN RSS feed URL
fox_feed_url = "FOX_NEWS_RSS_FEED_URL" # Replace with the actual Fox News RSS feed URL
cnn_articles = fetch_rss_articles(cnn_feed_url)
fox_articles = fetch_rss_articles(fox_feed_url)
return cnn_articles + fox_articles # Review return only the object contrsucted by the fetch_rss_articles call
# Function to fetch news articles from an RSS feed
def fetch_rss_articles(feed_url):
parsed_feed = feedparser.parse(feed_url)
articles = []
for entry in parsed_feed.entries:
title = entry.get("title", "") # Why do you declare a new title for every entry ?
description = entry.get("description", "")# Why do you declare a new description for every entry ?
# articles.append({"title": entry.get("title"), "description": entry.get("description")})
articles.append({"title": title, "description": description})
return articles
#review: Again this should be handled inside a class where every tweet is its own entity.
# Function to post tweets on Twitter
def post_tweet(tweet):
try:
api.update_status(tweet)
print("Tweet posted:", tweet)
except tweepy.TweepError as e:
print("Error posting tweet:", e)
# Function to rewrite news articles using GPT-3 (conceptual implementation)
def rewrite_with_gpt3(article):
# Placeholder for GPT-3 API call to rewrite article description
# Replace this with actual GPT-3 API implementation
rewritten_description = "This is a rewritten description using GPT-3."
return rewritten_description
# Main function to run the bot
def main():
try:
print("Twitter bot is now running!")
while True:
# Fetch news articles
news_articles = get_news_articles()
# Compose and post tweets with news content
for article in news_articles:
# Rewrite the article description using GPT-3 (conceptual implementation)
rewritten_description = rewrite_with_gpt3(article)
tweet = f"{article['title']} - {rewritten_description} via {article['source']}"
post_tweet(tweet)
# Add a delay of 30 minutes (1800 seconds) between tweets
time.sleep(1800)
# Sleep for a specific interval (e.g., 1 hour) before fetching and posting more news
time.sleep(1800)
except KeyboardInterrupt:
# Handle keyboard interrupt (e.g., Ctrl+C) to gracefully stop the bot
print("Twitter bot stopped.")
except Exception as e:
print("Error occurred:", e)
if __name__ == "__main__":
main() # Why call main, as its already in main?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment