DEV Community

Pulsebit News Sentiment API
Pulsebit News Sentiment API

Posted on

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

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

The Problem

As developers, we often find ourselves scraping the web for business sentiment data. This process can be tedious and error-prone. Not only do you have to deal with various website structures, but also the legal and ethical implications of scraping. Furthermore, maintaining and updating your scrapers as website layouts change can be a significant ongoing burden.

What if there was a cleaner way to get this data without all the scraping hassle? Enter the Pulsebit API.

The Solution

Pulsebit provides a straightforward API that allows you to access business sentiment data effortlessly. With just one endpoint, you can retrieve sentiment analysis based on news articles and other data sources. This means you can focus on building your applications instead of wrestling with web scraping.

The key endpoint we’ll be using is GET /news_semantic.

The Code

Let’s jump right into the code. Below is a Python snippet that demonstrates how to call the Pulsebit API and fetch sentiment data:

import requests

# Set your API key here
API_KEY = 'your_api_key_here'
BASE_URL = 'https://api.pulsebit.co'

def get_business_sentiment():
    url = f"{BASE_URL}/news_semantic"
    headers = {
        'Authorization': f'Bearer {API_KEY}',
        'Content-Type': 'application/json'
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Error fetching data: {response.status_code} {response.text}")

if __name__ == "__main__":
    sentiment_data = get_business_sentiment()
    print(sentiment_data)
Enter fullscreen mode Exit fullscreen mode

Make sure to replace 'your_api_key_here' with your actual API key from Pulsebit.

Reading the Response

When you call the /news_semantic endpoint, you’ll receive a JSON response that contains several fields. Here's a breakdown of key components:

{
    "business_sentiment": 0.00,
    "momentum": 0.60,
    "clusters": 0,
    "confidence": 0.87
}
Enter fullscreen mode Exit fullscreen mode
  • business_sentiment: This field indicates the overall sentiment score for business-related news. A score of 0.00 means neutral sentiment.

  • momentum: This reflects the change in sentiment over time. A momentum score of 0.60 suggests a positive trend, which can be useful for identifying shifts.

  • clusters: This number represents how many distinct sentiment clusters have been identified. A value of 0 indicates no significant clusters, which could mean a lack of diverse sentiment.

  • confidence: This score (0.87) reflects how confident the model is in its predictions. A higher number indicates a more reliable sentiment reading.

Three Use Cases

  1. Algo Alert: Integrate this API into your trading algorithms. Set up alerts for significant changes in sentiment, allowing you to react quickly to market movements.

  2. Slack Bot: Create a Slack bot that periodically checks sentiment and posts updates in a channel. This could keep your team informed about market trends without constant manual checks.

  3. Dashboard: Build a dashboard to visualize sentiment trends over time. Use libraries like Plotly or Matplotlib to create graphs that dynamically display changes in sentiment and momentum.

Get Started

Ready to dive in? Head over to Pulsebit Documentation to learn more about the API, authentication, and additional endpoints. The API is straightforward, and with a few lines of code, you can harness the power of sentiment analysis in your applications.

By leveraging the Pulsebit API, you can say goodbye to the headaches of DIY scraping and focus on what really matters: building great software.

Top comments (0)