How to make a function that counts the number of Tweets that have alt-text

Viewed 84

So with the code below, I am using Tweepy to search for a list of tweets that contain images, printing the text of the tweet, and then printing whether or not they have alt text. If an image has alt text, it will print the alt-text after the text of the tweet and if it does not have alt text, it will print "None". What I want to do is add a function that will go through the searched tweets and if the alt string text is not equal to "No", it will add to a count and print that count out at the end. Any and all help is appreciated!

query = "from:taschagroup -is:retweet has:images"

tweets = client.search_recent_tweets(query=query,
                                    expansions='attachments.media_keys',
                                    media_fields=['alt_text']
                                    )
    
media = {m["media_key"]: m for m in tweets.includes['media']}

for tweet in tweets.data:
    
    print('https://twitter.com/twitter/statuses/' + str(tweet.id))
    
    print(tweet)
    
    print(tweet.data.get('attachments'))
    
    attachments = tweet.data['attachments']
    media_keys = attachments['media_keys']
    
    first_photo = media[media_keys[0]]
    
    print(first_photo.alt_text)
    print()

Sample output:

Staying nimble and open to change create research opportunities for MLIS student, Lindsey Schwartz. Read about Lindsey's research journey on the TASCHA blog
{'media_keys': ['3_1489681078075887616']}
black and white photograph of Lindsey Schwartz with fall leaves on the ground
0 Answers
Related