13.5. Demo: Only positive news#

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

Let’s look at something we could try to do to improve the mental health for our users: Only show positive news!

We’ll use sentiment analysis again, but this time we’ll get posts from the news subreddit, but only display the posts with a positive sentiment.

Would this actually improve someone’s mental health? It’s hard to say! But we can see something that we might try out if we wanted to improve mental health of our users.

13.5.1. Normal Bluesky Setup#

We’ll start by doing our normal steps including these helper functions:

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

13.5.2. Load sentiment analyis code#

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!

13.5.3. Search for “news”#

We’ll now run a search for nows and then we’ll try to filter for just positive sentiment.

search for posts#

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

# go through each reddit submission
for post in search_results:
    print(post.record.text)
    print()
Breaking news: A lovely cat took a nice long nap today!

Breaking news: Someone said a really mean thing on the internet today!

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

Breaking news: All the horrors of the universe revealed at last!

13.5.4. Search through news submissions and only display good news#

Now we will make a different version of the code that computes the sentiment of each submission title and only displays the ones with positive sentiment.

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

# go through each reddit submission
for post in search_results:
        
    #calculate sentiment
    post_sentiment = sia.polarity_scores(post.record.text)["compound"]
    
    if(post_sentiment > 0):
        print(post.record.text)
        print()
Breaking news: A lovely cat took a nice long nap today!

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

13.5.5. Try it out on real Bluesky#

If you want, you can skip the fake_atrproto step and try your own search on real Bluesky.

Did it work like you expected?

You can also only show negative sentiment submissions (sentiment < 0) if you want to see only bad news.