

In all three cases, the pricing of the service includes a free tier that is absolutely sufficient for my light use.Īs for the code itself, in the case of Clarifai and Sightengine we are presented with a dedicated library. It may surprise you, but I chose three:įor each of these, I had to create an account and an API key, immediately stored in secrets.py. The last part of nsfw_bot.py I wrote concerns connecting to the nudity detectors and retrieving their scoring for a given picture. MyStreamListener=BotStreamer() stream=tweepy.Stream(auth, myStreamListener) stream.filter(track=[' Connecting to AI detector APIs Using that class, I instantiated a Stream object and connected to the Twitter API using it:.If an image is accompanying the tweet, it passes the three arguments the tweet_image_ratings() function described above:Ĭlass BotStreamer(tweepy.StreamListener): # Called when a new status arrives which is passed down from the on_data method of the StreamListener def on_status(self, status): username = _name status_id = status.id if 'media' in status.entities: for image in status.entities: tweet_image_ratings(image, username, status_id) BotStreamer captures the username and tweet id. I created a class inheriting from StreamListener, overriding the on_status method.Tweepy makes it easier to use the Twitter streaming API by hiding authentication, session handling and reading incoming messages under the hood.

In order to keep the connection open and be able to respond to all incoming and upcoming tweets, I need the streaming API. The last Tweepy-related bit to fill in - tweet_image_ratings() arguments, that is, the original tweet data. In order to retweet, I need to temporarily store the image as temp.jpg: def tweet_image_ratings(pic_url, username, status_id): # Take the pic url and tweet the various NSFW ratings clarifai_score = check_clarifai(pic_url) deepai_score = check_deepai(pic_url) sightengine_score = check_sightengine(pic_url) filename = 'temp.jpg' request = requests.get(pic_url, stream=True) if request.status_code = 200: with open(filename, 'wb') as image: for chunk in request: image.write(chunk) api.update_with_media(filename, status='Is it NSFW, + clarifai_score+deepai_score+sightengine_score, in_reply_to_status_id=status_id) My function consults the image with all the three image detectors and replies to the tweets with the corresponding scores and a retweet of the image. The function accepts - you guessed it - 3 arguments: Then I set the tweet_image_ratings() function exactly for that. Tweepy’s API class provides access to the entire Twitter RESTful API methods (see API Reference for more information). The api variable will be the entry point for the operations I will perform with Twitter - in this case, posting replies with the image scores. I used the newly-created credentials to connect to Twitter using Tweepy’s OAuthHandler class: auth = tweepy.OAuthHandler(consumer_key, consumer_secret) t_access_token(access_token, access_secret) api = tweepy.API(auth) Only now do I start to write the main script ( nsfw_bot.py in my case). That way I can share the rest of the code on GitHub without needing to worry about the secrecy of my API access keys: #Twitter API consumer_key = '.' consumer_secret = '.' access_token = '.' access_secret = '.' Connecting to Twitter In fact, I stored them straight away in a separate file called secrets.py. Space Invaders guard my passwords hide yours as well!
