Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (86 sloc) 3.1 KB
# coding=utf-8
import time
import tweepy
from random import randint
# OAuth keys and tokens
consumer_key = 'Your_Twitter_App_Developer_Consumer_Key'
consumer_secret = 'Your_Twitter_App_Developer_Consumer_Secret'
access_token = 'Your_Twitter_App_Developer_Access_Token'
access_token_secret = 'Your_Twitter_App_Developer_Access_Secret'
all_rules = []
fdebug_path = '/home/pi/Tweetbot/tweetlog.log'
fdebug = open(fdebug_path, 'a')
fdebug.write('\n' + time.strftime("%Y-%m-%d %H:%M:%S") + ' Starting \n')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' import time \n')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' import tweepy \n')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' import randint \n')
fdebug.close()
fswagrules_path = '/home/pi/Tweetbot/swagrules.txt'
ftweetedrules_path = '/home/pi/Tweetbot/tweetedrules.txt'
# OAuth Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Twitter connection
api = tweepy.API(auth)
try:
fsr = open(fswagrules_path, 'r')
all_rules = fsr.readlines()
fsr.close()
except:
fdebug = open(fdebug_path, 'a')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' swag rules could not be opened\n')
fdebug.close()
if len(all_rules) > 0:
number_of_rules = len(all_rules)
rule_to_tweet = randint(0, number_of_rules - 1)
tweet = all_rules[rule_to_tweet]
tweet = tweet.rstrip()
else:
tweet = ''
rule_to_tweet = 0
fdebug = open(fdebug_path, 'a')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' Rule to tweet: ' + tweet + '\n')
fdebug.close()
if len(tweet) > 0 and len(tweet) <= 140:
api.update_status(status=tweet)
elif len(tweet) > 140:
tweetparts = []
long_tweet = tweet
while len(long_tweet) > 135:
index = long_tweet.rfind(' ', 0, 135)
tweetparts.append(long_tweet[0:index])
long_tweet = long_tweet[index:]
tweetparts.append(long_tweet)
j = len(tweetparts)
for tweetstring in tweetparts:
actual_tweet = str(tweetparts.index(tweetstring)+1) + '/' + str(j) + ':' + tweetstring
api.update_status(status=actual_tweet)
else:
pass
try:
ftr = open(ftweetedrules_path, 'a')
ftr.write(tweet + '\n')
ftr.close()
except:
fdebug = open(fdebug_path, 'a')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' tweeted rules could not be written down\n')
fdebug.close()
# remove tweeted rule from rule set
if len(all_rules) > 0:
all_rules.pop(rule_to_tweet)
if len(all_rules) > 0:
all_rules[len(all_rules) - 1] = all_rules[len(all_rules)-1].rstrip()
try:
fsr = open(fswagrules_path, 'w')
i = 0
while i < len(all_rules):
fsr.write(str(all_rules[i]))
i += 1
fsr.close()
fdebug = open(fdebug_path, 'a')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' removed tweeded rule from swagrule pool\n')
fdebug.close()
except:
fdebug = open(fdebug_path, 'a')
fdebug.write(time.strftime("%Y-%m-%d %H:%M:%S") + ' swag rules could not be opened to remove tweeted rule\n')
fdebug.close()