-
Notifications
You must be signed in to change notification settings - Fork 3
/
unfollowtw.py
68 lines (57 loc) · 2.25 KB
/
unfollowtw.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
import logging
import os
import tweepy
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
consumer_key = os.environ.get('CONSUMER_KEY')
consumer_secret = os.environ.get('CONSUMER_SECRET')
access_token = os.environ.get('ACCESS_TOKEN')
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(
auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=10, retry_delay=5,
retry_errors=5
)
current_user = api.me().screen_name
friends = []
with open('friends_{}.txt'.format(current_user), 'r') as f:
for line in f:
friends.append(line.strip())
friends_cache = []
with open('friends_un_cache_{}.txt'.format(current_user), 'r') as f:
for line in f:
friends_cache.append(line.strip())
list_friends = [x for x in friends if x not in friends_cache]
with open('friends_un_cache_{}.txt'.format(current_user), 'a') as f:
try:
for fr in list_friends:
try:
if api.get_user(fr).followers_count < 300:
api.destroy_friendship(fr)
logger.info("Unfollow %s", fr)
f.write(fr + ' \n')
else:
logger.info("Add %s to file", fr)
f.write(fr + ' \n')
except Exception:
logging.exception("Pass first try!")
try:
if len(api.followers_ids(fr)) < 300:
api.destroy_friendship(fr)
logger.info("Unfollow %s", fr)
f.write(fr + ' \n')
else:
logger.info("Add %s to file", fr)
f.write(fr + ' \n')
except Exception:
logging.exception("Pass second try!")
pass
except Exception:
logging.exception("Pass iterate through the list!")
pass
if __name__ == "__main__":
main()