Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of words tweeted and choose random word at the end of the day #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/credentials.cpython-36.pyc
Binary file not shown.
78 changes: 59 additions & 19 deletions tweet_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,64 @@
from credentials import *
import os.path

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
time_delay = 12*60*60 #12 hours delay
def get_def():
file = open('dir_to_lookup/barrons800.txt', 'r')
data = file.readlines()
while True:
to_send = random.choice(data).strip()
if len(to_send) < 280 and len(to_send) > 5:
break
return to_send

#api.update_status('testing a twitter bot. continue scrolling.')
def reply():
for status in tweepy.Cursor(api.retweets_of_me()).items():
tweet_id = status.id_str
parent_id = status.retweeted_status.id_str
tweet_text = status.text
with open("answers.txt", "a") as file:
lines = file.readlines()

def get_def():
file = open('dir_to_lookup/barrons800.txt', 'r')
data = file.readlines()
while True:
to_send = random.choice(data).strip()
if len(to_send) < 280 and len(to_send) > 5:
break
return to_send

while True:
to_write = get_def()
print(to_write)
api.update_status(to_write)
time.sleep(time_delay)
for line in lines:
words = line.split()
if words[0] == parent_id and words[1].lower() == tweet_text.tolower():
api.update_status('You are correct!',tweet_id);

def main():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

no_of_words_per_day = 6
time_delay = (12*60*60) // no_of_words_per_day
time_delay = 2
word_count = 0;
todays_words = []


while True:
if word_count < no_of_words_per_day:
to_write = get_def()
tweet = api.update_status(to_write)
tweet_id = tweet.id_str
word = (tweet_id, to_write.split(':')[0].strip())
todays_words.append(word)
word_count += 1

reply()
time.sleep(time_delay)

elif word_count == no_of_words_per_day:
print('\ntodays_words:',todays_words)
question = list(random.choice(todays_words))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could by default make word a list type, instead of a tuple

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two elements of the tuple (tweet_id and answer) are always needed together so it makes sense to combine them in a tuple. Also, a list would be useful in cases where there is a possibility of adding more elements. That isn't the case here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but since you're converting it back to a list type later on, might as well have it as a list in the first place. just for cleaner code that's all

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant lines and comments, and test the code. I'll merge the PR.

print('random word:', question[1])

with open("answers.txt", "a") as file:
prajwalgatti marked this conversation as resolved.
Show resolved Hide resolved
file.write(str(question).strip('[').strip(']')+"\n")

todays_words.clear()
word_count = 0;
print("Going to sleep.....")
time.sleep(12*60*60)

if __name__ == '__main__':
main()