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:
helper function for atproto links#
NOTE: You don’t need to worry about the details of how this works, it just is here to make the code later easier to use.
import re #load a "regular expression" library for helping to parse text
from atproto import IdResolver # Load the atproto IdResolver library to get offical ATProto user IDs
# function to convert a post's special atproto "at" URI to a weblink url
def getWebLinkFromPost(post):
# Get the user id and post id from the weblink url
match = re.search(r'at://([^/]+)/app.bsky.feed.post/([^/]+)', post.uri)
if not match:
raise ValueError("Invalid Bluesky atproto post URL format.")
user_id, post_id = match.groups()
post_uri = f"https://bsky.app/profile/{user_id}/post/{post_id}"
return post_uri
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
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")
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.