Skip to main content

Python utilities for twitter bots

Project description

Twitter bot utils make it a little easier to set up a Twitter bot, with an eye to making config and command-line options easy to manage and reproduce. They’re intended for managing a small-to-medium-sized coterie of Twitter accounts on one machine. The package is a super-simple wrapper for the excellent Tweepy library. It also provides shortcuts for setting up command line tools with argparse.

This package is intended to assist with the creation of bots for artistic or personal projects. Don’t use it to spam or harrass people.

Works with Python 2.7 and 3.4 (2.6 & 3.3 probably work, too).

Install with pip install twitter_bot_utils.

Setting up a tweepy API

The main goal of Twitter bot utils is to create Tweepy instances with authentication data stored in a simple conf file. This gives botmakers a simple, reusable place to store keys outside of source control.

By default, twitter_bot_utils will read settings from a YAML or JSON config file. By default, it looks in the ~/ and ~/bots directories for files named “bots.yaml”, “bots.json”, or “botrc”. Custom config files can be set, too, of course.

from twitter_bot_utils import api

# Automatically check for a config file in the above-named directories
twitter = api.API('MyBotName')

# Specify a specific config file
twitter = api.API('MyBotName', config='path/to/config.yaml')

# This is possible, although you should consider just using Tweepy directly
twitter = api.API('MyBotName', consumer_key='...', consumer_secret='...', key='...', secret='...')

Twitter bot utils comes with some built-in command line parsers, and the API object will also happily consume the result of argparse.parser.parse_args() (see below for details).

Config file setup

Custom settings in the config are available at runtime, so use the config file for any special settings you want.

Example config file layout (This is YAML, JSON works, too):

# ~/bots.yaml

users:
    # twitter screen_name
    MyBotName:
        key: $oauth_key
        secret: $oauth_key_secret
        # The app key should match a key in apps below
        app: my_app_name
        custom_setting: 'hello world'

    other_bot:
        key: ...
        secret: ...
        app: my_app_name

apps:
    my_app_name:
        consumer_key: ...
        consumer_secret: ...

foo: bar

Using the config settings:

import twitter_bot_utils

# Look for config in the default places mentioned above:
twitter = twitter_bot_utils.api.API('MyBotName')

twitter.config['foo']
# returns 'bar'

# The current user and app are also available:
twitter.user['custom_setting']
# hello world

Setting a custom config file is simple:

# The config keyword argument will set a custom file location
twitter = twitter_bot_utils.api.API('MyBotName', config='special/file/path.yaml')

Recent tweets

Basically, the twitter_bot_utils.api.API object is a wrapper for Tweepy with some configuration reading options added. It also adds three convenience methods for finding recent tweets, since it’s often useful to know what a bot has done recently without setting up a whole backend for saving the bot’s tweets.

twitter = api.API('MyBotName')

twitter.last_tweet
# id of most recent tweet from MyBotName

twitter.last_reply
# id of most recent reply from MyBotName

twitter.last_retweet
# id of most recent retweet from MyBotName

# Example: what's happened since the last time the bot was active?
twitter.search('#botALLY', since_id=twitter.last_tweet)

Default Command Line Options

Some useful command line flags are available by default:

  • -n, --dry-run: Don’t tweet, just output to stdout

  • -v, --verbose: Log to stdout

  • -c, --config: path to a config file. This is a JSON or YAML file laid out according to the below format.

You can also pass authentication arguments with these options arguments.

  • --key: Twitter user key

  • --secret: Twitter user secret

  • --consumer-key: Twitter application consumer key

  • --consumer-secret: Twitter application consumer secret

Say this is yourapp.py:

import logging
import twitter_bot_utils

# This sets up an argparse.ArgumentParser with some default arguments, which are explained below
parser = twitter_bot_utils.setup_args('MyBot', description='Tweet something')

parser.add_argument('-m', '--my-arg', help="You're passing an argument to argparse.ArgumentParser")

args = parser.parse_args()

# Parse the default args. For instance, if --verbose is set, the logger will output to stdout.
twitter_bot_utils.defaults('MyBot', args)

# That's right, utils set up a logger for you.
# It has the same name as the first argument to setup_args
logger = logging.getLogger('MyBot')

# Do logic here to generate a tweet somehow
tweet = my_tweet_function(args.my_arg)

logger.info("Generated "+ tweet)

# Use args.dry_run to control tweeting
if not args.dry_run:
    twitter.update_status(tweet)

Then on the command line:

# Looks for settings in a config file (e.g. bots.yaml, see config section above)
# Outputs results to stdout, doesn't publish anything
$ python yourapp.py --dry-run --verbose
Generated <EXAMPLE TWEET>

# Authenicate with these values instead of the config file
$ python yourapp.py --verbose --consumer-key $ck --consumer-secret $cs --key $user_key --secret $user_secret
Generated <EXAMPLE TWEET 2>

Helpers

Checking for entities

Easily check if tweets have specific entities:

import twitter_bot_utils

# Don't set include_entities to False and expect the below to work
statuses = twitter.search('example search', include_entities=True)

status = status[0]

twitter_bot_utils.helpers.has_mention(status)
# returns True if status has one or more mentions, otherwise False

twitter_bot_utils.helpers.has_hashtag(status)
# returns True if status has one or more hashtags, otherwise False

twitter_bot_utils.helpers.has_media(status)
# returns True if status has one or more media entities (images, video), otherwise False

twitter_bot_utils.helpers.has_entities(status)
# returns True if status has any entities

# These also exist:
# twitter_bot_utils.helpers.has_url
# twitter_bot_utils.helpers.has_symbol

Filtering out entities

Easily remove entities from a tweet’s text.

import twitter_bot_utils

api = twitter_bot_utils.api.API('MyBotName')

results = api.search("special topic")

results[0].text
# 'This is an example tweet with a #hashtag and a link http://foo.com'

twitter_bot_utils.helpers.remove_entity(results[0], 'hashtags')
# 'This is an example tweet with a  and a link http://foo.com'

twitter_bot_utils.helpers.remove_entity(results[0], 'urls')
# 'This is an example tweet with a #hashtag and a link '

# Remove multiple entities with remove_entities.
twitter_bot_utils.helpers.remove_entities(results[0], ['urls', 'hashtags', 'media'])
# 'This is an example tweet with a  and a link '

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitter_bot_utils-0.6.5.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

twitter_bot_utils-0.6.5-py2-none-any.whl (21.4 kB view details)

Uploaded Python 2

File details

Details for the file twitter_bot_utils-0.6.5.tar.gz.

File metadata

File hashes

Hashes for twitter_bot_utils-0.6.5.tar.gz
Algorithm Hash digest
SHA256 8725c5bf836f9012ddb3f430c529674c2c2eae337dba897f41b006ec2814d910
MD5 8e2ad6d1ac5db7ecf4fc8ee96b0b6c15
BLAKE2b-256 c4a1f8d87fb1171b3ae8a2190938cfad95b80e9bf97df4dd09eb7c3d2dd4dfa7

See more details on using hashes here.

File details

Details for the file twitter_bot_utils-0.6.5-py2-none-any.whl.

File metadata

File hashes

Hashes for twitter_bot_utils-0.6.5-py2-none-any.whl
Algorithm Hash digest
SHA256 4f8296e217df9d569a9da4d372b408ab42374c7a46cdaaac4fc8a8f387e13615
MD5 d957b8d57854a13d5b42c42dc4d89dda
BLAKE2b-256 33c0bbfe7939a6ca383e06bc42f4647c8e6eedf71f9b727519ede9b8ddc7ab15

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page