
Welcome to Day 4 – The Awesome Edition!
Hey, beginner! You’ve already mastered fetching weather (Day 1), texting it (Day 2), and emailing alerts (Day 3). Now, let’s get flashy: we’re automating social media posts with a news API to keep your coffee shop’s X account buzzing. No more scrambling for content—let APIs and automation do the heavy lifting. This is so easy, social media pros might hate you for it! Ready for some awesome?
Why This Rocks
Posting manually to X every day is a grind. Why not let a robot scour the web for coffee news and share it for you? We’ll use:
NewsAPI: Grabs the latest coffee industry headlines.
X API: Posts them automatically.
A sprinkle of Python magic to tie it all together.
By the end, your X followers will see tweets like:“Fresh brew news: ‘Global Coffee Prices Spike in 2025’ via NewsAPI. Time for a latte?”
Real-Life Example: Coffee Shop Social Media
Picture this: you run a coffee shop’s X account. Your followers love coffee tips, but you’re too busy steaming oat milk to dig up news. Let’s automate it! Every day, your script finds a coffee-related article and tweets it—keeping your audience hooked without you touching a keyboard.
Step 1: Setting Up
You’ll need:
Python: With requests (from Day 1) and tweepy for X (pip install tweepy).
NewsAPI Key: Sign up at newsapi.org for a free key.
X API Access:
Apply for an X Developer account (free tier at developer.x.com).
Get your API Key, Secret, Access Token, and Secret (save them securely).
Your Day 4 Code: News + X Posts
Here’s the script:
python
import requests # For NewsAPI
import tweepy # For X API # NewsAPI setup
news_api_key = "YOUR_NEWSAPI_KEY"
news_url = f"https://newsapi.org/v2/everything?q=coffee&apiKey={news_api_key}&language=en&sortBy=publishedAt"
response = requests.get(news_url)
articles = response.json()["articles"]
latest_article = articles[0]
# Get the newest article
title = latest_article["title"]
url = latest_article["url"] tweet = f"Fresh brew news: ‘{title}’ via NewsAPI. Read more: {url}"
# X API setup
api_key = "YOUR_X_API_KEY"
api_secret = "YOUR_X_API_SECRET"
access_token = "YOUR_X_ACCESS_TOKEN"
access_secret = "YOUR_X_ACCESS_SECRET"
# Authenticate with X
auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, access_secret) api = tweepy.API(auth)
# Post to X
api.update_status(tweet)
print("Tweet posted! Here’s what went live:")
print(tweet)
How It Works

NewsAPI: Fetches the latest coffee-related article (sorted by date).
Tweet Crafting: Builds a short post with the article title and URL.
X API: Logs in with your credentials and posts the tweet.
Output: Prints the tweet so you can see what went live.
Try It Out!
Install tweepy with pip install tweepy.
Replace placeholders (YOUR_NEWSAPI_KEY, YOUR_X_API_KEY, etc.) with your real keys.
Save as coffee_news.py and run with python coffee_news.py.
Automate it daily (like Day 2/3) with Task Scheduler or cron:
0 9 * /usr/bin/python3 /path/to/coffee_news.py (9 AM example).
Check your X account—bam, instant coffee cred!
Why This Is Awesome
Low Effort, High Impact: One script keeps your socials alive.
API Power: NewsAPI + X API = a dynamic duo.
Social Media Hack: So easy, pros might cry foul!
Day 4 Takeaways
APIs can feed your social media effortlessly.
Automation turns you into a content machine.
You’re officially too cool for manual posts.
Comments