-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
56 lines (48 loc) · 1.65 KB
/
bot.py
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
import os
import time
import tweepy
import random
SLEEP_TIME = 600
def get_random_sentence_from_file():
with open('lyrics.txt', encoding="utf8") as f:
lines = f.read().splitlines()
return random.choice(lines)
def twitter_api():
keys_file = open("keys.txt")
lines = keys_file.readlines()
consumer_key = lines[0].rstrip()
consumer_secret = lines[1].rstrip()
access_token = lines[2].rstrip()
access_token_secret = lines[3].rstrip()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
api = twitter_api()
for tweet in tweepy.Cursor(api.search, q='#twentyonepilots').items():
follow = 0
try:
print('\nTweet by: @' + tweet.user.screen_name)
#tweet.retweet()
#print('Retweeted the tweet')
# Favorite the tweet
tweet.favorite()
print('Favorited the tweet')
#Follow the user who tweeted
#check that bot is not already following the user
if not tweet.user.following:
tweet.user.follow()
print('Followed the user')
follow +=1
if (follow==5):
tweet_text = str(get_random_sentence_from_file())
tweet_text = tweet_text.replace('\\n','\n') + '\n#twentyonepilots'
print(tweet_text)
api.update_status(status=tweet_text)
print("status Upadate Successfull")
follow = 0
time.sleep(SLEEP_TIME)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break