10.4.2. Demo: Alt-text From Bluesky#

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

Let’s search for images and look up some alt-text on images posted to Bluesky

First we need to do our normal Bluesky login steps (and optional fake atproto step)

Log into atproto (or fake atproto)#

Load atproto library#

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

(Optional) Step 1b: 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

Step 2: Login to Bluesky#

To use this on your real Bluesky account, copy your bluesky account name and login into the code below, replacing our fake bluesky name and password.

# 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

Get alt text from a search of posts#

search posts#

First we do a search to find some bluesky posts. We have two example searches, one for “news” and one for posts made by the UW iSchool account that contain the text “UW”.

# Search posts for the text "news"
posts = client.app.bsky.feed.search_posts({'q': "news"}).posts

# Alternatively search posts that include text "UW" from the user "uwischool.bsky.social"
#posts = client.app.bsky.feed.search_posts({'q': "UW", 'author': "uwischool.bsky.social"}).posts

display alt-text of images in search results#

To see alt-text in posts, we check the “embed” for embedded information (such as images) and see if it contains information on “images”. If it does, then we display info about the post, and loop through the images, display the image alt-text and a link to the thumbnail image.

for post in posts:
    if(hasattr(post.embed, "images")):
        images = post.embed.images

        print("Post by " + post.author.handle + ":")
        print(" ---- ")
        print(" - Text: " + post.record.text)
            
        for image in images:
            print("  ---- ")
            print("  - alt text: " + image.alt)
            print("  - img link (thumbnail): " + image.thumb)

        print("---------------------------------------")
Post by fake_user.bsky.social:
 ---- 
 - Text: Breaking news: A lovely cat took a nice long nap today!
  ---- 
  - alt text: a peaceful cat
  - img link (thumbnail): fake_cat1.jpg
  ---- 
  - alt text: another view of a peaceful cat
  - img link (thumbnail): fake_cat2.jpg
---------------------------------------
Post by imaginary_user.bsky.social:
 ---- 
 - Text: Breaking news: Some grandparents made some yummy cookies for all the kids to share!
  ---- 
  - alt text: 
  - img link (thumbnail): img1.jpg
---------------------------------------
Post by mysterious_user.bsky.social:
 ---- 
 - Text: Breaking news: All the horrors of the universe revealed at last!
  ---- 
  - alt text: eldritch horror
  - img link (thumbnail): img2.jpg
---------------------------------------

Get alt text from a feeds#

load feed#

First we do a search to find some bluesky posts. We have two example feeds, the “Animal” feed and the “What’s hot” feed.

# "Animal" feed
feed = client.app.bsky.feed.get_feed({'feed': 'at://did:plc:2m44csq7rgph66s2kltbypxh/app.bsky.feed.generator/aaab56iiatpdo'}).feed

# "What's Hot" feed
# feed = client.app.bsky.feed.get_feed({'feed': 'at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot'}).feed

display alt-text of images in search results#

To see alt-text in posts, we check the “embed” for embedded information (such as images) and see if it contains information on “images”. If it does, then we display info about the post, and loop through the images, display the image alt-text and a link to the thumbnail image.

for postInfo in feed:
    post = postInfo.post
    if(hasattr(post.embed, "images")):
        images = post.embed.images

        print("Post by " + post.author.handle + ":")
        print(" ---- ")
        print(" - Text: " + post.record.text)
            
        for image in images:
            print("  ---- ")
            print("  - alt text: " + image.alt)
            print("  - img link (thumbnail): " + image.thumb)

        print("---------------------------------------")
Post by fake_user.bsky.social:
 ---- 
 - Text: Look at my cute dog!
  ---- 
  - alt text: my fluffy white dog looking confused
  - img link (thumbnail): fake_dog1.jpg
  ---- 
  - alt text: my fluffy white dog sleeping peacefully
  - img link (thumbnail): fake_dog2.jpg
---------------------------------------
Post by imaginary_user.bsky.social:
 ---- 
 - Text: Look at my cute cat!
  ---- 
  - alt text: 
  - img link (thumbnail): fake_cat.jpg
---------------------------------------

What alt-texts do you find helpful?#

In the above output, you are in a position where you can read the alt-text of the image, but you can’t see the image (unless you open up the links, which if you are looking at the fake_atproto output, there are no actual posts).

Without seeing the images you can hopefully see what makes alt-text useful or not. Posts without alt-text will be hard to make sense of, and some alt text on photos might tell you information about the photo, but not the information you need.