Search code examples
pythontweepy

Getting all users mentioned in a tweet text and their user ID's


When I search for a tweet containing certain words, I want to check for users mentioned in those tweets that say specifically 'follow @someoneSo' so I could keep track of them and check out their profiles if they seem like relevant accounts, and hopefully, follow some accounts that I like at the end of it.

Is there something similar to Discord's message.mentions.users where it returns a list of users mentioned in a tweet text?

For example, if a tweet text = 'dvffegregre rg ADQEW follow @chad @lee efkneknen' or tweet text = 'dvffegregre rg ADQEW follow these guys @chad @lee efkneknen', my script would be able to follow @chad and @lee


Solution

  • text = 'dvffegregre rg ADQEW follow @chad @lee efkneknen'
    textlist = text.split(' ')
    users = []
    if 'follow' in textlist:
        for subtext in textlist:
            if subtext.startswith('@'):
                API.create_friendship(subtext.strip('@'))
    

    I think this should work for your purpose.