This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_followings.py
169 lines (136 loc) · 6.3 KB
/
auto_followings.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
# (c) 2017 Exceen
import tweepy
import argparse
from os import makedirs, path
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
dev_consumer_key = ''
dev_consumer_secret = ''
dev_access_token = ''
dev_access_token_secret = ''
class TwitterAccount(object):
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
self.api = tweepy.API(auth)
self.username = None
self.friends = None
self.followers = None
def get_username(self):
if self.username == None:
self.username = self.api.me().screen_name
return self.username
def set_username(self, username):
self.username = self.api.get_user(username).screen_name
self.friends = None
self.followers = None
def get_friends(self):
if self.friends == None:
self.friends = [str(friend) for friend in self.api.friends_ids(self.get_username())]
return self.friends
def get_followers(self):
if self.followers == None:
self.followers = [str(follower) for follower in self.api.followers_ids(self.get_username())]
return self.followers
class Followings(object):
def __init__(self, account):
self.account = account
self.workpath = path.join(path.dirname(path.realpath(__file__)), 'data')
self.database_file = path.join(self.workpath, 'followers.db')
def __get_friends_file(self):
return path.join(self.workpath, self.account.get_username() + '_friends')
def __get_followers_file(self):
return path.join(self.workpath, self.account.get_username() + '_followers')
def get_username_from_database(self, user_id):
db = []
if path.exists(self.database_file):
db = [line.strip() for line in open(self.database_file, 'r')]
for record in db:
if str(user_id) in record:
return record.split('|')[-1]
return None
def get_username(self, user_id):
username = self.get_username_from_database(user_id)
if username == None:
try:
return self.account.api.get_user(user_id).screen_name
except Exception, e:
pass
else:
return username
return '-Unkown User-'
def get_friends_from_database(self):
if path.exists(self.__get_friends_file()):
return [line.strip() for line in open(self.__get_friends_file())]
else:
return self.account.get_friends()
def get_followers_from_database(self):
if path.exists(self.__get_followers_file()):
return [line.strip() for line in open(self.__get_followers_file())]
else:
return self.account.get_followers()
def update_database(self, user_id_list):
db = []
if path.exists(self.database_file):
db = [line.strip() for line in open(self.database_file, 'r')]
user_id_list.append(self.account.get_username())
for user_id in user_id_list:
user_id = str(user_id)
if not any(user_id in data for data in db):
try:
data_set = user_id + '|' + str(self.account.api.get_user(user_id).screen_name)
db.append(data_set)
except tweepy.error.TweepError, err:
continue
with open(self.database_file, 'w') as f:
[f.write('%s\n' % item) for item in db]
def save_followings(self):
if not path.exists(self.workpath):
makedirs(self.workpath)
with open(self.__get_friends_file(), 'w') as f:
[f.write('%s\n' % item) for item in self.account.get_friends()]
with open(self.__get_followers_file(), 'w') as f:
[f.write('%s\n' % item) for item in self.account.get_followers()]
self.update_database(self.account.get_friends() + self.account.get_followers())
def main():
parser = argparse.ArgumentParser(description='Followings')
parser.add_argument('-d', '--create-db', action='store_true', help='creates a complete database of your followers (ratelimit)')
parser.add_argument('user', metavar='username', type=str, nargs='?', help='use the given username instead of yours')
args = parser.parse_args()
followings = Followings(TwitterAccount(consumer_key, consumer_secret, access_token, access_token_secret))
if args.user and followings.account.get_username() != args.user:
followings.account.set_username(args.user)
if args.create_db:
followings.update_database(followings.account.get_friends() + followings.account.get_followers())
exit()
####
current_friends = followings.account.get_friends()
current_followers = followings.account.get_followers()
previous_friends = followings.get_friends_from_database()
previous_followers = followings.get_followers_from_database()
unfollowed_friends = list(set(previous_friends) - set(current_friends))
new_friends = list(set(current_friends) - set(previous_friends))
unfollowers = list(set(previous_followers) - set(current_followers))
new_followers = list(set(current_followers) - set(previous_followers))
####
dev_auth = tweepy.OAuthHandler(dev_consumer_key, dev_consumer_secret)
dev_auth.set_access_token(dev_access_token, dev_access_token_secret)
dev_api = tweepy.API(dev_auth)
#Unfollowers
for user_id in unfollowers:
dev_api.send_direct_message(followings.account.get_username(), text='@' + followings.get_username(user_id) + ' unfollowed you.')
#New Followers
for user_id in new_followers:
dev_api.send_direct_message(followings.account.get_username(), text='@' + followings.get_username(user_id) + ' followed you.')
#Unfollowed Friends
for user_id in unfollowed_friends:
dev_api.send_direct_message(followings.account.get_username(), text='You unfollowed @' + followings.get_username(user_id) + '.')
#New Friends
for user_id in new_friends:
dev_api.send_direct_message(followings.account.get_username(), text='You followed @' + followings.get_username(user_id) + '.')
followings.save_followings()
if __name__ == '__main__':
main()