Use Python to Fetch Followers and Monitor Post Status on DEV.to
Luca Liu
Posted on February 25, 2024
Interested in using Python to track your followers and post stats on DEV? This guide shows you how to use Python with the DEV API for effortless tracking.
Introduction to the DEV API
The DEV API gives developers access to DEV.to's data and functions. Whether you're integrating, automating, or exploring insights, it's your key to programmatically interact with DEV. Visit the DEV API documentation to learn more and get your API key from your DEV account settings.
Get your personal API key
Obtaining your personal API key is quick and easy. Simply navigate to your account settings on DEV.to and visit the 'Extensions' section or use this direct link: DEV Settings - Extensions. From there, you can generate your API key, enabling seamless interaction with the DEV API for all your development needs.
Fetching Followers with Python
One common scenario is to retrieve the list of followers associated with your DEV account. With Python and the requests
library, accomplishing this task is straightforward. Let's examine the simplified function to fetch followers:
import requests
def get_dev_followers(api_key):
# Headers for the request
headers = {
"Content-Type": "application/json",
"api-key": api_key,
}
followers = 0
page_number = 1
# Loop until we reach the last page of followers
while True:
params = {
"per_page": 1000, # Number of followers per page (default is 80)
"page": page_number
}
res = requests.get(url='https://dev.to/api/followers/users', headers=headers, params=params)
response_data = res.json()
current_page_followers = len(response_data)
followers += current_page_followers
# Check if the current page has no followers, indicating it's the last page
if current_page_followers == 0:
break
# Move to the next page
page_number += 1
return followers
# Example usage
# Replace 'YOUR_API_KEY' with your actual API key
total_followers = get_dev_followers(api_key='YOUR_API_KEY')
print('Total followers:', total_followers)
Replace 'YOUR_API_KEY'
with your actual DEV API key, and you're ready to utilize this function to effortlessly retrieve your followers.
Monitoring Post Status
Another valuable capability is to monitor various metrics associated with your published articles, such as comments, reactions, and page views. Let's see how Python can simplify this process:
import requests
def get_published_article_number(api_key):
# URL of the API endpoint
url = "https://dev.to/api/articles/me/published"
# Headers for the request
headers = {
"Content-Type": "application/json",
"api-key": api_key
}
# Send GET request
response = requests.get(url, headers=headers)
# Check if request was successful
if response.status_code == 200:
# Parse JSON response
articles = response.json()
# Calculate total counts
total_articles = len(articles)
total_comments = sum(article["comments_count"] for article in articles)
total_public_reactions = sum(article["public_reactions_count"] for article in articles)
total_page_views = sum(article["page_views_count"] for article in articles)
return total_articles, total_comments, total_public_reactions, total_page_views
else:
# If request was unsuccessful, print error message
print("Error:", response.text)
# Example usage:
# Replace 'YOUR_API_KEY' with your actual API key
result = get_published_article_number(api_key='YOUR_API_KEY')
print("Total articles:", result[0])
print("Total comments:", result[1])
print("Total public reactions:", result[2])
print("Total page views:", result[3])
By providing your API key, this function enables you to retrieve insightful data about your articles' performance on DEV.
Conclusion
With the combination of Python and the DEV API, you can effortlessly fetch followers and monitor the status of your posts on DEV. Whether you're a content creator, community member, or enthusiast, these simple yet powerful functions can significantly enhance your experience on the platform.
Explore more
Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.
Posted on February 25, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.