DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

How to Detect Environment Sentiment Shifts with the Pulsebit API (Python)

How to Detect Environment Sentiment Shifts with the Pulsebit API (Python)

The Problem

If you've ever tried to scrape sentiment data from various sources, you know how painful it can be. Websites change their layouts, APIs become deprecated, and you often find yourself wrestling with rate limits. The time spent on DIY scraping could be better utilized in building something useful. Enter the Pulsebit API, which gives you a powerful endpoint to pull sentiment analysis directly without the scraping hassle.

The Solution

The Pulsebit API provides a single endpoint, /news_semantic, that can deliver timely sentiment data efficiently. The recent data shows an environment sentiment score of +0.375 with a momentum of +1.400 and a confidence level of 0.870. This is particularly noteworthy because the sentiment score is rising, while the historical baseline for this topic has been significantly lower. You can capitalize on this trend without diving deep into scraping logic.

The Code

Here's how you can get started with the Pulsebit API in Python. First, make sure you have the requests library installed. You can do this via pip:

pip install requests
Enter fullscreen mode Exit fullscreen mode

Next, use the following code to fetch sentiment data:

import requests

def get_environment_sentiment():
    url = "https://pulsebit.lojenterprise.com/api/news_semantic"
    params = {
        "topic": "environment"
    }

    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred: {err}")
    except Exception as err:
        print(f"An error occurred: {err}")

data = get_environment_sentiment()
print(data)  # Display the response data
Enter fullscreen mode Exit fullscreen mode

Reading the Response

Now, let's break down the response you will receive:

  • sentiment_score: This is the core metric you're interested in. A score of +0.375 indicates a positive sentiment, suggesting that the general opinion about environmental topics is currently favorable.

  • momentum_24h: This value, +1.400, shows a significant increase in sentiment within the last 24 hours. This spike is particularly striking and suggests a shift in public discourse that you should be aware of.

  • confidence: With a confidence level of 0.870, you can be fairly certain that this sentiment analysis is reliable.

  • sentiment_index_0_100: At 68.75, this indicates how positive the sentiment is on a scale from 0 to 100. Anything above 70 is generally considered strong.

  • direction: The response shows "rising," reinforcing that the sentiment is not just positive but also increasing.

  • semantic_clusters: With 0 clusters, this suggests that the sentiment is not being driven by multiple distinct topics but is rather centralized around a single narrative.

  • region: The sentiment is global, which means this isn't localized to a specific area.

  • semantic_similarity_avg: At 0.251, this indicates a moderate level of similarity among the discussions surrounding environmental issues.

Three Use Cases

  1. Algo Alert: Set up an alert system that notifies you when the momentum exceeds a certain threshold. For example, a rise above +1.500 could trigger a notification for you to take action.

  2. Slack Bot: Create a Slack bot that regularly posts updates on sentiment shifts. With the current momentum being high, your bot could help your team stay informed about crucial discussions.

  3. Dashboard: Build a simple dashboard that visualizes sentiment trends over time. You could plot the sentiment score and momentum on a line graph to visualize spikes like the one we're seeing now.

Get Started

If you want to dive deeper into the capabilities of the Pulsebit API, check out the documentation. It's straightforward and will save you the hassle of building your own scrapers.

With the current environment sentiment rising sharply, now is a great time to leverage these insights in your applications!

Top comments (0)