Skip to content

Instantly share code, notes, and snippets.

@hrehman200
Last active July 25, 2020 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrehman200/303224084789068bb5bf87d2c13312b9 to your computer and use it in GitHub Desktop.
Save hrehman200/303224084789068bb5bf87d2c13312b9 to your computer and use it in GitHub Desktop.
from random import shuffle
from os.path import isfile, join
from os import listdir
import os
import tweepy
import time
auth = tweepy.OAuthHandler('xxx', 'xxx')
auth.set_access_token('xxx', 'xxx')
api = tweepy.API(auth)
INCLUDED_KEYWORDS = ["senior", "software", "engineer"]
EXCLUDED_KEYWORDS = ["available", "purchase"]
MIN_TWEET_LENGTH = 50
REPLY_TEXT = "Nice"
IMAGES_FOLDER = "./images/"
SECS_TO_WAIT_BEFORE_TWEETING = 5
TWEETS_TO_FETCH_PER_USER = 10
# 1000 - 1500 user accounts file
user_accounts = []
with open('./useraccounts.txt') as f:
user_accounts = [line.rstrip() for line in f]
# 2 images out of image folder
images = [f for f in listdir(IMAGES_FOLDER)
if isfile(join(IMAGES_FOLDER, f))]
shuffle(images)
for user_account in user_accounts:
for tweet in tweepy.Cursor(api.user_timeline, id=user_account, lang='en').items(TWEETS_TO_FETCH_PER_USER):
if all(word in tweet.text.lower().split() for word in INCLUDED_KEYWORDS) and all(word not in tweet.text.lower().split() for word in EXCLUDED_KEYWORDS) and len(tweet.text) >= MIN_TWEET_LENGTH:
status = api.get_status(tweet.id, tweet_mode="extended")
try:
print(status.retweeted_status.full_text)
except AttributeError: # Not a Retweet
time.sleep(SECS_TO_WAIT_BEFORE_TWEETING)
print(
f"Liking/Retweeting tweet {tweet.id} of {tweet.author.name}")
# Like
api.create_favorite(tweet.id)
# Retweet
api.retweet(tweet.id)
# Reply
media_ids = []
for img in images[:2]:
res = api.media_upload(IMAGES_FOLDER + img)
media_ids.append(res.media_id)
reply_status = "@%s %s" % (user_account, REPLY_TEXT)
api.update_status(status=reply_status,
in_reply_to_status_id=tweet.id_str, media_ids=media_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment