-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
74 lines (59 loc) · 1.67 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import tweepy
import random
import time
import logging
from constants import transcripts
from os import environ
#Declaring credentials
CONSUMER_KEY = environ['CONSUMER_KEY']
CONSUMER_SECRET = environ['CONSUMER_SECRET']
ACCESS_KEY = environ['ACCESS_KEY']
ACCESS_SECRET = environ['ACCESS_SECRET']
#Every hour a new tweet will be posted
INTERVAL = 60 * 60
def friendships(api):
followers = api.followers_ids('avatar_texts')
friends = api.friends_ids('avatar_texts')
#Following non-protected followers
for user in followers:
try:
if user not in friends and not api.get_user(user).protected:
api.get_user(user).follow()
except tweepy.TweepError:
continue
#Unfollowing non-followers
for user in friends:
try:
if user not in followers:
api.destroy_friendship(user)
except tweepy.TweepError:
continue
def poster(api):
try:
api.update_status(random.choice(transcripts))
except tweepy.TweepError as err:
if err.api_code == 187:
logging.error("Status duplicated")
else:
raise err
def main():
logging.basicConfig(level=logging.INFO)
#Logging in credentials to Twitter API
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY,ACCESS_SECRET)
#Authenticating while setting up boundaries
api = tweepy.API(auth, wait_on_rate_limit = True,
wait_on_rate_limit_notify = True)
#Validating credentials
try:
api.verify_credentials()
logging.info('Authentication OK')
except:
logging.error("Error during Authentication")
#Running bot
while True:
poster(api)
friendships(api)
time.sleep(INTERVAL)
if __name__ == '__main__':
main()