-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstbot.py
More file actions
102 lines (86 loc) · 3.08 KB
/
Copy pathconstbot.py
File metadata and controls
102 lines (86 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tweepy
import consumer
def get_api():
"""Get authentication information and create API"""
auth = tweepy.OAuthHandler(consumer.CONSUMER_KEY, consumer.CONSUMER_SECRET)
auth.set_access_token(consumer.ACCESS_KEY, consumer.ACCESS_SECRET)
api = tweepy.API(auth)
return api
def post_tweet(tweet, api):
"""Post tweet"""
try:
api.update_status(tweet)
except tweepy.TweepError:
print("Couldn't post tweet. Quitting!")
quit()
def get_last_tweet(api, trump_handle, first_tweet):
"""Get last tweet"""
last_two_tweets = api.user_timeline(count=2)
try:
last_tweet = last_two_tweets[0].text
last_tweet2 = last_two_tweets[1].text
except IndexError:
# this must be a new account. post the beginning and quit
post_tweet(first_tweet, api)
quit()
# remove handle from last tweet
last_tweet = last_tweet[len(trump_handle):]
last_tweet2 = last_tweet2[len(trump_handle):]
# we get the tweets in reverse order, have to flip them
return last_tweet2 + last_tweet
def get_start_position(last_tweet, text):
"""Get current position in constitution based on last tweet"""
start_position = text.index(last_tweet)
start_position = start_position + len(last_tweet)
return start_position
def get_constitution():
"""Get the US Constitution. Weep if it cannot be found."""
try:
with open('const.txt', 'r') as c:
text = c.read()
return text
except FileNotFoundError:
print("Couldn't find the US Constitution.")
print("This is a sad day for America")
quit()
def construct_tweet(start_position, end_position, max, max_tweet_increment, text, trump_handle):
"""Construct the tweet"""
if start_position >= max:
# we are past the end, let's go back to the beginning
tweet = text[0:max_tweet_increment]
elif end_position >= max:
tweet = text[start_position:]
else:
tweet = text[start_position:end_position]
tweet = trump_handle + tweet
# get the last space in the tweet and rewind back
try:
if end_position < max:
last_space = tweet.rindex(' ')
else:
last_space = len(tweet)
except ValueError:
last_space = len(tweet)
tweet = tweet[:last_space]
return tweet
def main():
"""Main function"""
api = get_api() # get twitter api
trump_handle = '.@realDonaldTrump\n'
max_tweet_increment = 140 - len(trump_handle) # account for handle
text = get_constitution()
# set max words of constitution
max = len(text)
# get current position in the constitution
last_tweet = get_last_tweet(api, trump_handle, text[0:max_tweet_increment])
start_position = get_start_position(last_tweet, text)
end_position = start_position + max_tweet_increment
print(start_position)
tweet = construct_tweet(start_position, end_position, max, max_tweet_increment, text, trump_handle)
# print tweet for debugging
print(tweet)
# post tweet
post_tweet(tweet, api)
# run
if __name__ == "__main__":
main()