Tag Archives: tweethonbot. python

Python – Twitter bot to update status

0
Filed under python
Tagged as , , , , , , , , , ,

Author: slac3dork
Site: http://snippet.c0de.me
Summary: Twitter bot to update status. You need define your own time interval to tweet and your own messages. This script will update your status randomly within interval. This script for educational purpose only. Tested on Linux.
Usage: python tweethonbot.py -u your_username -p your_password

#!/usr/bin/python
#
#  _________      .__       .____   _______
# /   _____/ ____ |__|_____ |    |  \   _  \    ____
# \_____  \ /    \|  \____ \|    |  /  /_\  \  / ___\
# /        \   |  \  |  |_> >    |__\  \_/   \/ /_/  >
#/_______  /___|  /__|   __/|_______ \_____  /\___  /
#        \/     \/   |__|           \/     \//_____/
# http://snippet.c0de.me
# slac3dork[at]gmail[dot]com  

import urllib, random
from time import sleep, strftime
from optparse import OptionParser

username = ''
passwd = ''
update_uri = ''
interval = 60 # 60 seconds

# message goes here
msg = []
msg.append('twitter is great')
msg.append('I love tweeting')
msg.append('testing...')

# error message vars
con_error = 'Unable to connect. Check your username, password, and internet connection'
exit_msg = 'exiting...'

def init():
	usage = 'usage: %prog -u your_username -p your_password'
	parser = OptionParser(usage=usage)
	parser.add_option('-u', '--user', dest='username',
			help='your twitter username', metavar='your_username')
	parser.add_option('-p', '--password', dest='passwd',
			help='your twitter password', metavar='your_password')

	(options, args) = parser.parse_args()
	if (options.username and options.passwd):
		welcome()
		return options.username, options.passwd
	else:
		parser.print_help()
		exit(1)

def tweet():
	idx = random.randint(0,(len(msg) - 1))
	message = msg[idx]
	tweet_datetime = strftime(" (%Y-%m-%d %H:%M:%S)")

	data = urllib.urlencode({'status':message+tweet_datetime})
	try:
		urllib.urlopen(update_uri, data)
		print 'tweeting at%s' %(tweet_datetime)
	except IOError:
		print con_error
		exit(1)
def bot():
	while True:
		try:
			sleep(interval)
			tweet()
		except KeyboardInterrupt:
			print exit_msg
			exit(0)

def welcome():
	print '------------------------------------'
	print '[+] twitter bot to update twitter status'
	print '[+] tweethonbot.py'
	print '[+] coded by slac3dork'
	print '------------------------------------'

if (__name__ == '__main__'):
	username, passwd = init()
	update_uri = 'http://%s:%s@twitter.com/statuses/update.xml' %(username, passwd)
	bot()





python,logo,banner