A Simple Python Script for Your Twitter Bot

A simple python script allowing a twitter bot to search for two separate terms in the public tweet stream, and retweet the results in an alternating pattern. In this case i use the search terms "black people" and "white people".



This is not intended to teach you how to set up your twitter bot with developers.twitter.com.

Once you have your API keys and access tokens, copy-paste them into the script where indicated.

Currently if the variable "a" is even it will retweet a recent post that contains "black people". If it is odd, it will retweet a popular tweet that contains "white people". so adding more search terms will involve implementing some other system that has as many states as the search has search terms.

You can change the rate at which you retweet by editing the lines with "time.sleep(x)". where x is the number of seconds you wish for the bot to wait in between retweets.

You can change the search terms by replacing any instance of "black people", or "white people", & "black people OR white people" with some other search terms.

If you can be bothered, you can introduce more search terms by following the format "black people OR white people OR asian people OR [...]", but if you decide to do this, you will need to change the rest of the code to account for more than 2 search terms.

This method doesn't search for the newest tweet constantly. It does a search, when you run the code, and iterates through the results based on the aforementioned criteria, until you stop running the code. I did it this way because my initial attempt was to retweet every new tweet that contains the search terms in an alternating pattern, but it was doing multiple retweets per second, due to the sheer relevance of the search terms.

I should have thought about a more elegant way to alternate between the search terms, but this is what you get. It was just a quick solution to alternate between the two search terms without the need to search for 1, tweet it, search for the other, tweet it, search for the first one again... etc. It's all under the same search stream thing, so you won't be making unnecessary duplicate search requests, multiple times per second, and annoy the twitter overlords, resulting in a ban.

Other twitter bots, such as the political accelerationist bot (https://twitter.com/accelerbot), which retweets based on search terms like accelerationism, accelerationist, etc, is much less likely to get banned from spamming new tweets, as the search terms are relatively rare compared to my search terms. Just keep in mind that too many bot tweets can result in a ban. This is why i used a time gate to restrict the frequency of activity.

I may or may not make further changes some time, such as confirming it hasn't retweeted the same tweet ID before, or only retweeting original\ tweets and not retweeting retweets of tweets, to avoid tweet duplication. TWEET.

You can see an example of the bot in use at: https://twitter.com/WhiteBlackRobot

the code is as follows:

import tweepy
import time

# Enter your credentials
CONSUMER_KEY = "xxx"
CONSUMER_SECRET = "xxx"
ACCESS_TOKEN = "xxx"
ACCESS_TOKEN_SECRET = "xxx"

# Authenticate to Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)

def main():

a=0
for tweet in tweepy.Cursor(api.search, '-filter:retweets AND -filter:replies "black people" OR "white people"', result_type="recent", lang="en").items():
try:
if "black people" in tweet.text:
if (a % 2 == 0):
print(tweet.text)
tweet.retweet()
a = a+1
time.sleep(599)
if "white people" in tweet.text:
if (a % 2 != 0):
print(tweet.text)
tweet.retweet()
a = a+1
time.sleep(601)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break

main()

Comments

Popular posts from this blog

A Message to Queensland University of Technology and the MATE Program

Estimation of the Rydberg Constant