Thursday, February 19, 2015

Python, Tweepy, and Twitter.....Oh My!!!!

      If you landed here from twitter chances are you clicked a link provided by a bot.  This is not uncommon, everyday the access to easy to use development tools becomes more widespread.  Having not written a line of code since the late 80's imagine my surprise at how quickly a site like  codecademy walk me through entry level python.  Six days after the whim to check out python found me looking for a entry level project to solidify my newly acquired knowledge, and expose how sorely it was lacking.  So why a twitter bot?  I do not take things overly seriously.  That being said, I respect that other people do.  In twitter there is from my perspective a unique venue of real world interaction on a scale normally reserved for a few, along with low impact.  Meaning I am unlikely to cause anyone harm while messing around and learning, So while this is not intended in anyway to be a tutorial. After 6 days of an online course, and 3 days of playing, I hardly qualify.  The bot code currently looks like this:

#imports modulesimport tweepy, time, sys, random

#assigns text file arguments to variablesargfile = str(sys.argv[1])

#enter the corresponding information from your Twitter application:
CONSUMER_KEY = '123456'#keep the quotes, replace this with your consumer keyCONSUMER_SECRET = '123456'#keep the quotes, replace this with your consumer secret keyACCESS_KEY = '123456'#keep the quotes, replace this with your access tokenACCESS_SECRET = '123456'#keep the quotes, replace this with your access token secretauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

#sets up tweepy apiapi = tweepy.API(auth)


def hourlytweet():
    filename=open(argfile,'r')
    f=filename.readlines()
    filename.close()
    api.update_status(status=f)
    print("hourly tweet posted")


def follow_unfollow():
    followers = api.followers_ids()
    friends = api.friends_ids()

    for z in followers:
        if z not in friends:
            api.create_friendship(z)

    for z in friends:
        if z not in followers:
            api.destroy_friendship(z)
    print("follow, unfollow done")

def search_retweet():
        search_terms= ['python','technology','raspberry pi','anonymous','twitter bots']
        g = random.choice(search_terms)
        results = api.search(q=g, count=1)
        for u in results:
            api.retweet(u.id)
            print("retweeted top result for", g)



x=1y=2
while x<y:
    localtime = time.asctime(time.localtime(time.time()))
    hour = int(localtime[11] + localtime[12])
    print('current hour is:',hour)
    if hour>5 and hour<24:
        hourlytweet()
        time.sleep(1700+random.randint(200,400))
        search_retweet()
    elif hour == 24:
        follow_unfollow()
    time.sleep(1700+random.randint(200,400))


It currently has the basic functions, between 6am, and midnight local time, it will tweet from a textfile, than wait roughly 30 min. Perform a search on a term selected randomly from a list, and retweet the top result. wait roughly 30min and start over. At midnight, it will check for new followers and follow them, then check for unfollowers, and unfollow them.  Next I'll be looking at adding some more functionality, perhaps checking for mentions, and favorites.  Maybe adding in an exit condition, other than a crash or keyboard break.

Thanks for reading
and always make up your own damn mind

Jack
aka
PanseyBard

No comments:

Post a Comment