Skip to content

Instantly share code, notes, and snippets.

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 johnconroy/675317 to your computer and use it in GitHub Desktop.
Save johnconroy/675317 to your computer and use it in GitHub Desktop.
Script to check if a twitter user posted in previous week.
#script to see whether a user is 'active' during the previous 7 days
#I had a large list of Twitter users. I wanted to see if they remained 'active' from one week to the next
# active means they posted a message at least once during the previous 7 days
#Requires DeWitt Clinton's "Python-Twitter" api wrapper
# some nice date-handling stuff in here
# In a seperate script, I iterated thru a list of Twitter users, passing each in turn into this script
import twitter
import AuthDetails #file containing a valid Twitter username/password
import datetime
def main(thisuser): #gets user from calling script
api=Authenticate()
if thisuser.GetStatus()==None: #means user has never tweeted
return 3
else:
date=GetLatestTweetDate(thisuser)
dayssincetweet=AmtDaysSinceTweet(date)
status1=CheckIfActive(dayssincetweet)
if status1==1:
#print thisuser.screen_name
#print"its active"
else:
#print"not active"
#print str(status1)
return status1 #returns either 'active' or 'notactive'
def GetLatestTweetDate(thisuser):
status=thisuser.GetStatus() #returns latest tweet obj
longdate=status.GetCreatedAt()
#longdate is a string. cut it down to Month+Date+Year...
date1=longdate[4:10]
date2=longdate[-4:]
strshortdate= date1+" "+date2
#now convert this string into a datetime object using strptime
shortdate=datetime.datetime.strptime(strshortdate, "%b %d %Y")
return shortdate
def AmtDaysSinceTweet(date):
#todays date
today=datetime.datetime.today()
sincetweet=today-date #returns a datetime.timedelta object(dd, m,s,millisecs...)
amtdays=int(sincetweet.days) #amt of days in timedelta
print amtdays," since tweet\n"
return amtdays
def CheckIfActive(dayssincetweet):
#user is 'active' if they've tweeted in past 7 days
if dayssincetweet>7:
return 0
else:
print"active"
return 1
def Authenticate():
#authenticate session
api=twitter.Api(username=AuthDetails.username, password=AuthDetails.password)
return api
if __name__ =="__main__":
main(thisuser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment