8.6.3. Demo: Sentiment Analysis on Bluesky#

Choose Social Media Platform: Bluesky | Reddit | Discord | No Coding

Now let’s try using sentiment analysis (and loop variables) on Bluesky:

Normal Bluesky Setup#

Now we can continue logging in to Bluesky and look through multiple posts.

load atproto library#

# Load some code called "Client" from the "atproto" library that will help us work with Bluesky
from atproto import Client

(optional) make a fake Bluesky connection with the fake_atproto library#

For testing purposes, we”ve added this line of code, which loads a fake version of atproto, so it wont actually connect to Bluesky. If you want to try to actually connect to Bluesky, don’t run this line of code.

%run ../../fake_apis/fake_atproto.ipynb
Fake atproto (bsky.app) is replacing the atproto.blue library. Fake atproto doesn't need real passwords, and prevents you from accessing real Bluesky

login to Bluesky#

# Login to Bluesky
# TODO: put your account name and password below

client = Client(base_url="https://bsky.social")
client.login("your_account_name.bsky.social", "m#5@_fake_bsky_password_$%Ds")
Fake atproto is pretending to set up a client connection to: https://bsky.social
Fake atproto is pretending log into your account: your_account_name.bsky.social

Do a search for posts on Bluesky#

We’ll now search for posts on Bluesky

Note: If you run this on real Bluesky, we can’t gurantee anything about how offensive what you might find is.

search_query = "news"
search_results = client.app.bsky.feed.search_posts({'q': search_query}).posts

Sentiment Analysis#

load sentiment analysis library and make analyzer#

import nltk
nltk.download(["vader_lexicon"])
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
[nltk_data] Downloading package vader_lexicon to
[nltk_data]     C:\Users\kmthayer\AppData\Roaming\nltk_data...
[nltk_data]   Package vader_lexicon is already up-to-date!

loop through posts, finding average sentiment#

We can now combine our previous examples of looping through the posts with what we just learned of sentiment analysis and looping variables to find the average sentiment of a set of posts.

num_posts = 0
total_sentiment = 0

for post in search_results:
    
    #calculate sentiment
    post_sentiment = sia.polarity_scores(post.record.text)["compound"]
    num_posts += 1
    total_sentiment += post_sentiment

    print("Sentiment: " + str(post_sentiment))
    print("   post text: " + post.record.text)
    print()


average_sentiment = total_sentiment / num_posts
print("Average sentiment was " + str(average_sentiment))
Sentiment: 0.784
   post text: Breaking news: A lovely cat took a nice long nap today!

Sentiment: 0.0
   post text: Breaking news: Someone said a really mean thing on the internet today!

Sentiment: 0.7088
   post text: Breaking news: Some grandparents made some yummy cookies for all the kids to share!

Sentiment: -0.6114
   post text: Breaking news: All the horrors of the universe revealed at last!

Average sentiment was 0.22034999999999996

We can now see the average sentiment of a set of bluesky posts!

If you use your bluesky bot keys, you can change the search_query to be whatever one you want and see whether people are posting positively or negatively in it.