3

I'm trying to identify interesting people to follow on Twitter. To do this, I want to find users who post a tweet containing various keywords and then filter out users whose bios don't contain certain keywords.

I'm using the following code to identify the tweets, and then automatically follow the users who tweeted them:

naughty_words = ["example"]
good_words = ["example", "example"]
filter = " OR ".join(good_words)
blacklist = " -".join(naughty_words)
keywords = filter + blacklist

twitter = Twython(consumer_key, consumer_secret, access_token, 
                  access_token_secret)


search_results = twitter.search(q=keywords, count=10)
try:
    for tweet in search_results["statuses"]:
        try:
            st=tweet["entities"]["user_mentions"]
            if st != []:
                twitter.create_friendship(screen_name=st[0]["screen_name"])
        except TwythonError as e:
            print(e)
except TwythonError as e:
    print(e)

This code is working great, but I want to filter my results more, as this method returns a lot of users that I don't want to follow! Does anyone know how I could amend this to include a second filter that looks at users' bios?

Dloidean
  • 59
  • 7

1 Answers1

3

According to the Twitter Doc, you can search for users based on a query string. However, if I check the Twython API documentation, it seems that this call is not directly supported. Tweepy, on the other hand, provides a corresponding method API.search_users, see here.

I don't think that you can search for users and tweets in one request. So might might have to stick to your current tweet search, and check each tweet if you have already seen this users. If not, you have to get the user's profile and check if they satisfy your conditions (probably batches of users to limit the number of API calls).

Edit: You probably can use Twython to search for users as well. While it does not provide a dedicated method, it provides a generic method get where you can make calls to any endpoint. So it might look something like :

get('users/search', params={'q': 'soccer music -sex -porn'})

I haven't tried it myself, but that's what I can get from the Twython Docs.

Christian
  • 3,239
  • 5
  • 38
  • 79
  • Thanks! So to clarify, would you recommend that I try to do this using tweepy? – Dloidean Jan 24 '19 at 12:34
  • @Dloidean I'm not exactly sure what you're trying to do, i.e. when exactly do you want to establish a friendship? For example, if each valid user profile must contain at least one filter word but non blacklisted word, I would focus on the user search --since there are less users than tweets. But maybe the exact conditions for a friendship are different. By they way, you can explicitly state which words you don't want to be contained, (e.g. "soccer music -sex -porn"): https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators.html – Christian Jan 24 '19 at 12:53
  • I want to establish a friendship with a user who has: 1. Tweeted something that passes my keyword filter. 2. A bio/description which passes a second keyword filter. Only after both of these criteria have been fulfilled would the friendship be established, ideally. – Dloidean Jan 24 '19 at 13:27
  • Well, I would probably do it like described in my answer (but using the streaming instead of the search API): (1) Collect tweets based on a set of keywords. (2) For each tweet, check if you have already seen the users; if so ignore. (3) Once you have a batch of 50-100 new users -- I forgot the API limitations for that call -- get all their user profiles and filter them based on your criteria (4) If a user passes the filter, create friendship; all users are added to the set of known users. – Christian Jan 24 '19 at 13:40
  • Thank you, I'll definitely give it a try! – Dloidean Jan 24 '19 at 14:08