Skip to content

Instantly share code, notes, and snippets.

@Tazeg
Last active February 9, 2020 20:59
Show Gist options
  • Save Tazeg/a4b5ec7f083c6d038fa7 to your computer and use it in GitHub Desktop.
Save Tazeg/a4b5ec7f083c6d038fa7 to your computer and use it in GitHub Desktop.
Track Twitter followers/unfollowers and receive difference by email
#!/usr/bin/python3
# coding=utf-8
########################################################################
# AUTHOR : Jean-Francois GAZET
# WEB : http://www.jeffprod.com
# TWITTER : @JeffProd
# MAIL : jeffgazet@gmail.com
# LICENCE : GNU GENERAL PUBLIC LICENSE Version 2, June 1991
########################################################################
import difflib
import tweepy
import os.path
import smtplib
########################################################################
# CONF
########################################################################
# local file to record followers
FOLLOWERS_FILE = '/home/user/followers.txt'
# send changes by mail
EMAIL_TO = 'x@gmail.com'
EMAIL_FROM = 'x@gmail.com'
# gmail app paswd, see https://support.google.com/accounts/answer/185833
GMAIL_PSWD = 'xyxyxyxyxy'
# twitter API keys, see https://apps.twitter.com
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
# verbose
VERBOSE = False
########################################################################
# FUNCTIONS
########################################################################
# function to put a file into a string
def file_get_contents(fic):
with open(fic, 'r') as myfile:
return myfile.read()
# function to put a string into a file
def file_put_contents(filename, s):
f = open(filename, 'w')
f.write(s)
f.close()
def sendgmail(frm, to, subject, msg):
msgmail = "From: " + frm + "\n" + \
"To: " + to + "\n" + \
"Subject: " + subject + "\n\n" + \
msg
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(frm, GMAIL_PSWD)
server.sendmail(frm, to, msgmail)
server.quit()
log('email sent')
def log(msg):
if not VERBOSE:
return
print(msg)
########################################################################
# PROGRAM
########################################################################
# Auth Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# get current followers
c_followers = []
try:
log('getting current followers')
for user in tweepy.Cursor(api.followers).items():
c_followers.append(user.screen_name)
except tweepy.error.RateLimitError:
log('Rate limit exceeded')
exit(0)
c_followers.sort()
c_followers = "\n".join(c_followers)
# get previous followers from file
p_followers = ''
if os.path.isfile(FOLLOWERS_FILE):
log('getting previous followers')
p_followers = file_get_contents(FOLLOWERS_FILE)
else: # very first call, save followers list
log('followers file does not exists. writing it. exit')
file_put_contents(FOLLOWERS_FILE, c_followers)
exit(0)
# checking diff
result = ''
diff = difflib.ndiff(p_followers.splitlines(), c_followers.splitlines())
changes = [l for l in diff if l.startswith('+ ') or l.startswith('- ')]
for c in changes:
result += c + "\n"
# saving current followers to file
log('writing current followers to file')
file_put_contents(FOLLOWERS_FILE, c_followers)
# if lists are differents : send email
if result != '':
subject = 'Twitter followers status'
msg = "Followers changes :\n\n" + \
result + "\n" \
"Current followers : " + str(c_followers.count("\n"))
sendgmail(EMAIL_FROM, EMAIL_TO, subject, msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment