thehellobot/hello.py
2015-03-17 20:28:42 +01:00

68 lines
1.6 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, tweepy, calendar, datetime, itertools, sys, time, random
max_length = 14
cke = "Consumer Key"
cse = "Consumer Secret"
ato = "Access Token"
ase = "Access Secret"
allowedchars = "1234567890abcdefghijklmnopqrstuvwxyz_"
tmpfile = "tmp"
bot_path = os.path.dirname(os.path.abspath(__file__))
id_file = os.path.join(bot_path, tmpfile)
auth = tweepy.OAuthHandler(cke, cse)
auth.set_access_token(ato, ase)
api = tweepy.API(auth)
chars = list(allowedchars)
try:
with open(id_file, "r") as file:
current = list(file.read())
if current[-1] == '\n':
del current[-1]
except IOError:
print "No savepoint found. Starting from the top."
current = []
def tweet(word):
greeting = random.randint(1,3)
if greeting == 1:
text = "Hi there, @%s, how are you?" % word
elif greeting == 2:
text = "Hello @%s! Have a nice day!" % word
else:
text = "How are you doing, @%s?" % word
try:
api.update_status(text.encode('utf-8'))
print text
except tweepy.error.TweepError, e:
print "Tweet failed: " + word
print e
def nexttweet(offset = 1):
global current
if offset > len(current):
current = [chars[0]] + current
elif current[-offset] == chars[-1]:
current[-offset] = chars[0]
nexttweet(offset + 1)
else:
current[-offset] = chars[chars.index(current[-offset]) + 1]
while len(current) <= max_length:
nexttweet()
try:
api.get_user("".join(current))
tweet("".join(current))
with open(id_file, "w") as file:
file.write("".join(current))
time.sleep(60)
except tweepy.error.TweepError, e:
print "User does not exist: " + "".join(current)