Tuesday, 19 February 2019

How to Build Twitter, Instagram, and Reddit Bots Using Python

build-bots-python

It’s 2019. You’ve heard of bots. There are chat bots, email bots, web scraping bots, and, social media bots. Have you created a bot yet? It’s surprisingly easy. Probably why they’re all over the place. Embrace our bot overlords by joining their rank.

In this article, I’ll show you how you can leverage Python to interact with your Twitter, Reddit, and Instagram accounts automatically.

Working With Python

This walk-through uses Python as its language of choice. Python’s ease-of-use advantages outweigh any speed deficiencies it has compared to other languages. You can download Python for almost any OS. Use the editor of your choice.

Python distributions come associated with the utility called pip. At the time of writing, there are over 168,000 libraries available for download using pip. For the bots you will create today, only three are needed.

From the command line, you can install the libraries needed using these three lines.

pip install --upgrade InstagramAPI
pip install --upgrade tweepy pip install --upgrade praw

Now you’ll be able to import these libraries where needed.

Getting Application Keys and Secrets

A wise philosopher once said “Secrets, secrets are no fun. Secrets, secrets hurt someone.” Respectfully, the bots need secrets. The next step in setting up your bots is to allow them access to the API of each site. To do this, you’ll need an application key or secret.

As you collect keys, secrets, tokens, and more (sounds like a fun game!), put them all into a single file called “credentials.py.” This file would look something like this:

# keys

twitter_consumer_key = ''
twitter_consumer_secret = ''
twitter_access_token = ''
twitter_access_token_secret = ''

reddit_client_id = ''
reddit_client_secret = ''
reddit_user_agent = ''

instagram_client_id = ''
instagram_client_secret = ''

Very important privacy suggestion: do not commit this file any source control (e.g. Git). You do not want anyone else to have these keys and secrets. They are called secrets for a reason.

Setting Up Twitter Access

Check out our guide to setting up a Twitter bot with Node.js will show you how to create your Twitter app. Note that Twitter will take some time to verify your account and ensure you’re not creating a spam account.

After following those steps, copy the consumer key, consumer secret, access token, and access token secret into your credentials.py file.

Setting Up Reddit Access

Getting your Reddit client ID and secret is very similar to Twitter. You’ll need to register an app as a Reddit User Agent. After doing this, you should absolutely put “Reddit User Agent” on your resume.

Sign in to the Reddit account of your choosing. Navigate to the apps preferences page and Click create another app. Name your user agent something useful. Select script for the purpose of your app. The about URL can be left blank. Fill in any address for the redirect box.

Reddit App Bot Python

After you Press create app, the following screen will be presented to you with a list of your apps. The digits under your application name represent the client ID and the client secret is located below that. Copy these values to your credentials.py file.

Reddit App API Secret Python

Setting Up Instagram Access

Instagram access differs from Twitter and Reddit. Instead of registering an app, you use your username and password. I suggest creating a separate public account and using those details for your bot.

Accessing Reddit With Praw

Using praw is a two-step process. First, setting up the access is a single function call to the Reddit method of praw. Then, using the initialized object, searching a subreddit is done using the new() method.

import praw
from credentials import *

my_reddit = praw.Reddit(client_id=reddit_client_id, client_secret=reddit_client_secret, user_agent=reddit_user_agent)

sub_name = 'technology'
max_posts = 10

for submission in my_reddit.subreddit(sub_name).new(limit=max_posts):
        print(submission.title)

Change the sub_name variable to get posts from different subreddits. Instead of new(), other methods such as hot() are available to get posts.

Searching Instagram Hashtags Via InstagramAPI

The first step in using the Instagram API is setting up an object with the client ID and secret. Directly after that, call the login() method to complete set up.

from InstagramAPI import InstagramAPI
from credentials import instagram_client_id, instagram_client_secret

my_insta_api = InstagramAPI(instagram_client_id,instagram_client_secret)
my_insta_api.login()

Using the API is a little more complicated than its Reddit counterpart. In this example, the script uses getHashtagFeed to get a very large JSON response object from Instagram.

Searching through the response, the script looks for caption text and then prints out to the screen to view it.

get_hashtag = my_insta_api.getHashtagFeed(hashtag)

if get_hashtag != False:
 for item in my_insta_api.LastJson['items']:
 if 'caption' in item.keys() and 'text' in item['caption'].keys():
         caption = item['caption']['text']
 print(caption)

Add Images To Social Media

Moving forward, you may want your script to get the images in this hashtag feed. To do this, loop through the JSON response and find the media associated with each post. Here’s an implementation for extracting the information from the JSON response:

def get_images_from_hashtag(hashtag, num_images):
        images = []
        get_hashtag = my_insta_api.getHashtagFeed(hashtag)

        if get_hashtag == False:
                return images

        for item in my_insta_api.LastJson['items']:
                if item['media_type'] == 1 and 'image_versions2' in item.keys():
                        candidate = get_largest_image(item['image_versions2']['candidates'])
                        # get image 
                        filename = self.save_image_from_candidate(candidate['url'])
                        if filename != '':
                                # get status, save as tuple
                                caption = get_caption(item)
                                images.append((filename, caption))
                        if len(images) >= num_images:
                                break
 return images

There are two helper functions used in this function. Instagram sends a list of image “candidates” in the JSON response. Choose the largest of these images so that the media is displayed in its highest resolution.

def get_largest_image(candidates):
        candidate = {}
        pixels = 0
        for cand in candidates:
                # pick the highest resolution one
                res = cand['height']*cand['width']
                if res > pixels:
                        pixels = res
                        candidate = cand
        return candidate

Secondly, once the link to image is found, you can save the image locally by writing the content to a file. Using the requests library makes this straightforward.

def save_image_from_candidate(url):
 filename = ''
 response = requests.get(url)
 # check the response status code, 200 means good
 if response.status_code == 200: 
        filename = url.split("/")[-1].split('?')[0]
        with open(filename, 'wb') as f:
                f.write(response.content)
 return filename

Tweeting Out Media With Tweepy

First, you’ll need to set up Twitter access using your secrets and keys.

import tweepy
from credentials import *

tw_auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
tw_auth.set_access_token(twitter_access_token, twitter_access_token_secret)
tw_api = tweepy.API(tw_auth)

Creating a generic retweet bot is done in just a few lines.

for tweet in tweepy.Cursor(tw_api.search,q='MakeUseOf').items(10):
        try:
                tweet.favorite()
                tweet.retweet()
                time.sleep(2)
        except tweepy.TweepError as e:
                print(e.reason)
        except StopIteration:
                break

Many other things can be done with this library. To conclude this walk-through, you can use the get_images_from_hashtag function from the Instagram bot made earlier to tweet out images and captions.

Additionally, your Twitter bot can search Reddit for new posts and tweet those out as well. To put both of these functionalities together looks like this:

# use r/<hashtag> for reddit search
# and #<hashtag> for instagram search
hashtag = 'technology'
num_posts = 5

# tweet reddit info
reddit_posts = my_reddit.subreddit(hashtag).new(limit=num_posts)
for submission in reddit_posts:
        title = submission.title
        url = 'www.reddit.com{}'.format(submission.permalink)
        tweet_str = f'Reddit r/{sub} update:\n\n{title} #{sub} {url}'
        tweet_str = trim_to_280(tweet_str)
        tw_api.update(tweet_str)

# tweet instagram media
media_info = get_images_from_hashtag(hashtag, num_posts)
for (filename, message) in media_info:
        try:
                tweet_str = trim_to_280(message)
                tw_api.update_with_media(filename, status=tweet_str)
        except tweepy.TweepError as e:
                print(e.reason)
        except StopIteration:
                break

Your Python-Powered Social Media Bot Is Ready!

So, now you’ve got a bunch of code that pulls media from one site and posts to another. You can mix and match, too. Pull tweets and post to Reddit. Collect Instagram images for posting to subreddits. Make a bot and be a part of the real internet.

Lastly, to fully automate this process, you will want your bots to run in a loop on a server. This could be on your own computer with a time scheduler. Or, dust off that old Raspberry Pi that has been in your closet, and check out these sweet Twitter bot projects for the Pi.

Read the full article: How to Build Twitter, Instagram, and Reddit Bots Using Python



from MakeUseOf http://bit.ly/2GxzlZ0
via IFTTT

No comments:

Post a Comment