All Tutorials

Your One-Stop Destination for Learning and Growth

Title: Exploring the Depths of Disqus Comments with Python

In today's digital world, online comments have become an integral part of engaging users and fostering discussions. One popular commenting system is Disqus, which offers various features like threaded replies, user profiles, and moderation tools. In this post, we'll explore how to interact with Disqus comments using Python.

Prerequisites

Before diving into the code, make sure you have the following:

  1. A Disqus account with a public thread (for testing purposes).
  2. Access to your thread's shortname and API key from your Disqus settings.

Setting up the Environment

We'll be using the requests library for making HTTP requests, so make sure it is installed:

pip install requests

Interacting with Disqus

Now that we have everything set up let's write some Python code to fetch comments from a thread.

Fetching Comments

Create a new file called disqus_comments.py and add the following code:

import requests

API_KEY = "your-api-key"
SHORTNAME = "thread-shortname"

url = f"https://disqus.com/api/3/threads/{SHORTNAME}.json?api_key={API_KEY}"
response = requests.get(url)
data = response.json()

if data["success"]:
    comments = data["children"]
    for comment in comments:
        print(f"Comment from {comment['author']['username']}: {comment['content']}")
else:
    print("Error:", data["error"])

Replace your-api-key and thread-shortname with your actual API key and thread shortname, respectively.

Run the script using:

python disqus_comments.py

This will print out each comment's content along with the username of the author.

Adding a Comment

To add a new comment, update the disqus_comments.py file as follows:

import requests

API_KEY = "your-api-key"
SHORTNAME = "thread-shortname"
COMMENT = "Hello Disqus community!"

url = "https://disqus.com/api/3/posts/"
data = {
    "thread_id": SHORTNAME,
    "text": COMMENT,
}
response = requests.post(url, json=data, params={"api_key": API_KEY})

if response.status_code == 201:
    print("Comment added successfully!")
else:
    print("Error:", response.json()["error"])

Now run the updated script and check your Disqus thread for the new comment!

Conclusion

With this simple Python script, you can now fetch, display, and even add comments to a Disqus thread. This opens up endless possibilities, such as creating automated tools for moderating or analyzing user feedback on your Disqus-powered website. Stay tuned for more posts on advanced use cases!

Published May, 2019